3

I am trying to turn off the display of plot in R.

I read Disable GUI, graphics devices in R but the only solution given is to write the plot to a file.

What if I don't want to pollute the workspace and what if I don't have write permission ? I tried options(device=NULL) but it didn't work.

The context is the package NbClust : I want what NbClust() returns but I do not want to display the plot it does.

Thanks in advance !

edit : Here is a reproducible example using data from the rattle package :)

data(wine, package="rattle")
df <- scale (wine[-1])

library(NbClust)

# This produces a graph output which I don't want
nc <- NbClust(df, min.nc=2, max.nc=15, method="kmeans")

# This is the plot I want ;)
barplot(table(nc$Best.n[1,]), 
    xlab="Numer of Clusters", ylab="Number of Criteria",
    main="Number of Clusters Chosen by 26 Criteria")
Community
  • 1
  • 1
asachet
  • 6,620
  • 2
  • 30
  • 74
  • give a reproducible example. `NbClust` does not produce a graph when I run the first example in `help(NbClust)` – Spacedman Jul 15 '14 at 13:24
  • Do you have permissions to create a temporary file with `tempfile()` ? – Andrie Jul 15 '14 at 13:27
  • I edited in a reproducible example :) I just looked into tempfile(), thank you! Writing to the tmp directory seems a good alternative to turning off the plot display. – asachet Jul 15 '14 at 13:48
  • What OS is this machine? Can you write to the NULL device (NUL in windows or /dev/null in linux?) If yes, then you can direct output there. – konvas Jul 15 '14 at 13:52
  • Producing a graph without giving the option of not producing the graphs is pretty sucky. I'd ask the devs for a `plot=FALSE` option. – Spacedman Jul 15 '14 at 13:54

2 Answers2

10

You can wrap the call in

pdf(file = NULL)

and

dev.off()

This sends all the output to a null file which effectively hides it.

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
4

Luckily it seems that NbClust is one giant messy function with some other functions in it and lots of icky looking code. The plotting is done in one of two places.

Create a copy of NbClust:

> MyNbClust = NbClust

and then edit this function. Change the header to:

MyNbClust <-
    function (data, diss = "NULL", distance = "euclidean", min.nc = 2, 
              max.nc = 15, method = "ward", index = "all", alphaBeale = 0.1, plotetc=FALSE) 
{

and then wrap the plotting code in if blocks. Around line 1588:

    if(plotetc){
        par(mfrow = c(1, 2))
        [etc]
        cat(paste(...
    }

and similarly around line 1610. Save. Now use:

 nc = MyNbClust(...etc....)

and you see no plots unless you add plotetc=TRUE.

Then ask the devs to include your patch.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Line 1610? How big is this function... *checks NbClust*. Oh dear - I'm guessing you used grep to find the plotting commands because I hope you didn't search the entire function. – Dason Jul 15 '14 at 14:11
  • 1
    Yeah, search in my editor did the trick. The bonus of it all being in one function is that there's no worry about unexported things in the package not being found by a user-space copy... – Spacedman Jul 15 '14 at 14:15