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
f1 <- function(n = 100000,
               l = 100000)
{
   z = n + l
}

f2 <- function(n = 100000,
               l = 100000)
{
   z = 2 * (n + l)
}

Attachment: signature.asc
Description: This is a digitally signed message part

______________________________________________
R-help@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.

Reply via email to