lgndrzzz
lgndrzzz

Reputation: 108

Prevent a function to block

In the package shapes there is a function called

estcov

that uses some method to give mean of tensors, but this is not the point of the question. Everytime this function is called appears a plot, i want to stop it plotting without touching the code of the function,there is some trick to do this? Here there is a riproducible code

 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")

Upvotes: 0

Views: 40

Answers (3)

Kevin Cazelles
Kevin Cazelles

Reputation: 1255

What you can do is remove the part with the plot yourself (commented out below):

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
}

You can even add a parameter (a logical plot = F) to control when plot or outputs.

Upvotes: 0

Colin FAY
Colin FAY

Reputation: 5109

The best way to do that is to send everything to a NULL dev, and close it afterward :

pdf(file = NULL)
estcov( S , method="Procrustes")
dev.off()

Upvotes: 1

Nathan Werth
Nathan Werth

Reputation: 5273

You could create a wrapper around estcov that redirects graphics to a temporary file, which is deleted afterwards.

estcov_no_plot <- function(...) {
  temp_plot <- tempfile()
  png(temp_plot)
  on.exit({
    dev.off(dev.cur())
    file.remove(temp_plot)
  })
  shapes::estcov(...)
}

This example uses a new name for the function to remind you it's not the original. You could name it estcov, which would replace the package's function in your environment, but that might cause confusion.

Upvotes: 0

Related Questions