运行 参数在 32 位 R 中,在 64 位 R 中的 R 函数
Running R function with arguments in 32 bit R, inside 64 bit R
假设我想运行函数
test.function <- function(arg1){
print(arg1)
}
我怎么能运行,比方说:
test.function("Hello world")
在 32 位模式下,使用 64 位 R?我使用 运行 在 32 位模式下使用
管理了整个脚本
system(paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe ",'"some_script.R"'))
但是我该如何更改它,以便它可以 运行 一个带参数的函数,而不是整个脚本?
编辑
遵循 Roman Luštrik 和 运行ning
的回答
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
(错误信息是我的母语,所以我不得不翻译几个词,所以文本在其他系统上可能会略有不同)
您不能 运行 仅使用特定功能,您必须创建一个脚本。不过,这不会阻止您创建一个只有一个功能的脚本。
如果您创建一个名为 test.script.R
的脚本并将其放在您可以找到的地方。
args <- commandArgs(trailingOnly = TRUE)
str(args)
test.function <- function(x) {
message("Your input:")
message(x)
}
invisible(sapply(args, test.function))
打开一个终端window。您可以使用 Windows' cmd.exe
(按 Windows 键并键入 cmd.exe
或 Command 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()
更改它。
我通过修改 Roman Luštrik 的解决方案,设法自己找到了解决方案。
按照他的例子,我们有一个名为 test_script.R
:
的脚本
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
然后在另一个 运行s 64 位 R 的脚本中,我们可以 运行 在 32 位 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
在这个例子中我们有 x = "Hello World"
。如果您的路径中有空格,您将需要像我在本例中那样使用双引号。
假设我想运行函数
test.function <- function(arg1){
print(arg1)
}
我怎么能运行,比方说:
test.function("Hello world")
在 32 位模式下,使用 64 位 R?我使用 运行 在 32 位模式下使用
管理了整个脚本 system(paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe ",'"some_script.R"'))
但是我该如何更改它,以便它可以 运行 一个带参数的函数,而不是整个脚本?
编辑
遵循 Roman Luštrik 和 运行ning
的回答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
(错误信息是我的母语,所以我不得不翻译几个词,所以文本在其他系统上可能会略有不同)
您不能 运行 仅使用特定功能,您必须创建一个脚本。不过,这不会阻止您创建一个只有一个功能的脚本。
如果您创建一个名为 test.script.R
的脚本并将其放在您可以找到的地方。
args <- commandArgs(trailingOnly = TRUE)
str(args)
test.function <- function(x) {
message("Your input:")
message(x)
}
invisible(sapply(args, test.function))
打开一个终端window。您可以使用 Windows' cmd.exe
(按 Windows 键并键入 cmd.exe
或 Command 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()
更改它。
我通过修改 Roman Luštrik 的解决方案,设法自己找到了解决方案。
按照他的例子,我们有一个名为 test_script.R
:
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
然后在另一个 运行s 64 位 R 的脚本中,我们可以 运行 在 32 位 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
在这个例子中我们有 x = "Hello World"
。如果您的路径中有空格,您将需要像我在本例中那样使用双引号。