I want to run a Rmd script from command line using something like this:
Rscript script.Rmd
But this throws an error because the 'Rscript' command does not understand the R markdown syntax. I don't want to 'render' the R markdown. What I want is just to be able to run only the code chunks included in the Rmd file from the command line. Is there any way to do this without copy-pasting all the code chunks into a new separate script file?
rmarkdown is designed to render a Rmd file's texts and executing its code both into a single output stream. AFAIK, there's no way within the package to render only the chunk output.
If you are on a *nix there's a scripting way of doing this that I can outline. I haven't had to use windows long enough that I have no idea whether the same ideas apply.
Given script.Rmd, write a sed command to extract the chunks, stripping them of their triple backticks , | pipe it to the littler pipe-friendly R binary, see the results on stdout, tee or pipe to file.
I don't think you need to go this far
There is already a function in knitr to extract all the R code from a Rmd file. See ?knitr::purl. It uses the tangle = TRUE in knitr.
Here is an example of how to generate a R script from Rmd's chunks.