Markdown表到R中的数据框

Tar*_*aas 3 markdown r r-markdown readr

有很多方法可以将数据帧转换为Markdown表。但是在给定Markdown表的情况下,如何将其转换回数据框

给定一个表格表格:

Table Header | Second Header
------------- | -------------
Table Cell | Cell 2
Cell 3 | Cell 4 
Run Code Online (Sandbox Code Playgroud)

或者,甚至更糟的是,

Table Header | Second Header \n------------- | ------------- \nTable Cell | Cell 2 \nCell 3 | Cell 4 
Run Code Online (Sandbox Code Playgroud)

如何将其放入数据帧?

ali*_*ire 8

我编写了几个函数来处理这些问题,尽管我怀疑这对于编写 SO 答案的人来说比其他任何人都更像是一个问题。不管:

# base R version
read.markdown <- function(file, stringsAsFactors = FALSE, strip.white = TRUE, ...){
    if (length(file) > 1) {
        lines <- file
    } else if (grepl('\n', file)) {
        con <- textConnection(file)
        lines <- readLines(con)
        close(con)
    } else {
        lines <- readLines(file)
    }
    lines <- lines[!grepl('^[[:blank:]+-=:_|]*$', lines)]
    lines <- gsub('(^\\s*?\\|)|(\\|\\s*?$)', '', lines)
    read.delim(text = paste(lines, collapse = '\n'), sep = '|', 
               stringsAsFactors = stringsAsFactors, strip.white = strip.white, ...)
}

# readr version
read_markdown <- function(file, trim_ws = TRUE, ...){
    if (length(file) > 1) {
        lines <- file
    } else {
        lines <- readr::read_lines(file)
    }
    lines <- lines[!grepl('^[[:blank:]+-=:_|]*$', lines)]
    lines <- gsub('(^\\s*?\\|)|(\\|\\s*?$)', '', lines)
    readr::read_delim(paste(lines, collapse = '\n'), delim = '|', 
                      trim_ws = trim_ws, ...)
}
Run Code Online (Sandbox Code Playgroud)

他们可以处理 Markdown 表的许多变体,并且对单个字符串感到满意:

read.markdown('Table Header | Second Header \n------------- | ------------- \nTable Cell | Cell 2 \nCell 3 | Cell 4 ')
#>   Table.Header Second.Header
#> 1   Table Cell        Cell 2
#> 2       Cell 3        Cell 4
Run Code Online (Sandbox Code Playgroud)

一个线向量(就像一个从 得到clipr::read_clip):

clipr::write_clip(
' |                     |  mpg  |  cyl  |  disp  |  hp  |
 |:-------------------:|:-----:|:-----:|:------:|:----:|
 |      Mazda RX4      |  21   |   6   |  160   | 110  |
 |    Mazda RX4 Wag    |  21   |   6   |  160   | 110  |
 |     Datsun 710      | 22.8  |   4   |  108   |  93  |'
)

read.markdown(clipr::read_clip())
#>               X  mpg cyl disp  hp
#> 1     Mazda RX4 21.0   6  160 110
#> 2 Mazda RX4 Wag 21.0   6  160 110
#> 3    Datsun 710 22.8   4  108  93
Run Code Online (Sandbox Code Playgroud)

或文件名(尽管文件只能包含表和空格):

tmp <- tempfile()
writeLines(
' +---------------------+-------+-------+--------+------+--------+
 |                     |  mpg  |  cyl  |  disp  |  hp  |  drat  |
 +=====================+=======+=======+========+======+========+
 |      Mazda RX4      |  21   |   6   |  160   | 110  |  3.9   |
 +---------------------+-------+-------+--------+------+--------+
 |    Mazda RX4 Wag    |  21   |   6   |  160   | 110  |  3.9   |
 +---------------------+-------+-------+--------+------+--------+
 |     Datsun 710      | 22.8  |   4   |  108   |  93  |  3.85  |
 +---------------------+-------+-------+--------+------+--------+',
tmp
)

read_markdown(tmp)
#> Warning: Missing column names filled in: 'X1' [1]
#> # A tibble: 3 x 6
#>   X1              mpg   cyl  disp    hp  drat
#>   <chr>         <dbl> <int> <int> <int> <dbl>
#> 1 Mazda RX4      21.0     6   160   110  3.90
#> 2 Mazda RX4 Wag  21.0     6   160   110  3.90
#> 3 Datsun 710     22.8     4   108    93  3.85
Run Code Online (Sandbox Code Playgroud)

编辑:我已经将这些函数放在一个包中,如果有人觉得它们有用的话。


Tar*_*aas 5

我输入了问题,但随后意识到答案非常简单。功能read_delimreadr包处理这个问题很容易:

library(readr)
library(dplyr)

object <- 'Table Header | Second Header \n------------- | ------------- \nTable Cell | Cell 2 \nCell 3 | Cell 4'
data_frame <- read_delim(object, delim = '|')

# A tibble: 3 x 2
  `Table Header ` ` Second Header `
            <chr>             <chr>
1  -------------     ------------- 
2     Table Cell            Cell 2 
3         Cell 3             Cell 4
Run Code Online (Sandbox Code Playgroud)

一个人只需要过滤掉“ -------”行。等等。

希望此解决方案可以帮助某人。