简体   繁体   English

R、RStudio:以编程方式在 R 的 32 位和 64 位版本之间切换

[英]R, RStudio: Programmatically switch between 32-bit and 64-bit versions of R

Is there a function that, within RStudio, restarts the R console in 64-bit mode or 32-bit mode without re-opening RStudio (or at least automatically re-opening it if that can't be avoided)? Is there a function that, within RStudio, restarts the R console in 64-bit mode or 32-bit mode without re-opening RStudio (or at least automatically re-opening it if that can't be avoided)?

I commonly run in 32-bit when using RODBC so that I can retrieve data from an Access database, but would like to otherwise leverage the capabilities of 64-bit mode for all other tasks while still in RStudio.在使用RODBC时,我通常以 32 位运行,以便我可以从 Access 数据库中检索数据,但我希望在 RStudio 中的所有其他任务中利用 64 位模式的功能。

You could save the part of your code that you wish to execute using the 32-bit executable into a new script.您可以将希望使用 32 位可执行文件执行的部分代码保存到新脚本中。 For example, I have a script called myscript.r which will just print which version of the R executable (64 or 32-bit) which was used to run it:例如,我有一个名为myscript.r的脚本,它将打印用于运行它的 R 可执行文件(64 位或 32 位)的哪个版本:

cat(as.character(version[2]))

Of course you could replace this with the part of your code dealing with RODBC .当然,您可以将其替换为处理RODBC的代码部分。

Now the main way to programmatically run a script with a custom executable is to invoke a command to the OS terminal or shell .现在,使用自定义可执行文件以编程方式运行脚本的主要方法是调用 OS终端shell命令 This command should contain:该命令应包含:

  • Which executable to invoke, in our case R 32-bit (the Rscript.exe file contained in the i386 folder of your R_HOME directory)调用哪个可执行文件,在我们的例子中是 R 32 位( Rscript.exe目录的i386文件夹中包含的R_HOME文件)
  • Arguments accepted by this executable, such as the path to the R script we want to run using this executable. Arguments 被这个可执行文件接受,例如我们想要使用这个可执行文件运行的 R 脚本的路径。

The path of myscript is "c:/gp/trash/myscript.r" , and my 32-bit R executable is myscript的路径是"c:/gp/trash/myscript.r" ,我的 32 位 R 可执行文件是

paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe)
C:/PROGRA~1/R/R-40~1.4/bin/i386/Rscript.exe

I can run this script using:我可以使用以下命令运行此脚本:

myscript <- "c:/gp/trash/myscript.r"
output <- system(paste0(Sys.getenv("R_HOME"), "/bin/x64/Rscript.exe ", myscript), wait = FALSE, invisible = FALSE, intern = T)
output
[1] "x86_64"

output_32 <- system(paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe ", myscript), wait = FALSE, invisible = FALSE, intern = T)
output_32
[1] "i386"

So as you can see we're executing this script from two different executables.如您所见,我们正在从两个不同的可执行文件执行此脚本。 In practice I'd suggest to save the results of your ODBC queries to a file, which you can read in your main x64 R session.在实践中,我建议将 ODBC 查询的结果保存到文件中,您可以在主x64 R session 中读取该文件。


Just a little vocabulary if you don't know some of these terms:如果您不知道其中一些术语,只需一点词汇:

The terms terminal or shell are often used interchangeably.术语终端或 shell 经常互换使用。 In RStudio, if you click on the terminal tab next to console , you'll be able to enter commands that will be processed by a shell .在 RStudio 中,如果您单击控制台旁边的终端选项卡,您将能够输入将由shell处理的命令

  • Shell commands are instructions that instruct the system to do some action. Shell命令是指示系统执行某些操作的指令。
  • A shell is a user interface for accessing the services of an operating system. shell是用于访问操作系统服务的用户界面。
  • A terminal is a wrapper program that runs a shell and allows us to enter commands.终端是一个包装程序,它运行 shell 并允许我们输入命令。

sources: terminal, console and cli , shell commands , scripting with r , this stackoverflow answer来源: 终端、控制台和 clishell 命令使用 r 编写脚本这个 stackoverflow 答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM