3

I'm trying to achieve what you might naively write as:

R -e "
rmarkdown::render(
  'MyDocument.Rmd', 
  params = list(
    year = 2017
  ),
  output_file = 'ExampleRnotebook.html'
)
"

So that I can make nicely formatted submission scripts to run on a cluster.

I've tried some variants on the below, I'm wondering if there might be an alternative approach to do this with the R -f flag?

read -r -d '' EXP << EOF
rmarkdown::render(
  'MyDocument.Rmd',
  params = list(
    year = 2017
  ),
  output_file = 'ExampleRnotebook.html'
)
EOF
R -e "$EXP"

but I get a series of errors that look like this:

ARGUMENT 'params~+~=~+~list(' __ignored__

for the different lines of the expression, followed by:

> rmarkdown::render(
+ 
+ Error: unexpected end of input

To reproduce:

MyDocument.Rmd =

---
title: "R Notebook"
output: html_notebook
params: 
  year: 0000
---

```{r}
params$year

```

This works fine:

read -r -d '' EXP <<- EOF 
rmarkdown::render('MyDocument.Rmd', params = list(year = 2017 ), output_file = 'ExampleRnotebook.html')
EOF
R -e "$EXP"

but gets hard to read with longer param lists

zx8754
  • 52,746
  • 12
  • 114
  • 209
Richard J. Acton
  • 885
  • 4
  • 17
  • try `<< -EOF` (with a dash) to keep the indentation – Aserre Jan 24 '19 at 14:32
  • I did try that: `read -r -d'' EXP <<- EOS` and I get this error: `ERROR: option '-e' requires a non-empty argument` – Richard J. Acton Jan 24 '19 at 14:36
  • Try `echo "$EXP"` before calling `R`. If the result shown looks like what you expect, then maybe additional flags are required for `R` to read multiline input – Aserre Jan 24 '19 at 14:46
  • Save the script as file then call `Rscript myScript.R`, [related post](https://stackoverflow.com/questions/18306362/run-r-script-from-command-line)? – zx8754 Jan 24 '19 at 14:54
  • hmm the echo is what I expect but i'm still getting the `ARGUMENT '~+~~+~'MyDocument.Rmd',' __ignored__` errors I think it must be something to do with the way R parses the contents of `-e''` – Richard J. Acton Jan 24 '19 at 14:55
  • Thanks but i'm trying to avoid the `Rscript myScript.R` approach @zx8754 as then I end up with 2 throw-away scripts to write when I submit a job to the cluster. – Richard J. Acton Jan 24 '19 at 14:58
  • Use `--vanilla` flag? [see this post](https://stackoverflow.com/questions/21733137) – zx8754 Jan 24 '19 at 15:04
  • nope same result with `R --vanilla` / `Rscript --vanilla` as in that post which might be useful though... – Richard J. Acton Jan 24 '19 at 15:09
  • using multiple `-e` flags as in that post only appears to work if each is a complete expression that could be terminated with an `;` or newline – Richard J. Acton Jan 24 '19 at 15:14
  • Do you mean `R --vanilla` doesn't work with codes with indentation? – zx8754 Jan 24 '19 at 15:14
  • yes adding `--vanilla` works fine in the one line versions and it does not change the errors in when the command is split across lines. which is consistent with what I understand `--vanilla` mode is supposed to do, I don't see any reason why it would alter the parsing behaviour of arguments to `-e` – Richard J. Acton Jan 24 '19 at 15:18
  • I tested with `R --no-save < – zx8754 Jan 24 '19 at 15:20
  • interesting could you share your exact syntax? - i'm using R 3.4.3 – Richard J. Acton Jan 24 '19 at 15:22

1 Answers1

3

This works for me (R version 3.5.0):

R --no-save <<code
for(i in 1:3) {
  i + 
    2
}
print(i)

runif(5,
      1,10)
code

Note: line-breaks and paddings are intentional.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    Note this approach bypasses the use of the `-e` flag entirely and makes use of the same behaviour as `echo 'print("Hello World")' | R --no-save`. (Thanks @zx8754) – Richard J. Acton Jan 24 '19 at 15:40