mcjudd
mcjudd

Reputation: 1580

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)?

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.

Upvotes: 28

Views: 7120

Answers (1)

gaut
gaut

Reputation: 5958

You could save the part of your code that you wish to execute using the 32-bit executable into a new script. 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:

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

Of course you could replace this with the part of your code dealing with 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. 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)
  • Arguments accepted by this executable, such as the path to the R script we want to run using this executable.

The path of myscript is "c:/gp/trash/myscript.r", and my 32-bit R executable is

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.


Just a little vocabulary if you don't know some of these terms:

The terms terminal or shell are often used interchangeably. 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.

  • Shell commands are instructions that instruct the system to do some action.
  • A shell is a user interface for accessing the services of an operating system.
  • A terminal is a wrapper program that runs a shell and allows us to enter commands.

sources: terminal, console and cli, shell commands, scripting with r, this stackoverflow answer

Upvotes: 2

Related Questions