Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query data.frame through Bash script argument with Rscript

Tags:

r

If I have an R script:

#! /usr/bin/env Rscript
args <- commandArgs(TRUE)

t <- read.table(args[2], header = TRUE)
print(t$args[1])

q(status = 0)

which I use with the TSV file "example-table.tsv":

"a" "b"
1 3
2 2
3 1

...using the Bash command: ./example.R a example-table.tsv... (after making the R script executable, of course)

Why does t$args[1] return NULL? How do I get this example to return the proper data.frame column that I specify in the script arguments?

Thanks for you help!

I realize this may or may not be a better question for programming, rather than Cross Validated...?

like image 318
brews Avatar asked Oct 11 '22 05:10

brews


1 Answers

try:

print(t[[args[1]]])

note that t$args[1] is parsed as (t$args)[1] -> NULL[1] -> NULL because your data.frame has no 'args' column.

like image 96
kohske Avatar answered Oct 13 '22 09:10

kohske