Jeremias Bohn
Jeremias Bohn

Reputation: 85

Running a command line command with PHP

So my problem is the following: I have an R markdown file on my Ubuntu 18.04.3 server that I want to be knitted via command line (this works fine with Rscript -e "rmarkdown::render('path/to/file.Rmd')"), so I tried to run this from PHP with the exec() command (obviously using escape characters). The output from PHP is the following: Execution haltedArray ( [0] => Error: unexpected input in "rmarkdown::render(\" [1] => Execution halted ) when running the following lines of PHP:

exec("Rscript -e \"rmarkdown::render(\'path/to/file.Rmd\')\" 2>&1", $output);
print_r($output); 

What is the unexpected input?

Upvotes: 2

Views: 83

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

Don't escape the inner ''s

exec("Rscript -e \"rmarkdown::render('path/to/file.Rmd')\" 2>&1", $output);

.. or change the outermost string characters to use single quotes instead of double:

exec('Rscript -e "rmarkdown::render(\'path/to/file.Rmd\')" 2>&1', $output);

.. dont escape both unless you use both, which is ugly and confusing

exec("Rscript -e \"rmarkdown::render(\\\"path/to/file.Rmd\\\")\" 2>&1", $output);

Upvotes: 2

Related Questions