我正在用Shiny用R markdown编写应用程序。在一个页面“ Explore”中,我将显示一些交互式图表,在另一个“ About”中,我将对该应用程序及其使用情况进行一些描述。
我希望此部分也成为要提交的GitLab存储库中的README文件,因此它必须处于markdown状态。我想知道是否有一种方法可以从R markdown文档中调用该README文件,因此我不需要在两个文件中维护相同的文本。
让我们考虑以下示例。
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.
我要特别询问的是,“关于”下的两个文本行是否可以由读取markdown文件的某些函数代替。因此以下文件可用作自述文件和应用程序中的about部分。
降价文件
#### README
A brief description of the document.
答案 0 :(得分:1)
使用readLines
的建议。
定义一个函数以使用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=""))
}
使用块选项results = 'asis'
以.Rmd输出。已通过图片(即![](http://example.com/logo.png)
)和常规HTML代码进行了测试。应该适用于大多数情况。参见下面的示例。
about.md
<!-- #### README -->
<!-- A brief description of the document. -->
#### README
A brief description of the document.
This is some more about text
##### This logo is important for the project
![optional caption text](https://www.rstudio.com/wp-content/uploads/2016/09/RStudio-Logo-Blue-Gray-125.png)
Works with **bold** and *italics*
###### Another Header
<span style="color:blue">text is blue</span>
.md文件
---
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")
```