Nini
Nini

Reputation: 23

Need help to import a Word file into R with references and headings

I have a text file that has a paragraph with references. I am having trouble importing it into R Markdown. Do you have any suggestions?

For example, a word file looks like this:

Title: sample word file to import to R Markdown

Keywords: help, R Markdown, word file

I- Introduction

Overview of the study:

This document provides an simple example to be imported to R Markdwon. There are many import functions such as read.csv and etc (ref. 1).

II- Body

It is very important to import a word file into R (ref. 2).

III- Conlusion

I think there must be a solution.

IV- References

1- https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/read.table

2- https://stat.ethz.ch/R-manual/R-devel/library/utils/html/read.table.html

I have used the read_file, read_tsv, and readtext functions to import this file but it messes up the whole structure and sentences.

Upvotes: 0

Views: 979

Answers (1)

JBGruber
JBGruber

Reputation: 12440

If you simply want to read the DOCX file into individual lines in R, you can do so with readtext (which produces a data.frame) and then split the text into lines:

x <- readtext::readtext("https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.docx")
y <- strsplit(x$text, "\n")[[1]]
y[1:10]
#>  [1] "Lorem ipsum "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
#>  [2] "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ac faucibus odio. "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
#>  [3] "Vestibulum neque massa, scelerisque sit amet ligula eu, congue molestie mi. Praesent ut varius sem. Nullam at porttitor arcu, nec lacinia nisi. Ut ac dolor vitae odio interdum condimentum. Vivamus dapibus sodales ex, vitae malesuada ipsum cursus convallis. Maecenas sed egestas nulla, ac condimentum orci. Mauris diam felis, vulputate ac suscipit et, iaculis non est. Curabitur semper arcu ac ligula semper, nec luctus nisl blandit. Integer lacinia ante ac libero lobortis imperdiet. Nullam mollis convallis ipsum, ac accumsan nunc vehicula vitae. Nulla eget justo in felis tristique fringilla. Morbi sit amet tortor quis risus auctor condimentum. Morbi in ullamcorper elit. Nulla iaculis tellus sit amet mauris tempus fringilla."
#>  [4] "Maecenas mauris lectus, lobortis et purus mattis, blandit dictum tellus."                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
#>  [5] "Maecenas non lorem quis tellus placerat varius. "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
#>  [6] "Nulla facilisi. "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
#>  [7] "Aenean congue fringilla justo ut aliquam. "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#>  [8] "Mauris id ex erat. Nunc vulputate neque vitae justo facilisis, non condimentum ante sagittis. "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
#>  [9] "Morbi viverra semper lorem nec molestie. "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
#> [10] "Maecenas tincidunt est efficitur ligula euismod, sit amet ornare est vulputate."

If you are also intrested in the formatting, you can use officer:

library(officer)
download.file("https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.docx", "in.docx")

doc <- read_docx("in.docx")
content <- docx_summary(doc)
head(tibble::as_tibble(content)) #convert to tibble for nicer printing
#> # A tibble: 6 x 11
#>   doc_index content_type style_name text  level num_id row_id is_header cell_id
#>       <int> <chr>        <chr>      <chr> <dbl>  <int>  <int> <lgl>       <dbl>
#> 1         1 paragraph    Title      "Lor…    NA     NA     NA NA             NA
#> 2         2 paragraph    Text Body  ""       NA     NA     NA NA             NA
#> 3         3 paragraph    Heading 1  "Lor…     1      1     NA NA             NA
#> 4         4 paragraph    Text Body  ""       NA     NA     NA NA             NA
#> 5         5 paragraph    Text Body  "Ves…    NA     NA     NA NA             NA
#> 6         6 paragraph    Text Body  "Mae…    NA     NA     NA NA             NA
#> # … with 2 more variables: col_span <dbl>, row_span <dbl>

If you want to convert your word file to R Markdown, you can use pandoc:

# In your Terminal
pandoc -s in.docx --wrap=none --reference-links -t markdown -o out.Rmd

# Or from R:
system("pandoc -s in.docx --wrap=none --reference-links -t markdown -o out.Rmd")

Upvotes: 1

Related Questions