Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call external R script from R markdown (.Rmd) in RStudio?

It's fairly trivial to load external R scripts as per this R Sweave example:

<<external-code, cache=FALSE>>=
read_chunk('foo-bar.R')
@

Can the same be done for R Markdown?

like image 821
opyate Avatar asked Feb 10 '13 09:02

opyate


People also ask

Can you convert R script to R Markdown?

In fact, you can take any R script and compile it into a report that includes commentary, source code, and script output. Reports can be compiled to any output format including HTML, PDF, MS Word, and Markdown.

What is the difference between R script and R Markdown?

To put it simply - R is the actual programming language, RStudio is a convenient interface in which to use it, and R Markdown is a specific type of file format designed to produce documents that include both code and text.

How do I run an R script from another R script?

You can execute R script as you would normally do by using the Windows command line. If your R version is different, then change the path to Rscript.exe. Use double quotes if the file path contains space.

How do you call a Markdown in R?

You can call External r markdown page from r script using render function. Syntax: rmarkdown::render(rmd_file, output_type, output_file_name, knit options,....)


1 Answers

Yes.

Put this at the top of your R Markdown file:

```{r setup, echo=FALSE}
opts_chunk$set(echo = FALSE, cache=FALSE)
read_chunk('../src/your_code.R')
```

Delimit your code with the following hints for knitr (just like @yihui does in the example):

## @knitr part1
plot(c(1,2,3),c(1,2,3))

## @knitr part2
plot(c(1,2,3),c(1,2,3))

In your R Markdown file, you can now have the snippets evaluated in-line:

Title
=====

Foo bar baz...

```{r part1}
```

More foo...

```{r part2}
```
like image 189
opyate Avatar answered Sep 27 '22 23:09

opyate