2017-07-26 79 views
1

假設我想運行的功能運行v功能與32位R參數,內部的64位R

test.function <- function(arg1){ 
        print(arg1) 
       } 

如何運行,讓說:

在32位模式下
test.function("Hello world") 

,使用64位R?我已成功使用

system(paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe ",'"some_script.R"')) 

但在32位模式下運行的整個腳本如何我可以改變這一點,以便它可以運行參數的函數,而不是整個腳本?

編輯

繼羅馬Luštrik答案和運行

system('Rscript test.script.R "hello"') 

使我有以下錯誤:

Error in winDialog(type = "ok", message = message_text) : winDialog() cannot be used non-interactively call: -> check.for.updates.R -> winDialog Running stopped

Warning message: running command 'Rscript test.script.R "hello"' had status 1

(該錯誤消息是我的母語,所以我有翻譯幾句話,所以文字可能在其他系統上略有不同)

+0

你可以把這一行放到你的函數中嗎? – AK47

+0

可能是可行的,但我還沒有進一步,比運行整個腳本,所以我不知道它應該怎麼做 – Acarbalacar

+0

你的編輯看起來應該是一個新的問題。無論如何,你可能正在使用一些不會非交互式使用的函數。 –

回答

0

我設法找到一個解決方案我自己,通過修改羅馬Luštrik的解決方案。

在他的例子中,我們的腳本調用test_script.R

pathIn32BitRScript <- '"C:/Some path/test_script.R"' 
system(paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe", pathIn32BitRScript, " ", "Hello world")) 
load("Data.Rda") # Loads the results into an object called mydata 
invisible(file.remove("Data.Rda")) # Deletes the file we created 

在:

args <- commandArgs(trailingOnly = TRUE) 

test.function <- function(x) { 
    print(x) 
} 

args.run <- list(x = args) 
mydata <- do.call(test.function, args = args.run) 
save(mydata, file = "Data.Rda") # If you need the output to an R object 
在運行64位R另一個腳本

然後,我們可以在32位R運行此功能這個例子我們有x = "Hello World"。如果你的路徑中有空格,你將需要雙引號,就像我在這個例子中那樣。

1

您不能僅運行特定功能,您將不得不創建腳本。儘管如此,這並不能阻止你創建一個單一功能的腳本。

如果您創建一個名爲test.script.R的腳本,並將其放在可找到它的地方。

args <- commandArgs(trailingOnly = TRUE) 

str(args) 

test.function <- function(x) { 
    message("Your input:") 
    message(x) 
} 

invisible(sapply(args, test.function)) 

打開一個終端窗口。您可以使用Windows'cmd.exe(按Windows鍵並鍵入cmd.exeCommand Prompt或您在本地化版本系統中的任何內容)。導航到腳本所在的位置,並使用以下命令運行它。

$ Rscript test.script.R "hello" "hello" "won't you tell me your name" "i hate the doors" 
chr [1:4] "hello" "hello" "won't you tell me your name" ... 
Your input: 
hello 
Your input: 
hello 
Your input: 
won't you tell me your name 
Your input: 
i hate the doors 

或者,你可以使用system通過R

system('Rscript test.script.R "hello" "hello" "won't you tell me your name" "i hate the doors"') 

通知做同樣的事情,我使用單引號和雙引號的方式。單引號在外面。此調用還假定腳本位於R當前正在查看的工作空間中。不過,您可以使用setwd()來更改它。

+0

當你運行這個腳本時,你到底在做什麼?我可能不得不提及我正在使用Windows 10 – Acarbalacar

+0

@Acarbalacar執行您希望直接運行的一個函數。腳本只是一個包裝器,可以捕獲參數,執行任何潛在的預處理,然後將參數傳遞給函數。上面的腳本在Windows機器上運行,但是通過現在嵌入在RStudio中的'cygwin'終端。 –

+0

對我來說這是一個全新的世界。我確實使用Rstudio,但我沒有使用cygwin的經驗。你能編輯你的答案,提供一步一步的指導來做到這一點,或提供一個鏈接到一個提供這樣的指南的來源? – Acarbalacar