在包形状中有一个叫做的函数
estcov
使用某种方法来给出张量的均值,但这不是问题的关键。每次调用此函数时都会出现一个情节,我想在不触及函数代码的情况下停止绘图,有一些技巧可以做到这一点吗?这里有一个riproducible代码
S <- array(0,c(5,5,10) )
for (i in 1:10){
tem <- diag(5)+.1*matrix(rnorm(25),5,5)
S[,,i]<- tem
}
estcov( S , method="Procrustes")
最好的方法是将所有内容发送到NULL
dev,然后关闭它:
pdf(file = NULL)
estcov( S , method="Procrustes")
dev.off()
您可以在estcov
周围创建一个包装器,将图形重定向到临时文件,然后将其删除。
estcov_no_plot <- function(...) {
temp_plot <- tempfile()
png(temp_plot)
on.exit({
dev.off(dev.cur())
file.remove(temp_plot)
})
shapes::estcov(...)
}
此示例使用函数的新名称来提醒您它不是原始名称。您可以将其命名为estcov
,它将替换您环境中的软件包功能,但这可能会引起混淆。
您可以做的是自己删除部分图(下面已注释):
my_estcov <- function (S, method = "Riemannian", weights = 1, alpha = 1/2,
MDSk = 2)
{
out <- list(mean = 0, sd = 0, pco = 0, eig = 0, dist = 0)
M <- dim(S)[3]
if (length(weights) == 1) {
weights <- rep(1, times = M)
}
if (method == "Procrustes") {
dd <- estSS(S, weights)
}
if (method == "ProcrustesShape") {
dd <- estShape(S, weights)
}
if (method == "Riemannian") {
dd <- estLogRiem2(S, weights)
}
if (method == "Cholesky") {
dd <- estCholesky(S, weights)
}
if (method == "Power") {
dd <- estPowerEuclid(S, weights, alpha)
}
if (method == "Euclidean") {
dd <- estEuclid(S, weights)
}
if (method == "LogEuclidean") {
dd <- estLogEuclid(S, weights)
}
if (method == "RiemannianLe") {
dd <- estRiemLe(S, weights)
}
out$mean <- dd
sum <- 0
for (i in 1:M) {
sum <- sum + weights[i] * distcov(S[, , i], dd, method = method)^2/sum(weights)
}
out$sd <- sqrt(sum)
dist <- matrix(0, M, M)
for (i in 2:M) {
for (j in 1:(i - 1)) {
dist[i, j] <- distcov(S[, , i], S[, , j], method = method)
dist[j, i] <- dist[i, j]
}
}
out$dist <- dist
if (M > MDSk) {
ans <- cmdscale(dist, k = MDSk, eig = TRUE, add = TRUE,
x.ret = TRUE)
out$pco <- ans$points
out$eig <- ans$eig
#if (MDSk > 2) {
# shapes3d(out$pco[, 1:min(MDSk, 3)], axes3 = TRUE)
#}
#if (MDSk == 2) {
# plot(out$pco, type = "n", xlab = "MDS1", ylab = "MDS2")
# text(out$pco[, 1], out$pco[, 2], 1:length(out$pco[,
# 1]))
#}
}
out
}
您甚至可以添加一个参数(逻辑plot = F
)来控制绘图或输出。