bnosac/taskscheduleR

Error in the placement of the rscript_args argument in taskscheduler_create()

cemoze opened this issue · 5 comments

cemoze commented

I was using the taskscheduler_create() function from the taskscheduleR library to create a task in the Windows Task Scheduler, and I noticed that the rscript_args argument is not placed in the correct location in the command string. Currently, it is appended to the end of the command, but it should be placed between cmd /c and the path to Rscript.exe.

Steps to Reproduce:

  • Install the taskscheduleR library
  • Use the taskscheduler_create() function with the rscript_args argument set to "--encoding=UTF-8"
  • Set the debug argument as TRUE.
  • Observe that the rscript_args argument is placed at the end of the command string.

Expected Behavior:

The rscript_args argument should be placed between cmd /c and the path to Rscript.exe in the command string, as in the command:
cmd path/Rscript.exe --encoding=UTF-8 sample_script.R

But it returns --encoding=UTF-8 argument at the end of the command.
cmd path/Rscript.exe sample_script.R --encoding=UTF-8

Affected Version:

I'm using version 1.6 of the taskscheduleR library, and, version 2022.12.0 of the RStudio. Operating system is Windows Server 2012 R2 Standard in Turkish language.

Possible Solution:

The error comes from the command below in the taskscheduler_create() function:

task <- sprintf("cmd /c %s %s %s >> %s 2>&1", Rexe, 
        shQuote(rscript), paste(rscript_args, collapse = " "), 
        shQuote(sprintf("%s.log", tools::file_path_sans_ext(rscript))))

And it should be replaced with:

task <- sprintf("cmd /c %s %s \"%s\" >> %s 2>&1", Rexe, 
        paste(rscript_args, collapse = " "), shQuote(rscript), 
        shQuote(sprintf("%s.log", tools::file_path_sans_ext(rscript))))

The intention of that argument is the [args] element of Rscript which allows you to pass on extra arguments which can next be extracted with the help of commandArgs()
An example is put here: https://github.com/bnosac/taskscheduleR/blob/master/inst/extdata/helloworld.R and in the README

Rscript --help
Usage: /path/to/Rscript [--options] [-e expr [-e expr2 ...] | file] [args]

--options accepted are
  --help              Print usage and exit
  --version           Print version and exit
  --verbose           Print information on progress
  --default-packages=list
                      Where 'list' is a comma-separated set
                        of package names, or 'NULL'
or options to R, in addition to --no-echo --no-restore, such as
  --save              Do save workspace at the end of the session
  --no-environ        Don't read the site and user environment files
  --no-site-file      Don't read the site-wide Rprofile
  --no-init-file      Don't read the user R profile
  --restore           Do restore previously saved objects at startup
  --vanilla           Combine --no-save, --no-restore, --no-site-file
                        --no-init-file and --no-environ

'file' may contain spaces but not shell metacharacters
Expressions (one or more '-e <expr>') may be used *instead* of 'file'
See also  ?Rscript  from within R
cemoze commented

I know how that argument works but the problem is rscript_args argument is located at the end of the command which leads to an undesired situation. For example, the task I would like to create generates a report with Rmarkdown that renders a PDF file. However, in this report, Unicode characters appear instead of Turkish characters. Therefore, encoding should be forced to set to the UTF-8 in Rscript and Rscript function should be run with --encoding=UTF-8 argument in cmd command line. Besides, this argument has to be placed just after the Rscript command. However, with the taskscheduler_create() function, this argument is located just after the path of the script (at the end of the command). You can see the example at the Expected Behavior section of my comment above.

Rscript has arguments [args] at the end - see the help
There is no --encoding argument, the --encoding argument is part of the R.exe, not part of Rscript.exe

$ Rscript --help
Usage: Rscript [options] file [args]
   or: Rscript [options] -e expr [-e expr2 ...] [args]
A binary front-end to R, for use in scripting applications.

Options:
  --help              Print usage and exit
  --version           Print version and exit
  --verbose           Print information on progress
  --default-packages=LIST  Attach these packages on startup;
                        a comma-separated LIST of package names, or 'NULL'
and options to R (in addition to --no-echo --no-restore), for example:
  --save              Do save workspace at the end of the session
  --no-environ        Don't read the site and user environment files
  --no-site-file      Don't read the site-wide Rprofile
  --no-init-file      Don't read the user R profile
  --restore           Do restore previously saved objects at startup
  --vanilla           Combine --no-save, --no-restore, --no-site-file,
                        --no-init-file and --no-environ

Expressions (one or more '-e <expr>') may be used *instead* of 'file'.
Any additional 'args' can be accessed from R via 'commandArgs(TRUE)'.
See also  ?Rscript  from within R.
R --help
Usage: Rterm [options] [EnvVars]

Start R, a system for statistical computation and graphics, with the
specified options

EnvVars: Environmental variables can be set by NAME=value strings

Options:
  -h, --help            Print usage message and exit
  --version             Print version info and exit
  --encoding=enc        Specify encoding to be used for stdin
  --encoding enc        ditto
  --save                Do save workspace at the end of the session
  --no-save             Don't save it
  --no-environ          Don't read the site and user environment files
  --no-site-file        Don't read the site-wide Rprofile
  --no-init-file        Don't read the .Rprofile or ~/.Rprofile files
  --restore             Do restore previously saved objects at startup
  --no-restore-data     Don't restore previously saved objects
  --no-restore-history  Don't restore the R history file
  --no-restore          Don't restore anything
  --workspace=file      Workspace to be restored
  --vanilla             Combine --no-save, --no-restore, --no-site-file,
                          --no-init-file and --no-environ
  --max-ppsize=N        Set max size of protect stack to N
  -q, --quiet           Don't print startup message
  --silent              Same as --quiet
  --no-echo             Make R run as quietly as possible
  --verbose             Print more information about progress
  --args                Skip the rest of the command line
  --ess                 Don't use getline for command-line editing
                          and assert interactive use
  -f file               Take input from 'file'
  --file=file           ditto
  -e expression         Use 'expression' as input

One or more -e options can be used, but not together with -f or --file

An argument ending in .RData (in any case) is taken as the path
to the workspace to be restored (and implies --restore)



Or: R CMD command args

where 'command' is one of:
  INSTALL  Install add-on packages
  REMOVE   Remove add-on packages
  SHLIB    Make a DLL for use with dynload
  BATCH    Run R in batch mode
  build    Build add-on packages
  check    Check add-on packages
  Rprof    Post process R profiling files
  Rdconv   Convert Rd format to various other formats
  Rdiff    difference R output files
  Rd2pdf   Convert Rd format to PDF
  Rd2txt   Convert Rd format to pretty text
  Stangle   Extract S/R code from vignette
  Sweave    Process vignette documentation
  config   Obtain configuration information about R
  open     Open a file via Windows file associations
  texify   Process a latex file

Use
  R CMD command --help
for usage information for each command.
cemoze commented

By typing ?Rscript in R:

Rscript [options] file [args]
Rscript [options] -e expr [-e expr2 ...] [args]

options a list of options, all beginning with '--'. These can be any of the options of the standard R front-end, and also those described in the details.

And in the details section it has been stated that 'Rscript --help gives details of usage'. So as far as I understand, Rscript.exe can also take options of the R.exe. I guess, I made a confusion in terminology (options and arguments).

So hereby I am sharing the Help page of Rscript below;
https://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html

Consequently, I see that there is no error in taskscheduler_create function since args are coming to the end.

On the other hand; I can not see a way for adding an option like --encoding=UTF-8 to the Rscript.exe in built-in taskscheduler_create function.

I've solved my problem with this way. Maybe rscript_opts argument could be added to the taskscheduler_create function to add options between Rscript.exe and R script that would like to be executed. Because I can't use this function without modifying it in my case.

Ok, feel free to create a pull request to add rscript_options as an argument in taskscheduler_create