Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call RMarkdown on command line using a.R that is passed a file

In summary, I am using my script 'Graphs.R' on 'input_file1.txt' in RStudio to create a Rmd which I then knit to html. I would like to automate this process to run more files on the command line.

So far, I can get the Rscript to run on the command line using:

Rscript Graphs.R input_file1.txt

I also know that I can create an .RMD file using:

Rscript -e rmarkdown::render(Graphs.R)

However, I would like to do the following:

Rscript -e rmarkdown::render('Graphs.R input_file1.txt', 'output_file.Rmd')

are there any ideas as how to do this?

like image 997
user1234579 Avatar asked Feb 13 '15 19:02

user1234579


People also ask

How do I import a file into R Markdown?

You can click in the upper left menu File > Import Dataset > From Excel and select the file to import it. Then you can copy the code that appears in the R console with the code required for import the data in xlsx and then copy it in a R Markdown code chunk.

How do I run an RMD file in R?

To open a new file, click File > New File > R Markdown in the RStudio menu bar. A window will pop up that helps you build the YAML frontmatter for the . Rmd file. Use the radio buttons to select the specific type of output that you wish to build.

How do I run an R script in terminal?

If R has been installed properly, simply entering R on the command line of a terminal should start the program. In Windows, the program is typically specified as the action performed when clicking on an icon.


1 Answers

It's not entirely clear what you are trying to do. It seems like you have a text file which has to be converted to an Rmd by an R script (why isn't it just an Rmd to begin with?) and then you want to render the Rmd. You can do this by running these commands in your terminal:

Rscript Graphs.R
Rscript -e "rmarkdown::render('output_file.Rmd')"

The first command runs the Graphs.R file which presumably generates output_file.Rmd. The second command runs a one-liner which knits output_file.Rmd into output_file.html.

If you want to read command line arguments in an R file, try ?commandArgs.

args <- commandArgs(trailingOnly = TRUE)

Also see this Stack Overflow question.

like image 106
Lincoln Mullen Avatar answered Oct 20 '22 01:10

Lincoln Mullen