[R] does pdf() work inside of a function?
    Berwin A Turlach 
    berw|n@tur|@ch @end|ng |rom gm@||@com
       
    Mon Jul 29 06:21:54 CEST 2019
    
    
  
G'day Anuj,
On Sun, 28 Jul 2019 16:40:28 -0700
Anuj Goyal <anuj.goyal using gmail.com> wrote:
> Why does pdf() work outside a function, but not inside it?
Because you use a graphics system that need printing to produce the
plot.  On the command line auto-printing is your friend, but in
functions you have to do it explicitly yourself.
> R version 3.6.0 (2019-04-26) on Mac 10.14.5
> 
> # R --silent --vanilla < s2.r
> library(quantmod)
> options("getSymbols.warning4.0"=FALSE)
> options("getSymbols.yahoo.warning"=FALSE)
> 
> gs <- function(f) {
>   csvText <- "s,n\nAFL,AFLAC\nAIG,AIG"
>   csv <- read.csv(text=csvText, stringsAsFactors = FALSE)
>   symVec <- getSymbols(as.vector(csv$s))
> 
>   # create PDF
>   fname = paste0(f,".pdf")
>   pdf(file = fname)
>   par(mfrow = c( 4,2 ) )
>   mapply (chart_Series, mget(symVec))
Change this to
    mapply( function(x) print(chart_Series(x)), mget(symVec))
and it should work.  At least it does on my machine:
  -rw-r--r--  1 berwin berwin 154281 Jul 29 12:19 t.pdf
Cheers,
	
	Berwin
    
    
More information about the R-help
mailing list