簡體   English   中英

如何在 rmarkdown 代碼塊中解析文本表

[英]How to parse a text table in rmarkdown code chunks

有一個帶有markdown表的rmarkdown文件,該表將定期更新。 內容應該在代碼塊中進行解析,以便可以使用例如ggplot 我不想在代碼塊或單獨的文件中維護表。

如何從代碼塊中讀取表格?

您可以在下面的markdown表中找到入門rmarkdown代碼。

---
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
```

在此處輸入圖像描述

在代碼塊中,將readLines應用於您的Rmd文件以在向量中獲取此文件的行:

allLines <- readLines("yourFile.Rmd")

Select 以|開頭和結尾的行 ,並刪除第二個(即分隔線"|-----|-----|" ):

tableLines <- allLines[grep("^\\|.*\\|$", allLines)][-2]

然后使用下面的代碼,您將表格作為矩陣,其第一行包含列名:

tableAsMatrix <- t(sapply(strsplit(tableLines, "\\|"), function(pieces){
  stringr::str_trim(pieces[-1])
}))

最后將該矩陣去掉第一行轉換為 dataframe,並使用其第一行設置列名:

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
```

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM