Exporting .csv from R & Batch file - r

I run a report in R every morning and I'm trying to automate this task. I have a Windows machine and I've created a task within Task Scheduler. I can get the file to run at a certain time, but I can't get it to export the csv. My initial thoughts is that there is a disconnect between forward- & back-slashes, but I'm not sure where the break is. Anyone have any thoughts?
R_script.R
setwd('C:/Users/Me/Desktop')
x <- runif(5)
y <- runif(5)
xy <- data.frame(X = x, Y = y)
write.csv(xy, 'C:/Users/Me/Desktop/xy.csv')
Batch File
Rscript CMD BATCH
C:\Users\Me\R_script.R

Try running the first line of your batch file in a cmd window. It results in an error:
>Rscript CMD BATCH
Fatal error: cannot open file 'CMD': No such file or directory
And if you use R CMD BATCH it doesn't detect the input file because they should be on the same line:
>R CMD BATCH
no input file
Instead run the command in one of these two ways, with the file path on the same line:
>Rscript C:\Users\Me\R_script.R
>R CMD BATCH C:\Users\Me\R_script.R

Related

how to pause an R script until cmd prompt is running?

How can I assign with in Rscript to pause until the command prompt is open?
I am running a Rscript where a batch file will be executed through command prompt. Later I want to copy a product of executed batch file to another folder. However, Rscript tries to copy while it is being written.
This batch file will open multiple cmd prompts to execute multiple files, so I don't want to use "shell", "system", or "system2" instead of "shell.exec2".
lapply(1:num_cores, function(o) {
## batch file location
loc <- paste0(projectfolder_location, pp, "\\ppfolder_", o, ".Sufi2.SwatCup")
## executing batch file
shell.exec2(paste0(pp_folder, "\\SUFI2_Run.bat"))
})
#PAUSING HERE until cmd prompt is open!!
file.copy(list.files(
path=paste0(projectfolder_location, pp, "\\ppfolder_", 1, ".Sufi2.SwatCup"),
pattern="Name.out$", full.name=TRUE), projectfolder, overwrite=TRUE)

Spaces in paths in batch mode R

I'm trying to get an R script to run from a batch file so it can be nice and clean for other users. Currently, you drag and drop a CSV file onto the batch file and it passes the file name to the R script for input.
When there's a space in the file path/name it works fine in RStudio but causes problems when I call it from the batch file. When I do that it tries to open the path before the space.
I've tried to reformat the file path from within R by using shortPathName(inputPath) and by replacing spaces with "\ " but it doesn't seem to work.
At the moment, the script is launched with
"%~dp0\R-3.6.0\bin\R.exe" CMD BATCH "--args %~1" "%~dp0\Script.R"
with the script containing
args <- commandArgs(TRUE)
inputPath <- args[1]
inputPath <- shortPathName(inputPath)
inputData <- read.csv(inputPath)
It runs fine from within RStudio but crashes when launched from the batch producing this error message in the output file:
Error in file(file, "rt") : cannot open the connection
Calls: read.csv -> read.table -> file
In addition: Warning message:
In file(file, "rt") :
cannot open file 'file path up to the space': No such file or directory
Execution halted
By no means a R expert, but I'd try
%~dp0\R-3.6.0\bin\R.exe" CMD BATCH "--args %~s1" "%~dp0\Script.R"
The %~s1 should supply the short filename as the argument.
After trying several formulations of the batch file and some debugging, I found that the batch file was passing the first part of the file before the space as the first argument.
After finding that the use of R in CMD BATCH mode is no longer advisable so switched to running using Rscript mode as
"%~dp0\R-3.6.0\bin\Rscript.exe" --vanilla "%~dp0\Script.R" "%~1"
This allowed for the argument to be passed to R with "", and hence with the space.
Since v3.5.1, R accepts file paths with spaces.

Correct way to use here package with cronR scheduling

I've been using the here package to make my projects more portable. It works great apart from when I use cronR to schedule some of my scripts. When I run my_script.R from Rstudio I get a message from library(here):
here() starts at /home/pd/projects/my_proj
When I set script.R to run using cronR I get a different message:
here() starts at /home/pd
Which is where my_schedule.cron is stored. Ideally I want to keep my_schedule.cron where it is. I can see from the logs that my_script.R runs fine apart from when it comes to saving data because the path used by here() is incorrect. Is there anyway to get the here function to detect the project dir when my_script.R is run from cronR or the terminal?
You can modify the command cmd that is usually created with cron_rscript() by adding cd to your project folder followed by the usual part:
cmd <- "cd /home/pd/projects/my_proj && /usr/lib/R/bin/Rscript ./my_script.R >> ./my_script.log 2>&1"
cron_add(command = cmd, frequency = 'daily', at = '18:00')
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Learn how to use environment variables
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Or use the 'Additional arguments to Rscript' element in the cronR user interface to pass something extra to the Rscript and fetch it with commandArgs().
If you don't use the cronR interface but cron_rscript, use cronR::cron_rscript(..., rscript_args = "/home/pd/projects/my_proj")
args <- commandArgs(trailingOnly = TRUE)
if(length(args) > 0){
wd <- args[1]
}

How do I run an r script from within an r script from within power BI?

I have script like:
library('RPostgreSQL')
source('credentials.R') # Defines USERNAME, PASSWORD, DBNAME, PORT, and HOST
postgres <- dbConnect(PostgreSQL(),
user=USERNAME,
password=PASSWORD,
dbname=DBNAME,
port=PORT,
host=HOST)
results <- dbGetQuery(postgres, "SELECT * FROM table;")
The script runs fine from RStudio, but when I run it as a data source in Power BI, 'credentials.R' is not found. How do I run this?
The issue is the working directory. Within RStudio, the console is in the directory that contains 'credentials.R'. Add the following line to the beginning of your script:
setwd('/path/to/working/directory')

How to wait for user input in R-script ran from Windows CMD

I'm new to R and I'm stuck in a very simple task.
I want to run an R script from console, but I want the script to be able to read user inputs.
This is how I'm reading from the script:
library = readLines(n = 1L)
if(library == "1")
{
library = "GUDHI"
}
And this is how I'm running my script from R-Portable with a .bat:
#echo on
cd..
cd..
cd..
cd..
cd..
PATH C:\Users\MyUser\Desktop\App\RFolder\R-Portable\App\R-Portable\bin;%path%
cd C:\Users\MyUser\Desktop\App\RFolder
Rscript Phom.R 1
pause
When I run this .bat it throws an error (Argument is of length zero):
As if the console didn't wait for user input.
The problem is not the .bat code. If I remove the readLines functions from my script and hardcode the input, it works perfectly. I also tried the readline function with no success.
Thanks.
Solution for Interactive R script from Windows CMD:
cat("Prompt Message: ")
library = readLines(con = "stdin", 1)
I'm not sure if the prompt MUST end with ": ", but I had trouble when I removed that piece of string.
This worked for me, I hope this helps somebody.

Resources