如何从R标记文档的文件中读取标记代码

如何从R标记文档的文件中读取标记代码,r,shiny,markdown,r-markdown,R,Shiny,Markdown,R Markdown,我正在用Shiny的R markdown写一个应用程序。在一个“探索”页面中,我会展示一些互动情节,在另一个“关于”页面中,我会写一些应用程序及其用法的描述 我希望这一部分也是GitLab存储库中的自述文件,它将被提交到GitLab存储库中,因此它需要降价。我想知道是否有一种方法可以从R markdown文档调用自述文件,因此我不需要在两个文件中维护相同的文本 让我们考虑下面的例子。 R降价文件 --- title: "Example" runtime: shiny vertical_layou

我正在用Shiny的R markdown写一个应用程序。在一个“探索”页面中,我会展示一些互动情节,在另一个“关于”页面中,我会写一些应用程序及其用法的描述

我希望这一部分也是GitLab存储库中的自述文件,它将被提交到GitLab存储库中,因此它需要降价。我想知道是否有一种方法可以从R markdown文档调用自述文件,因此我不需要在两个文件中维护相同的文本

让我们考虑下面的例子。

R降价文件

---
title: "Example"
runtime: shiny
vertical_layout: fill
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

```{r setup, include=FALSE}
library(shiny)
library(tidyverse)
```


Explore
======================================================================

### PLOT 1
```{r}
mtcars %>% ggplot(aes(x=mpg, y=cyl)) + geom_point()
```

### PLOT 2
```{r}
mtcars %>% ggplot(aes(x=mpg, y=qsec)) + geom_point()
```

About
============================================================================

#### README

A brief description of the document.
#### README

A brief description of the document.
---
title: "Example"
runtime: shiny
vertical_layout: fill
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

```{r setup, include=FALSE}
library(shiny)
library(tidyverse)

pasteReadme <- function(fileName){

  breakFun <- function(x){
    #function to replace empty lines with newline. 
    if(nchar(x) == 0){
      return("\n\n") #double newline to give same space as in the .md-file
    } else {
      return(x)
    }
  }

  storeLines <- readLines(fileName)

  cat(paste0(lapply(storeLines, FUN=function(x) breakFun(x)), collapse=""))

}

```


Explore
======================================================================

### PLOT 1
```{r}
mtcars %>% ggplot(aes(x=mpg, y=cyl)) + geom_point()
```

### PLOT 2
```{r}
mtcars %>% ggplot(aes(x=mpg, y=qsec)) + geom_point()
```

About
============================================================================


```{r, results='asis'}
pasteReadme("about.md")
```
我特别想问的是,“About”下面的两行文本是否可以被读取标记文件的函数所替代。因此,以下文件可以用作应用程序中的自述文件和关于部分

降价文件

---
title: "Example"
runtime: shiny
vertical_layout: fill
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

```{r setup, include=FALSE}
library(shiny)
library(tidyverse)
```


Explore
======================================================================

### PLOT 1
```{r}
mtcars %>% ggplot(aes(x=mpg, y=cyl)) + geom_point()
```

### PLOT 2
```{r}
mtcars %>% ggplot(aes(x=mpg, y=qsec)) + geom_point()
```

About
============================================================================

#### README

A brief description of the document.
#### README

A brief description of the document.
---
title: "Example"
runtime: shiny
vertical_layout: fill
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

```{r setup, include=FALSE}
library(shiny)
library(tidyverse)

pasteReadme <- function(fileName){

  breakFun <- function(x){
    #function to replace empty lines with newline. 
    if(nchar(x) == 0){
      return("\n\n") #double newline to give same space as in the .md-file
    } else {
      return(x)
    }
  }

  storeLines <- readLines(fileName)

  cat(paste0(lapply(storeLines, FUN=function(x) breakFun(x)), collapse=""))

}

```


Explore
======================================================================

### PLOT 1
```{r}
mtcars %>% ggplot(aes(x=mpg, y=cyl)) + geom_point()
```

### PLOT 2
```{r}
mtcars %>% ggplot(aes(x=mpg, y=qsec)) + geom_point()
```

About
============================================================================


```{r, results='asis'}
pasteReadme("about.md")
```

建议使用
读线

定义一个函数,使用
readLines()
读取.md文件,然后用换行符替换空行,
\n
,最后使用
cat
连接并打印

pasteReadme <- function(fileName){

  breakFun <- function(x){
    #function to replace empty lines with newline. 
    if(nchar(x) == 0){
      return("\n\n") #double newline to give same space as in the .md-file
    } else {
      return(x)
    }
  }

  storeLines <- readLines(fileName)

  cat(paste0(lapply(storeLines, FUN=function(x) breakFun(x)), collapse=""))

}
.Rmd文件

---
title: "Example"
runtime: shiny
vertical_layout: fill
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

```{r setup, include=FALSE}
library(shiny)
library(tidyverse)
```


Explore
======================================================================

### PLOT 1
```{r}
mtcars %>% ggplot(aes(x=mpg, y=cyl)) + geom_point()
```

### PLOT 2
```{r}
mtcars %>% ggplot(aes(x=mpg, y=qsec)) + geom_point()
```

About
============================================================================

#### README

A brief description of the document.
#### README

A brief description of the document.
---
title: "Example"
runtime: shiny
vertical_layout: fill
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

```{r setup, include=FALSE}
library(shiny)
library(tidyverse)

pasteReadme <- function(fileName){

  breakFun <- function(x){
    #function to replace empty lines with newline. 
    if(nchar(x) == 0){
      return("\n\n") #double newline to give same space as in the .md-file
    } else {
      return(x)
    }
  }

  storeLines <- readLines(fileName)

  cat(paste0(lapply(storeLines, FUN=function(x) breakFun(x)), collapse=""))

}

```


Explore
======================================================================

### PLOT 1
```{r}
mtcars %>% ggplot(aes(x=mpg, y=cyl)) + geom_point()
```

### PLOT 2
```{r}
mtcars %>% ggplot(aes(x=mpg, y=qsec)) + geom_point()
```

About
============================================================================


```{r, results='asis'}
pasteReadme("about.md")
```
---
标题:“示例”
运行时间:闪亮
垂直布局:填充
输出:
flexdashboard::flex_仪表板:
方向:行
---
```{r设置,include=FALSE}
图书馆(闪亮)
图书馆(tidyverse)
粘贴自述