[R] using var from bash in R script

2012-01-04 Thread dood
Dear R users,

This probably a really noob question, but I'm stuck. I'd like to pass some
variables from bash to R as strings. I can successfully pass variables using
commandArgs(), the problem is that I end up with an array. So, for example:

 Args - commandArgs(TRUE)
 Args
[1] one   two   three

Now, it just so happens that one, two, three are names of columns that
I'd like to work with. I'd like to do something like this:

 print(summary(lm(Args[1] ~ Args[2])))

But, this doesn't work. The alternative would be to let bash write a number
of R-scripts and then rm them when done, but that seems like an unnecessary
step. 

Can this be done?

Thanks



--
View this message in context: 
http://r.789695.n4.nabble.com/using-var-from-bash-in-R-script-tp4262857p4262857.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] using var from bash in R script

2012-01-04 Thread Marc Schwartz

On Jan 4, 2012, at 3:08 PM, dood wrote:

 Dear R users,
 
 This probably a really noob question, but I'm stuck. I'd like to pass some
 variables from bash to R as strings. I can successfully pass variables using
 commandArgs(), the problem is that I end up with an array. So, for example:
 
 Args - commandArgs(TRUE)
 Args
 [1] one   two   three
 
 Now, it just so happens that one, two, three are names of columns that
 I'd like to work with. I'd like to do something like this:
 
 print(summary(lm(Args[1] ~ Args[2])))
 
 But, this doesn't work. The alternative would be to let bash write a number
 of R-scripts and then rm them when done, but that seems like an unnecessary
 step. 
 
 Can this be done?
 
 Thanks


See ?as.formula and ?paste

Something along the lines of the following should work:

Args - c(one, two, three)

 Args
[1] one   two   three
 
 paste(Args[1], ~, Args[2])
[1] one ~ two

 as.formula(paste(Args[1], ~, Args[2]))
one ~ two

Then use:

summary(lm(as.formula(paste(Args[1], ~, Args[2]


If 'one', 'two' and 'three' are columns in a data frame (say 'DF'), you will 
want to use the data argument in the call to lm():

  summary(lm(as.formula(paste(Args[1], ~, Args[2])), data = DF))

HTH,

Marc Schwartz

__
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] using var from bash in R script

2012-01-04 Thread David Winsemius


On Jan 4, 2012, at 4:08 PM, dood wrote:


Dear R users,

This probably a really noob question, but I'm stuck. I'd like to  
pass some
variables from bash to R as strings. I can successfully pass  
variables using

commandArgs(), the problem is that I end up with an array.


Huh? You should be getting a character vector.


So, for example:


Args - commandArgs(TRUE)
Args

[1] one   two   three

Now, it just so happens that one, two, three are names of  
columns



 columns? Of what? Or perhaps ... in what? Does this what have a  
name in the R workspace?



that
I'd like to work with. I'd like to do something like this:


print(summary(lm(Args[1] ~ Args[2])))


No 'data' argument to lm. You would be getting better answers if you  
provided more specifics.




But, this doesn't work. The alternative would be to let bash write a  
number
of R-scripts and then rm them when done, but that seems like an  
unnecessary

step.

Can this be done?


require(fortunes)
fortune(Yoda)



Thanks



--
View this message in context: 
http://r.789695.n4.nabble.com/using-var-from-bash-in-R-script-tp4262857p4262857.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.


David Winsemius, MD
West Hartford, CT

__
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] using var from bash in R script

2012-01-04 Thread dood

Marc Schwartz-3 wrote
 
 See ?as.formula and ?paste
 
 Something along the lines of the following should work:
 
 Args - c(one, two, three)
 
 Args
 [1] one   two   three
  
 paste(Args[1], ~, Args[2])
 [1] one ~ two
 
 as.formula(paste(Args[1], ~, Args[2]))
 one ~ two
 
 Then use:
 
 summary(lm(as.formula(paste(Args[1], ~, Args[2]
 
 
 If 'one', 'two' and 'three' are columns in a data frame (say 'DF'), you
 will want to use the data argument in the call to lm():
 
   summary(lm(as.formula(paste(Args[1], ~, Args[2])), data = DF))
 

Thanks, that works!

--
View this message in context: 
http://r.789695.n4.nabble.com/using-var-from-bash-in-R-script-tp4262857p4263235.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.