2012-03-09 22 views
1

データフレーム名をコマンドライン引数で指定したい。次のことは、私がやろうとしていることを明確にするはずです...私は願っています!R - データフレーム名をコマンドライン引数で指定したい。

使用して実行します。Cの

rterm --vanilla < c:/temp/myprog.txt --args XYZ 

内容:/temp/myprog.txt:事前に

# I am using command line arguments 
Args <- commandArgs(TRUE); 

# Args[1] is the desired dataframe name 
print(Args[1]); 

# Create a simple dataframe 
df <- c(c(1,2),c(3,4)); 
print(df); 

# Save it 
path <- 'c:/temp/mydata.rdata' 
save(df, file=path); 

# Clear the dataframe from memory 
rm(df); 

# Is it really gone? 
print(df); 

# Load the dataframe from disk 
load(path); 

# Did you get it? 
print(df); 

# --- This is where things start to go bad --- 
# --- I know this is wrong, and I know why --- 
# --- but it should make clear what it is --- 
# --- I am attempting to do.    --- 

# Copy it to dataframe with name passed from command line 
Args[1] <- df; 

# Write it to disk with the new name 
save(Args[1], file=path); 

# Clear the dataframe from memory 
rm(Args[1]); 

# Is it really gone? 
print(Args[1]); 

# Load the dataframe from disk 
load(path); 

# Did you get it? 
print(Args[1]); 

# That's all 

感謝を。

* * ADDED事実の後に...この作品...

C:\Program Files\R\R-2.14.2\bin\x64>rterm --vanilla < c:/temp/myprog.txt --args XYZ 

> # I am using command line arguments 
> Args <- commandArgs(TRUE); 
> 
> # Args[1] is the desired dataframe name 
> print(Args[1]); 
[1] "XYZ" 
> 
> # Create a simple dataframe 
> df <- c(c(1,2),c(3,4)); 
> print(df); 
[1] 1 2 3 4 
> 
> # Save it 
> path <- 'c:/temp/mydata.rdata' 
> save(df, file=path); 
> 
> # Clear dataframe so I can see if it 
> # is really populated by the load 
> rm(df); 
> 
> # Load the dataframe from disk 
> load(path); 
> 
> # Did you get it? 
> print(df); 
[1] 1 2 3 4 
> 
> # --- This is where things start to go bad --- 
> # --- I know this is wrong, and I know why --- 
> # --- but it should make clear what it is --- 
> # --- I am attempting to do.    --- 
> 
> # Copy it to dataframe with name passed from command line 
> assign(Args[1], df); 
> 
> # Write it to disk with the new name 
> save(list=Args[1], file=path); 
> 
> # Clear memory so I can see if the dataframe 
> # is really populated by the load 
> rm(df); 
> rm(XYZ); 
> 
> # Load the dataframe from disk 
> load(path); 
> 
> # Did you get it? Is its name as expected? 
> # (In subsequent processing I will be able to 
> # hard code the name as shown here.) 
> print(XYZ); 
[1] 1 2 3 4 
> 

答えて

2

assign(Args[1],df) 

を試してみてください(?assignを参照してください)。

Args[1]文字列「XYZ」が含まれている場合、あなたはXYZによってデータフレームを参照することができるようになります。

例えば:

dfname <- 'XYZ' # your Args[1] I presume 
df <- data.frame(a=runif(10)) # random data frame 

assign(dfname,df) 

# now I can access df by typing XYZ: 
XYZ 
XYZ$a 

# etc. 

あなたが救う、save(df,...)が仕事を行うことはありません - それは変数名dfdfを節約できます。

代わりに、list引数を使用して保存する変数の名前を渡して保存します。例えば

save(list='XYZ',file='tmp.rda') 

あなたは、あなたがあなたがそれを保存したとき、それが含まれているものは何でも含む変数XYZを持っていますload('tmp.rda')。あなたのためにそう

、:

# to show it works: 
path <- 'tmp.rda' 
save(list=Args[1],file=path) 

rm(list=ls()) 
load(path) 
print(XYZ) # this will work. 
+0

ありがとう、それは動作しませんでした。おそらく、上記の完全なプログラム出力を見ることができます。 –

+0

これはうまくいきましたが、 'save'すると、' df'(これは 'df'という名前で保存されます)ではなく、' list'引数を使って保存したいものの* name *を渡す必要があります。 : 'save(list = Args [1]、...)'を実行します。 –

+0

私はこの追加情報で私の答えを更新しました。 –

0

代わりの

Args[1] <- df; 

これを試してみてください。

assign(Args[1],df) 
+0

ありがとうございますが、うまくいきませんでした。おそらく、上記の完全なプログラム出力を見ることができます。 –

0

saveRDSreadRDS機能が動作し使用して代わりのジミーにデータをしよう.frameをカスタム名に変換しますか?そのような

何か:

x <- data.frame(a=1:10, b=letters[1:10]) 
saveRDS('some.file.rds') 
rm(x) 

XYZ <- readRDS('some.file.rds') 
## Carry on ... 
関連する問題