Disable graph display in R

I am trying to disable the graph display in R.

I am reading Disable GUI, graphics devices in R , but the only solution is to write the graph to a file.

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

The context is the NbClust package: I want NbClust() return, but I do not want to display the graph it makes.

Thanks in advance!

edit: Here is a reproducible example using data from a 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") 
0
source share
2 answers

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.

+4
source

Fortunately, it seems that NbClust is one gigantic messy function with some other functions in it and lots of bad code. Bookmarking is done in one of two places.

Create a copy of NbClust :

 > MyNbClust = NbClust 

and then edit this function. Change the title 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 build code in if blocks. On 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 don’t see any graphs unless you add plotetc=TRUE .

Then ask the developers to enable your patch.

+1
source

Source: https://habr.com/ru/post/1242115/


All Articles