Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 209786

How to convert a Markdown file into an R DataFrame

$
0
0

I have the following markdown file (md3.md) and I want to convert it into an R dataframe using an XML conversion as an intermediary step. It is possible then to use XML R package to convert the XML file into a dataframe.

# level_1
## level_11
- ind1
- ind2
## level_12
- ind3
# level_2
## level_21
### level_211
- ind4

To convert md file to XML I used:

library(commonmark)
library(xml2)

md <- readLines("md3.md")
xml <- markdown_xml(md)
write(xml, "md3.xml")

but the resulting file is too complex and I don't know how to convert it into a dataframe. I tried to use the R XML package similar to the following but I am not sure how to express the XML file to be converted in the right way:

library(XML)
library(dplyr)

xml_doc <- readLines("md3.xml")

myXML <- xmlParse(xml_doc)
myData <- xmlToDataFrame(myXML, stringsAsFactors = FALSE,) %>%
  mutate_all(~type.convert(., as.is = T))

Viewing all articles
Browse latest Browse all 209786

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>