是否可以从RStudio中的R markdown(.Rmd)调用外部R脚本?

时间:2022-01-07 01:22:53

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

按照这个R Sweave示例加载外部R脚本是相当简单的:

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

Can the same be done for R Markdown?

R Markdown可以做同样的事情吗?

1 个解决方案

#1


14  

Yes.

是。

Put this at the top of your R Markdown file:

把它放在你的R Markdown文件的顶部:

```{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的以下提示来划分您的代码(就像示例中的@yihui一样):

## @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:

在您的R Markdown文件中,您现在可以在线评估片段:

Title
=====

Foo bar baz...

```{r part1}
```

More foo...

```{r part2}
```

#1


14  

Yes.

是。

Put this at the top of your R Markdown file:

把它放在你的R Markdown文件的顶部:

```{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的以下提示来划分您的代码(就像示例中的@yihui一样):

## @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:

在您的R Markdown文件中,您现在可以在线评估片段:

Title
=====

Foo bar baz...

```{r part1}
```

More foo...

```{r part2}
```