如何在rmarkdown代码块中解析文本表
有一个如何在rmarkdown代码块中解析文本表,r,r-markdown,knitr,pandoc,R,R Markdown,Knitr,Pandoc,有一个rmarkdown文件,其中包含一个markdown表,该表将定期更新。应该在代码块中解析内容,以便可以使用例如ggplot。我不想在代码块或单独的文件中维护该表 如何从代码块中读取表 您可以在下表中找到作为初学者的r标记代码和标记 --- title: "Parse tables" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(message = FALSE,
rmarkdown
文件,其中包含一个markdown
表,该表将定期更新。应该在代码块中解析内容,以便可以使用例如ggplot
。我不想在代码块或单独的文件中维护该表
如何从代码块中读取表
您可以在下表中找到作为初学者的r标记
代码和标记
---
title: "Parse tables"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
```
# Step 1: Create markdown table as text
That table will be manually updated directly in the markdown file.
Table: Project Timeline
| date | description |
|------------|-------------|
| 2020-05-11 | Milestone 1 |
| 2020-07-11 | Milestone 2 |
| 2020-07-20 | Milestone 3 |
# Step 2: Parse the table above
The table should be maintained as a markdown table. That seems to be more easy than working directly with
`tibble` or `tribble`. How can I read the table from the code chunk?
```{r}
library(tidyverse)
df <- tibble(date = c("2020-05-11", "2020-07-11", "2020-07-20"),
description = c("Milestone 1", "Milestone 2", "Milestone 3"))
df
```
---
标题:“解析表”
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(message=FALSE,warning=FALSE)
```
#步骤1:将标记表创建为文本
该表将直接在标记文件中手动更新。
表:项目时间表
|日期|说明|
|------------|-------------|
|2020-05-11 |里程碑1|
|2020-07-11 |里程碑2|
|2020-07-20 |里程碑3|
#步骤2:解析上面的表
该表应作为降价表进行维护。这似乎比直接与客户合作更容易
`tibble`或tribble`。如何从代码块中读取表?
```{r}
图书馆(tidyverse)
df在代码块中,对Rmd
文件应用readLines
,以获得向量中此文件的行:
allLines <- readLines("yourFile.Rmd")
然后使用下面的代码,您可以将表作为矩阵,其第一行包含列名:
tableAsMatrix <- t(sapply(strsplit(tableLines, "\\|"), function(pieces){
stringr::str_trim(pieces[-1])
}))
setNames(as.data.frame(tableAsMatrix[-1,,drop = FALSE]), tableAsMatrix[1,])
完整代码
---
标题:“解析表”
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(message=FALSE,warning=FALSE)
```
#步骤1:将标记表创建为文本
该表将直接在标记文件中手动更新。
表:项目时间表
|日期|说明|
|------------|-------------|
|2020-05-11 |里程碑1|
|2020-07-11 |里程碑2|
|2020-07-20 |里程碑3|
#步骤2:解析上面的表
该表应作为降价表进行维护。如何从代码块中读取表?
```{r}
我不明白你的问题。您的意思是要从步骤2中输出df
,使其看起来像步骤1中的表格吗?或者从步骤1中读取表,它不在代码块中,因此可以成为TIBLE?从步骤1中读取表,它不是代码块,因此可以成为TIBLE
。
setNames(as.data.frame(tableAsMatrix[-1,,drop = FALSE]), tableAsMatrix[1,])
---
title: "Parse tables"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
```
# Step 1: Create markdown table as text
That table will be manually updated directly in the markdown file.
Table: Project Timeline
| date | description |
|------------|-------------|
| 2020-05-11 | Milestone 1 |
| 2020-07-11 | Milestone 2 |
| 2020-07-20 | Milestone 3 |
# Step 2: Parse the table above
The table should be maintained as a markdown table. How can I read the table from the code chunk?
```{r}
allLines <- readLines("ParseTable.Rmd")
tableLines <- allLines[grep("^\\|.*\\|$", allLines)][-2]
tableAsMatrix <- t(sapply(strsplit(tableLines, "\\|"), function(pieces){
stringr::str_trim(pieces[-1])
}))
df <- setNames(as.data.frame(tableAsMatrix[-1,,drop = FALSE]), tableAsMatrix[1,])
df
```