[R] does pdf() work inside of a function?

Anuj Goyal @nuj@goy@| @end|ng |rom gm@||@com
Mon Jul 29 01:40:28 CEST 2019


Why does pdf() work outside a function, but not inside it?

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))
  dev.off()
}
gs("t")

csvText <- "s,n\nAFL,AFLAC\nAIG,AIG"
csv <- read.csv(text=csvText, stringsAsFactors = FALSE)
symVec <- getSymbols(as.vector(csv$s))

# create PDF
pdf(file = "t2.pdf")
par(mfrow = c( 4,2 ) )
mapply (chart_Series, mget(symVec))
dev.off()


>>> output

-rw-r--r--@ 1 b  staff     4053 Jul 28 16:15 t.pdf
-rw-r--r--@ 1 b  staff   154506 Jul 28 16:15 t2.pdf



More information about the R-help mailing list