Not_Grothendieck
Not_Grothendieck

Reputation: 5

How to use PowerShell to schedule sourcing an R script?

My goal is to use PowerShell to schedule the sourcing of an R script.

My current work flow is that I open RStudio, click the "Source" button in the upper right corner. Then I wait until it's finished, and close RStudio. I change nothing in the R script.

In PowerShell I've been using its Register-ScheduledJob cmdlet to kick off C# programs on a daily schedule. And here's the problem, I can't find an example of effectively using PowerShell to source an R script.

I believe the PowerShell script should probably use the Invoke-Expression cmdlet. But I'm not 100% sure.

To no avail I've tried this: Start-Process "C:\Program Files\R\R-3.2.4revised\bin\x64\Rterm.exe" -RedirectStandardInput "C:\MyScript.R"

Also, I'd like to avoid the solution that uses CMD BATCH as that's defeating the purpose of using PowerShell.

Upvotes: 0

Views: 3582

Answers (1)

Martin Lhotsky
Martin Lhotsky

Reputation: 456

If just sourcing the R script is what you're looking for then one way to do is something like this

& "C:\Program Files\R\R-3.1.1\bin\Rscript.exe" "C:/Program Files/R/R-3.1.1/tests/demos.R"

where "C:\Program Files\R\R-3.1.1\bin\Rscript.exe" is path Rscript in your local R installation and "C:/Program Files/R/R-3.1.1/tests/demos.R" is path to script you'd normally source() directly in RStudio. One thing to keep in mind is depending on location of files your R script needs you might need to adjust your script with appropriate setwd()

Upvotes: 3

Related Questions