Suppressing console output in R

87 views Asked by At

I'm trying to plot several functions, certain number of which is controlled by a slider. But when I change value of a slider besides changes in plot, also unnecessary information in console appears. My code:

options(warn=-1)
library(ggplot2)
library(manipulate)

plotss<-function(n){
  l=0
  l[1]=curve(dnorm(x,0,1),-15,15,col=1,ylab='y')
  if (n>1){
    for (i in 2:n){
      l[i]=curve(dnorm(x,0,i),-15,15,col=i,add=TRUE)
    }
  }
  names=0
  for (i in 1:n){
    names[i]<-paste("N(0,",as.character(i),")",sep="")
  }
  colours=0
  colours[1:n]=1:n
  legend(3,0.4,legend=names,fill=colours)
  return(l)
}

uppern=readline("uppern= ")
manipulate(plotss(k),k=slider(min=1,max=strtoi(uppern),step=1))

Console looks like:

[[<value of a slider>]]
  [1] -15.0 -14.7 -14.4 -14.1 -13.8 -13.5 -13.2 -12.9 -12.6 -12.3 -12.0 -11.7
 [13] -11.4 -11.1 -10.8 -10.5 -10.2  -9.9  -9.6  -9.3  -9.0  -8.7  -8.4  -8.1
 [25]  -7.8  -7.5  -7.2  -6.9  -6.6  -6.3  -6.0  -5.7  -5.4  -5.1  -4.8  -4.5
 [37]  -4.2  -3.9  -3.6  -3.3  -3.0  -2.7  -2.4  -2.1  -1.8  -1.5  -1.2  -0.9
 [49]  -0.6  -0.3   0.0   0.3   0.6   0.9   1.2   1.5   1.8   2.1   2.4   2.7
 [61]   3.0   3.3   3.6   3.9   4.2   4.5   4.8   5.1   5.4   5.7   6.0   6.3
 [73]   6.6   6.9   7.2   7.5   7.8   8.1   8.4   8.7   9.0   9.3   9.6   9.9
 [85]  10.2  10.5  10.8  11.1  11.4  11.7  12.0  12.3  12.6  12.9  13.2  13.5
 [97]  13.8  14.1  14.4  14.7  15.0

New message like that generates in console with every change of slider's state

I tried quiet, suppress messages, echo, but maybe I just tried to use them in the wrong place

2

There are 2 answers

2
israil_doberman On

remove the return statement.as it is printing the values retuned by curve – user20650

0
phili_b On

Add invisible() to plotss().

uppern=readline("uppern= ")

manipulate(invisible(plotss(k)),k=slider(min=1,max=strtoi(uppern),step=1))

Written with lines and indentations to see invisible():

uppern=readline("uppern= ")

manipulate(
  invisible(
    plotss(k)
  ),
  k=slider(min=1,max=strtoi(uppern),step=1)
)

Tested ok with RStudio Server 2023.06.2 for Ubuntu.

https://stat.ethz.ch/R-manual/R-devel/library/base/html/invisible.html