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

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)

Related

Reading files in R inside a bash loop

I want to run a R script for 23 chromosomes. The files I need to read are "chr_1.txt, chr_2.txt,...,chr_23.txt"
So I have a bash file
#!/bin/bash
for chr in {1..23}; do \
sbatch torunR.sh "chr_$"
done
and another bash file (torunR.sh)
R CMD BATCH script.R
The problem is that I don't know how to read a different "chr_X.txt" files in R (script.R).
I have tried chr_$ or chr_*', for example:
geno = read.table(file="chr_*.txt") but it didn't work.
Any ideas?? Thanks!!
I'm not an expert in bash scripting so I don't know the necessity of batching, but I would modify your R script to use a command line argument created within the loop.
Your R script would look like this:
## script.R
targetFile <- commandArgs(trailingOnly = TRUE)
# optional status message
cat(sprintf("Processing file %s\n", targetFile))
geno <- read.table(file = targetFile)
Then modify your bash script to be something along the lines of
#!/bin/bash
for chr in {1..23}; do \
Rscript script.R "chr_$chr.txt"
done
Result when running bash script (with optional status message):
$ ./bashScript.sh
Processing file chr_1.txt
Processing file chr_2.txt
Processing file chr_3.txt
Processing file chr_4.txt
Processing file chr_5.txt
Processing file chr_6.txt
Processing file chr_7.txt
Processing file chr_8.txt
Processing file chr_9.txt
Processing file chr_10.txt
Processing file chr_11.txt
Processing file chr_12.txt
Processing file chr_13.txt
Processing file chr_14.txt
Processing file chr_15.txt
Processing file chr_16.txt
Processing file chr_17.txt
Processing file chr_18.txt
Processing file chr_19.txt
Processing file chr_20.txt
Processing file chr_21.txt
Processing file chr_22.txt
Processing file chr_23.txt

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 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.

Exporting .csv from R & Batch file

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

Scheduling R Script

I have written an R script that pulls some data from a database, performs several operations on it and post the output to a new database.
I would like this script to run every day at a specific time but I can not find any way to do this effectively.
Can anyone recommend a resource I could look at to solve this issue? I am running this script on a Windows machine.
Actually under Windows you do not even have to create a batch file first to use the Scheduler.
Open the scheduler: START -> All Programs -> Accesories -> System Tools -> Scheduler
Create a new Task
under tab Action, create a new action
choose Start Program
browse to Rscript.exe which should be placed e.g. here:
"C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe"
input the name of your file in the parameters field
input the path where the script is to be found in the Start in field
go to the Triggers tab
create new trigger
choose that task should be done each day, month, ... repeated several times, or whatever you like
Supposing your R script is mytest.r, located in D:\mydocuments\, you can create a batch file including the following command:
C:\R\R-2.10.1\bin\Rcmd.exe BATCH D:\mydocuments\mytest.r
Then add it, as a new task, to windows task scheduler, setting there the triggering conditions.
You could also omit the batch file. Set C:\R\R-2.10.1\bin\Rcmd.exe in the program/script textbox in task scheduler, and give as Arguments the rest of the initial command: BATCH D:\mydocuments\mytest.r
Scheduling R Tasks via Windows Task Scheduler (Posted on February 11, 2015)
taskscheduleR: R package to schedule R scripts with the Windows task manager (Posted on March 17, 2016)
EDIT
I recently adopted the use of batch files again, because I wanted the cmd window to be minimized (I couldn't find another way).
Specifically, I fill the windows task scheduler Actions tab as follows:
Program/script:
cmd.exe
Add arguments (optional):
/c start /min D:\mydocuments\mytest.bat ^& exit
Contents of mytest.bat:
C:\R\R-3.5.2\bin\x64\Rscript.exe D:\mydocuments\mytest.r params
Now there is built in option in RStudio to do this, to run scheduler first install below packages
install.packages('data.table')
install.packages('knitr')
install.packages('miniUI')
install.packages('shiny')
install.packages("taskscheduleR", repos = "http://www.datatailor.be/rcube", type =
"source")
After installing go to
**TOOLS -> ADDINS ->BROWSE ADDINS ->taskscheduleR -> Select it and execute it.**
Setting up the task scheduler
Step 1) Open the task scheduler (Start > search Task Scheduler)
Step 2) Click "Action" > "Create Task"
Step 3) Select "Run only when the user is logged on", uncheck "Run with highest priveledges", name your task,
configure for "Windows Vista/Windows Server 2008"
Step 4) Under the "Triggers" tab, set when you would like the script to run
Step 5) Under the "Actions" tab, put the full location of the Rscript.exe file, i.e.
"C:\Program Files\R\R-3.6.2\bin\Rscript.exe" (include the quotes)
Put the name of your script with with -e and source() in arguments wrapping it like this:
-e "source('C:/location_of_my_script/test.R')"
Troubleshooting a Rscript scheduled in the Task Scheduler
When you run a script using the Task Scheduler, it is difficult to troubleshoot any issues because you don't get any error messages.
This can be resolved by using the sink() function in R which will allow you to output all error messages to a file that you specify. Here is how you can do this:
# Set up error log ------------------------------------------------------------
error_log <- file("C:/location_of_my_script/error_log.Rout", open="wt")
sink(error_log, type="message")
try({
# insert your code here
})
The other thing that you will have to change to make your Rscript work is to specify the full file path of any file paths in your script.
This will not work in task scheduler:
source("./functions/import_function.R")
You will need to specify the full file path of any scripts you are sourcing within your Rscript:
source("C:/location_of_my_script/functions/import_function.R")
Additionally, I would remove any special characters from any file paths that you are referencing in your R script. For example:
df <- fread("C:/location_of_my_data/file#2342.csv")
may not run. Instead, try:
df <- fread("C:/location_of_my_data/file_2342.csv")
Changing windows passwords
Beware: Changing windows passwords will pause your task scheduler script(s). You will need to log back into the task scheduler and enter your password to get them started again.
I set up my tasks via the SCHTASKS program. For running scripts on startup, you would write something along the lines of
SCHTASKS /Create /SC ONSTART /TN MyProgram /TR "R CMD BATCH --vanilla d:\path\to\script.R"
See this website for more details on SCHTASKS. More details at Microsoft's website.
You can use Windows Task Scheduler.
After following any combination of these steps and you receive the "Argument Batch Ignored" error after R.exe runs, try this, it worked for me.
In Windows Task Scheduler:
Replace BATCH "C:\Users\desktop\yourscript.R"in the arguments field
with
CMD BATCH --vanilla --slave "C:\Users\desktop\yourscript.R"

Resources