[R] Passing aruments with source
Hi. Im writing a small test program just to see how passing arguments work with R. From the command line everything works as expected but from inside R using source(test.R) i dont know where and how to send in the arguments did try source(test.R --test) but it just says that it cant find or open the file. (without the argument part it can). Anyone know how to make it work? //Joel Damberg -- View this message in context: http://r.789695.n4.nabble.com/Passing-aruments-with-source-tp2531012p2531012.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Passing aruments with source
On Wed, Sep 8, 2010 at 10:11 AM, Joel joda2...@student.uu.se wrote: Hi. Im writing a small test program just to see how passing arguments work with R. From the command line everything works as expected but from inside R using source(test.R) i dont know where and how to send in the arguments did try source(test.R --test) but it just says that it cant find or open the file. (without the argument part it can). Anyone know how to make it work? source() runs the file in the context of the caller, so if you have an 'x' in the caller then the file sourced will see it. So here, where s.R is just: print(x) you can do: x=12 source(s.R) [1] 12 x=99 source(s.R) [1] 99 Barry __ 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.
Re: [R] Passing aruments with source
That is true but then I (or anyone else using the script) most know exactly what the name in the script is to be able to set it correctly and so on. Therefor it would be much better to be able to just send in the value and let the script handle the variable setting. But thx for the answer //Joel Damberg -- View this message in context: http://r.789695.n4.nabble.com/Passing-aruments-with-source-tp2531012p2531087.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Passing aruments with source
On Wed, Sep 8, 2010 at 11:29 AM, Joel joda2...@student.uu.se wrote: That is true but then I (or anyone else using the script) most know exactly what the name in the script is to be able to set it correctly and so on. Therefor it would be much better to be able to just send in the value and let the script handle the variable setting. But thx for the answer Then what you really want to do is write a _function_ that takes arguments, and not a script. Tell your users something like: Do 'source(foo.R)' and then call the fnord function with your data set: fnord(x). Your foo.R file looks like this: fnord =function(x){ print(x) # etc } It's even possible to save functions to .RData files and then tell your users to attach() them, thus not putting objects in their working spaces. And the next step is to write a package for your function... Barry __ 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.
Re: [R] Passing aruments with source
Thats an idea will try it out and see how it works Thanks a lot for your help. Joel -- View this message in context: http://r.789695.n4.nabble.com/Passing-aruments-with-source-tp2531012p2531138.html Sent from the R help mailing list archive at Nabble.com. __ 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.