source from another R script

How to run R script from another R script and use as a source

Here is a solution with running R script from another R script and using as a source. It comes handy when R script uses different bit version R, or you want to continue script execution with another one.

Run R script from another R script

You can execute R script as you would normally do by using the Windows command line. If your R version is different, then change the path to Rscript.exe. Use double quotes if the file path contains space.

system("cmd.exe", input = paste('"C:\\Program Files\\R\\R-3.6.1\\bin\\Rscript.exe" C:\\Users\\nobody\\Documents\\R\\MyScript.R'))

Source R script from another R script

In this way, you will get R script results (for example, data frames, etc.) and it is also possible to continue working with them.

source("C:/Users/nobody/Documents/R/MyScript.R")

How to run 32 bit R script from 64 bit RStudio and use as a source

It is workaround where first of all you should prepare your 32 bit script to achieve results. Latest RStudio versions come only in 64 bit version, and if you are using some specific ODBC with 32 bit driver, that will help.

In the first script, save necessary objects in RData or another format. In this case, in the working directory. You can check your working directory location with function getwd().

# Save one or multiple objects in RData format in your working directory

save(MyData, file = "MyData.RData")

It the second script execute the first script with 32bit R and load RData file.

system("cmd.exe", input = paste('"C:\\Program Files\\R\\R-3.6.1\\bin\\i386\\Rscript.exe" C:\\Users\\nobody\\Documents\\R\\FirScript32bit.R'))

# Get results from working directory
load("MyData.RData")

# Remove RData file if neccesary
file.remove("MyData.RData")




Posted

in

,

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *