1

I'm developing a bash program that execute a R oneliner command to convert a RMarkdown template into a HTML document.

This R oneliner command looks like:

R -e 'library(rmarkdown) ; rmarkdown::render( "template.Rmd", "html_document", output_file = "report.html", output_dir = "'${OUTDIR}'", params = list( param1 = "'${PARAM1}'", param2 = "'${PARAM2}'", ... ) )

I have a long list of parameters, let's say 10 to explain the problem, and it seems that the R or bash has a command line length limit.

When I execute the R oneliner with 10 parameters I obtain a error message like this:

WARNING: '-e library(rmarkdown)~+~;~+~rmarkdown::render(~+~"template.Rmd",~+~"html_document",~+~output_file~+~=~+~"report.html",~+~output_dir~+~=~+~"output/",~+~params~+~=~+~list(~+~param1~+~=~+~"param2", ...
Fatal error: you must specify '--save', '--no-save' or '--vanilla'

When I execute the R oneliner with 9 parameters it's ok (I tried different combinations to verify that the problem was not the last parameter).

When I execute the R oneliner with 10 parameters but with removing all spaces in it, it's ok too so I guess that R or bash use a command line length limit.

R -e 'library(rmarkdown);rmarkdown::render("template.Rmd","html_document",output_file="report.html",output_dir="'${OUTDIR}'",params=list(param1="'${PARAM1}'",param2="'${PARAM2}'",...))

Is it possible to increase this limit?

miken32
  • 42,008
  • 16
  • 111
  • 154
thbtmntgn
  • 151
  • 2
  • 8
  • Based on [this SO question](https://stackoverflow.com/questions/19354870/bash-command-line-and-input-limit) your commands are nowhere near the length limit, which is imposed by the OS. Maybe tell us which OS you are using and which bash tool. I have never encountered this program as an "end user" of the bash. – Tim Biegeleisen Feb 19 '18 at 04:26
  • 5
    `R_ReadConsole()` used to have buflen=1024. I've not tested with the latest version. To confirm it, `echo` your oneliner and pipe it to `wc -c` command. The limitation is hard coded in R and cannot increase unless you modify the source code and recompile it. Why don't you write an R script instead? – tshiono Feb 19 '18 at 06:32
  • OS is Amazon Linux 2017.09 and bash is version 4.2.46. – thbtmntgn Feb 19 '18 at 23:18
  • I use `wc -c` to check the length and it seems that the limit is somewhere between 9941 and 9993 characters – thbtmntgn Feb 19 '18 at 23:48
  • I'll try with a script thanks! – thbtmntgn Feb 19 '18 at 23:50

1 Answers1

0

This will break a number of ways – including if your arguments have spaces or quotes in them.

Instead, try passing the values as arguments. Something like this should give you an idea how it works:

# create a script file
tee arguments.r << 'EOF'
argv <- commandArgs(trailingOnly=TRUE)
arg1 <- argv[1]
print(paste("Argument 1 was", arg1))
EOF

# set some values
param1="foo bar"
param2="baz"

# run the script with arguments
Rscript arguments.r "$param1" "$param2"

Expected output:

[1] "Argument 1 was foo bar"

Always quote your variables and always use lowercase variable names to avoid conflicts with system or application variables.

miken32
  • 42,008
  • 16
  • 111
  • 154