Thanks, but printing doesn't work within the function either. (i.e, no result or output, or error). Also, like I said, the loop is working fine on its own (so the path, name, filename, and all other variables called from the function exist, are available and are recognized just fine). It just doesn't do anything (anymore) if the loop is inside a function. On Tue, 2017-04-04 at 11:21 +0200, peter dalgaard wrote:> How about inserting print() statements on the output of "ls()" and > the value of "filename". In particular, is the value of Plots_path > the same as last week? > > -pd > > > > On 4 Apr 2017, at 10:50 , DANIEL PRECIADO <danprec at hotmail.com> > > wrote: > > > > The following function is supposed to search the workspace and save > > plots??(i.e. listing all objects in the workspace named "Figs", > > which > > are all ggplot2 plots, and saving them as png files) > > > > SaveFigs <- function() > > { > > for (i in ls(pattern="_Figs_")) > > { > > filename = paste(Plots_Path, i, ".png", sep="") > > png(filename) > > print(eval(as.name(i))) > > dev.off() > > } > > } > > > > > > It was working perfectly until some days ago, but now nothing > > happens > > when the function is called. No error, no output, no result, no > > files, > > nothing at all. Completely useless. > > > > If I run the for loop inside alone, without the function, it works > > perfectly and produces the expected result (png files in the > > defined > > folder). But running it as a function doesn't do anything at all. > > > > Can anyone explain why did this function simply and suddenly > > stopped > > working? > > > > (using R version 3.3.3 on an ubuntu 16.10, if that is of any help) > > ______________________________________________ > > R-help at r-project.org 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-g > > uide.html > > and provide commented, minimal, self-contained, reproducible code. > >
Given the following little experiment> foobar <- 1 > f <- function() ls() > f()character(0)> f <- function(x) ls() > f(2)[1] "x">... I am pretty sure that your code _never_ actually worked. It probably helps if you tell ls() which environment to list, as in:> f <- function() ls(.GlobalEnv) > f()[1] "f" "foobar"> On 4 Apr 2017, at 12:27 , DANIEL PRECIADO <danprec at hotmail.com> wrote: > > Thanks, but printing doesn't work within the function either. (i.e, no > result or output, or error). Also, like I said, the loop is working > fine on its own (so the path, name, filename, and all other variables > called from the function exist, are available and are recognized just > fine). It just doesn't do anything (anymore) if the loop is inside a > function. > > > On Tue, 2017-04-04 at 11:21 +0200, peter dalgaard wrote: >> How about inserting print() statements on the output of "ls()" and >> the value of "filename". In particular, is the value of Plots_path >> the same as last week? >> >> -pd >> >> >>> On 4 Apr 2017, at 10:50 , DANIEL PRECIADO <danprec at hotmail.com> >>> wrote: >>> >>> The following function is supposed to search the workspace and save >>> plots (i.e. listing all objects in the workspace named "Figs", >>> which >>> are all ggplot2 plots, and saving them as png files) >>> >>> SaveFigs <- function() >>> { >>> for (i in ls(pattern="_Figs_")) >>> { >>> filename = paste(Plots_Path, i, ".png", sep="") >>> png(filename) >>> print(eval(as.name(i))) >>> dev.off() >>> } >>> } >>> >>> >>> It was working perfectly until some days ago, but now nothing >>> happens >>> when the function is called. No error, no output, no result, no >>> files, >>> nothing at all. Completely useless. >>> >>> If I run the for loop inside alone, without the function, it works >>> perfectly and produces the expected result (png files in the >>> defined >>> folder). But running it as a function doesn't do anything at all. >>> >>> Can anyone explain why did this function simply and suddenly >>> stopped >>> working? >>> >>> (using R version 3.3.3 on an ubuntu 16.10, if that is of any help) >>> ______________________________________________ >>> R-help at r-project.org 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-g >>> uide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >>-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
To your first comment: Yes, the function used to work, and the loop inside it still does (as indicated in my first email). I wouldn't bother asking otherwise. To your second, no, specifying the environment in the ls() call doesn't help, the problem persist. On Tue, 2017-04-04 at 15:26 +0200, peter dalgaard wrote: Given the following little experiment foobar <- 1 f <- function() ls() f() character(0) f <- function(x) ls() f(2) [1] "x" ... I am pretty sure that your code _never_ actually worked. It probably helps if you tell ls() which environment to list, as in: f <- function() ls(.GlobalEnv) f() [1] "f" "foobar" On 4 Apr 2017, at 12:27 , DANIEL PRECIADO <danprec at hotmail.com<mailto:danprec at hotmail.com>> wrote: Thanks, but printing doesn't work within the function either. (i.e, no result or output, or error). Also, like I said, the loop is working fine on its own (so the path, name, filename, and all other variables called from the function exist, are available and are recognized just fine). It just doesn't do anything (anymore) if the loop is inside a function. On Tue, 2017-04-04 at 11:21 +0200, peter dalgaard wrote: How about inserting print() statements on the output of "ls()" and the value of "filename". In particular, is the value of Plots_path the same as last week? -pd On 4 Apr 2017, at 10:50 , DANIEL PRECIADO <danprec at hotmail.com<mailto:danprec at hotmail.com>> wrote: The following function is supposed to search the workspace and save plots (i.e. listing all objects in the workspace named "Figs", which are all ggplot2 plots, and saving them as png files) SaveFigs <- function() { for (i in ls(pattern="_Figs_")) { filename = paste(Plots_Path, i, ".png", sep="") png(filename) print(eval(as.name(i))) dev.off() } } It was working perfectly until some days ago, but now nothing happens when the function is called. No error, no output, no result, no files, nothing at all. Completely useless. If I run the for loop inside alone, without the function, it works perfectly and produces the expected result (png files in the defined folder). But running it as a function doesn't do anything at all. Can anyone explain why did this function simply and suddenly stopped working? (using R version 3.3.3 on an ubuntu 16.10, if that is of any help) ______________________________________________ R-help at r-project.org<mailto:R-help at r-project.org> 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-g uide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]