Paul Menzel
2011-Jul-29 13:28 UTC
[R] scripting/littler: How to call function named iteratively (`f1`, `f2`, …)?
Dear R folks, wanting to compare different implementations of a solution I want to script it to iterate over the different implementations. Is there a way to do this in the R shell/command line? $ more /tmp/iterf.r f1 <- function(n = 100000, l = 100000) { z = n + l } f2 <- function(n = 100000, l = 100000) { z = 2 * (n + l) } I tried the following, but it of course does not work. > source("/tmp/iterf.r") > print(f1(2,3)) [1] 5 > print(f2(2,3)) [1] 10 > for (i in 1:2) { print( fi(2, 3) ) } Fehler in print(fi(2, 3)) : konnte Funktion "fi" nicht finden Can I compose a command from values of variables? Going on I tried to script that using the `r` from the package `littler` [1]. Unfortunately because of the required quotes "" for the command `source()` I am not able to expand the variable. $ for i in $(seq 2); do r -e "print($i)" ; done [1] 1 [1] 2 $ for i in $(seq 2); do r -e 'source("/tmp/iterf.r"); print(1)' ; done [1] 1 [1] 1 $ # The next example does not work, because the variable $i does not get expanded when surrounded by ''. $ for i in $(seq 2); do r -e 'source("/tmp/iterf.r"); print(f$i(2, 3))' ; done Fehler in print(f$i(2, 3)) : Objekt 'f' nicht gefunden Ausf?hrung angehalten Fehler in print(f$i(2, 3)) : Objekt 'f' nicht gefunden Ausf?hrung angehalten Searching for ?iterating function names? with rseek.org did give any good results. I also read the appendix in the introduction to R [2], but this did not have anything regarding to this either. Is there a way to script this? Thanks, Paul [1] http://packages.debian.org/sid/littler [2] http://cran.r-project.org/doc/manuals/R-intro.html#Scripting-with-R -------------- next part -------------- f1 <- function(n = 100000, l = 100000) { z = n + l } f2 <- function(n = 100000, l = 100000) { z = 2 * (n + l) } -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110729/e425a028/attachment.bin>
Eik Vettorazzi
2011-Jul-29 13:46 UTC
[R] scripting/littler: How to call function named iteratively (`f1`, `f2`, …)?
Hi Paul, how about this for (i in 1:2) { print( do.call(paste("f",i,sep=""),list(2, 3) )) } or using get for (i in 1:2) { print( get(paste("f",i,sep=""))(2, 3) ) } cheers Am 29.07.2011 15:28, schrieb Paul Menzel:> Dear R folks, > > > wanting to compare different implementations of a solution I want to > script it to iterate over the different implementations. Is there a way > to do this in the R shell/command line? > > $ more /tmp/iterf.r > f1 <- function(n = 100000, > l = 100000) > { > z = n + l > } > > f2 <- function(n = 100000, > l = 100000) > { > z = 2 * (n + l) > } > > I tried the following, but it of course does not work. > > > source("/tmp/iterf.r") > > print(f1(2,3)) > [1] 5 > > print(f2(2,3)) > [1] 10 > > for (i in 1:2) { print( fi(2, 3) ) } > Fehler in print(fi(2, 3)) : konnte Funktion "fi" nicht finden > > Can I compose a command from values of variables? > > Going on I tried to script that using the `r` from the package `littler` > [1]. Unfortunately because of the required quotes "" for the command > `source()` I am not able to expand the variable. > > $ for i in $(seq 2); do r -e "print($i)" ; done > [1] 1 > [1] 2 > $ for i in $(seq 2); do r -e 'source("/tmp/iterf.r"); print(1)' ; done > [1] 1 > [1] 1 > $ # The next example does not work, because the variable $i does not get expanded when surrounded by ''. > $ for i in $(seq 2); do r -e 'source("/tmp/iterf.r"); print(f$i(2, 3))' ; done > Fehler in print(f$i(2, 3)) : Objekt 'f' nicht gefunden > Ausf?hrung angehalten > Fehler in print(f$i(2, 3)) : Objekt 'f' nicht gefunden > Ausf?hrung angehalten > > Searching for ?iterating function names? with rseek.org did give any > good results. I also read the appendix in the introduction to R [2], but > this did not have anything regarding to this either. > > Is there a way to script this? > > > Thanks, > > Paul > > > [1] http://packages.debian.org/sid/littler > [2] http://cran.r-project.org/doc/manuals/R-intro.html#Scripting-with-R > > > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790
David Winsemius
2011-Jul-29 13:58 UTC
[R] scripting/littler: How to call function named iteratively (`f1`, `f2`, …)?
On Jul 29, 2011, at 9:28 AM, Paul Menzel wrote:> Dear R folks, > > > wanting to compare different implementations of a solution I want to > script it to iterate over the different implementations. Is there a > way > to do this in the R shell/command line?You might consider examining the short and seemingly very well written `benchmark` function in the rbenchmark:package. It is designed as a testing mechanism but should have the capacity after being stripped of some of its bells and whistles to do what you ask. You can probably even get it to do what you want by adjusting a few parameters.> > $ more /tmp/iterf.r > f1 <- function(n = 100000, > l = 100000) > { > z = n + l > } > > f2 <- function(n = 100000, > l = 100000) > { > z = 2 * (n + l) > } > > I tried the following, but it of course does not work. > >> source("/tmp/iterf.r") >> print(f1(2,3)) > [1] 5 >> print(f2(2,3)) > [1] 10 >> for (i in 1:2) { print( fi(2, 3) ) } > Fehler in print(fi(2, 3)) : konnte Funktion "fi" nicht finden > > Can I compose a command from values of variables?It's also possible that you want some combination of 'assign' or 'ge't with paste("name" , 1:10, sep="_"). Or, perish the thought, eval(parse(text=paste("f", 1:2, sep="")))> > Going on I tried to script that using the `r` from the package > `littler` > [1]. Unfortunately because of the required quotes "" for the command > `source()` I am not able to expand the variable.I wasn't able to parse that sentence into standard R/English, but it may be that my lack of use of littler is at fault.> > $ for i in $(seq 2); do r -e "print($i)" ; done > [1] 1 > [1] 2 > $ for i in $(seq 2); do r -e 'source("/tmp/iterf.r"); > print(1)' ; done > [1] 1 > [1] 1 > $ # The next example does not work, because the variable $i > does not get expanded when surrounded by ''. > $ for i in $(seq 2); do r -e 'source("/tmp/iterf.r"); print(f > $i(2, 3))' ; done > Fehler in print(f$i(2, 3)) : Objekt 'f' nicht gefunden > Ausf?hrung angehalten > Fehler in print(f$i(2, 3)) : Objekt 'f' nicht gefunden > Ausf?hrung angehalten > > Searching for ?iterating function names? with rseek.org did give any > good results. I also read the appendix in the introduction to R [2], > but > this did not have anything regarding to this either. > > Is there a way to script this?library(fortunes) fortune("Yoda") # surely applies here David Winsemius, MD West Hartford, CT
Paul Menzel
2011-Jul-29 14:08 UTC
[R] [solved] scripting/littler: How to call function named iteratively (`f1`, `f2`, …)?
Dear Eik, Am Freitag, den 29.07.2011, 15:46 +0200 schrieb Eik Vettorazzi:> how about this > for (i in 1:2) { print( do.call(paste("f",i,sep=""),list(2, 3) )) } > > or using get > for (i in 1:2) { print( get(paste("f",i,sep=""))(2, 3) ) }works great for me. Thank you very much. Regarding search machines it is sad that the names of these commands are so general ? which is of course a good thing when knowing them and using them. Thanks, Paul -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110729/ed61ce7b/attachment.bin>
Paul Menzel
2011-Jul-30 12:56 UTC
[R] [solved] scripting/littler: How to call function named iteratively (`f1`, `f2`, …)?
Am Freitag, den 29.07.2011, 15:28 +0200 schrieb Paul Menzel:> wanting to compare different implementations of a solution I want to > script it to iterate over the different implementations. Is there a way > to do this in the R shell/command line? > > $ more /tmp/iterf.r > f1 <- function(n = 100000, > l = 100000) > { > z = n + l > } > > f2 <- function(n = 100000, > l = 100000) > { > z = 2 * (n + l) > }[?]> Going on I tried to script that using the `r` from the package `littler` > [1]. Unfortunately because of the required quotes "" for the command > `source()` I am not able to expand the variable. > > $ for i in $(seq 2); do r -e "print($i)" ; done > [1] 1 > [1] 2 > $ for i in $(seq 2); do r -e 'source("/tmp/iterf.r"); print(1)' ; done > [1] 1 > [1] 1 > $ # The next example does not work, because the variable $i does not get expanded when surrounded by ''. > $ for i in $(seq 2); do r -e 'source("/tmp/iterf.r"); print(f$i(2, 3))' ; done > Fehler in print(f$i(2, 3)) : Objekt 'f' nicht gefunden > Ausf?hrung angehalten > Fehler in print(f$i(2, 3)) : Objekt 'f' nicht gefunden > Ausf?hrung angehalten > > Searching for ?iterating function names? with rseek.org did give any > good results. I also read the appendix in the introduction to R [2], but > this did not have anything regarding to this either. > > Is there a way to script this?Searching for ?source code? in the R Wiki, I found the Wiki page scriptingr [3] ? which did not turn up in my other searches. As it turned out you can encode `"` by `\"` in Bash too. So the following works just fine. $ for i in $(seq 2); do r -e "source(\"/tmp/iterf.r\"); print(f$i(2, 3)) )" ; done [1] 5 [1] 10 Thanks, Paul> [1] http://packages.debian.org/sid/littler > [2] http://cran.r-project.org/doc/manuals/R-intro.html#Scripting-with-R[3] http://rwiki.sciviews.org/doku.php?id=tips:scriptingr -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110730/6087b480/attachment.bin>