2012-03-09 21 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 

在此先感謝。

* *添加的事實之後...這個工程......

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,...)不會做的工作 - 它會保存df變量名稱df

相反,你要使用list參數保存到保存變量的名稱通過。

例如:

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

當你再load('tmp.rda')你必須包含變量XYZ當你保存它不管它包含。

所以,爲您提供:

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

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

謝謝,但沒有奏效。也許你可以看看上面的完整程序輸出。 – 2012-03-09 03:48:43

+0

它沒有工作,但是當你'save'你有你想要使用'list'參數保存什麼名*傳遞,而不是'df'(將被保存的名字'df') :保存(list = Args [1],...)'。 – 2012-03-09 03:53:32

+0

我已經用這個額外的信息更新了我的答案。 – 2012-03-09 03:55:51

0

而不是

Args[1] <- df; 

試試這個:

assign(Args[1],df) 
+0

謝謝,但那沒有奏效。也許你可以看看上面的完整程序輸出。 – 2012-03-09 03:48:20

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 ...