does pdf() work inside of a function?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

does pdf() work inside of a function?

Anuj Goyal
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: does pdf() work inside of a function?

Duncan Murdoch-2
On 28/07/2019 7:40 p.m., Anuj Goyal wrote:
> Why does pdf() work outside a function, but not inside it?

Try a simpler example, and you'll see that it does, e.g.

  f <- function() { pdf("temp.pdf"); plot(1, main="inside f"); dev.off() }

   You're doing very complicated things in your function; quantmod does
very complicated things in getSymbols().  One of those complicated
things is breaking, it's not pdf() that's breaking.

Duncan Murdoch

>
> 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
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: does pdf() work inside of a function?

Anuj Goyal
Thanks!  I think the quantmod maintainer is trying to make it a bit
more functional so there are fewer side effects.
I'll try to find a workaround.

On Sun, Jul 28, 2019 at 4:58 PM Duncan Murdoch <[hidden email]> wrote:

>
> On 28/07/2019 7:40 p.m., Anuj Goyal wrote:
> > Why does pdf() work outside a function, but not inside it?
>
> Try a simpler example, and you'll see that it does, e.g.
>
>   f <- function() { pdf("temp.pdf"); plot(1, main="inside f"); dev.off() }
>
>    You're doing very complicated things in your function; quantmod does
> very complicated things in getSymbols().  One of those complicated
> things is breaking, it's not pdf() that's breaking.
>
> Duncan Murdoch
>
> >
> > 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
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>


--
-Anuj

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: does pdf() work inside of a function?

Berwin A Turlach-3
In reply to this post by Anuj Goyal
G'day Anuj,

On Sun, 28 Jul 2019 16:40:28 -0700
Anuj Goyal <[hidden email]> 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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.