rknitr

How to use knitr from command line with Rscript and command line argument?


I have an R code my_code.R which takes in an argument a file test.txt. I can use:

   Rscript -e my_code.R test.txt 

and run the script, but i want to use stitch() from knitR to generate the report of the script in pdf/tex.

I have trolled around stack overflow and used following suggestions, but didn't get any results:

   Rscript -e "library(knitr);knit('my_code.R "-args arg1=test.txt" ')"
   Rscript -e "knitr::stitch('my_code.R "-args arg1=test.txt"')"

Here is another similar discussion on what i want (link), but with option for adding argument(s).


Solution

  • I do not see why this is not possible. Here is my_code.R:

    commandArgs(TRUE)
    

    And I simply run

    Rscript -e "library(knitr); stitch('my_code.R')" --args foo bar whatever=blabla
    

    I get the output

    knitr stitch() output

    It seems you did not use double quotes correctly in your original attempt. It should be

    Rscript -e "library(knitr); stitch('my_code.R')" --args arg1=test.txt