0

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.

Nini
  • 23
  • 1
  • 6
  • 1
    you likely need `readLines` but your question is so vague it's hard to help you without [an example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – rawr Aug 15 '20 at 19:10
  • I edited the question and modified the example. Please see if it is clear to you – Nini Aug 15 '20 at 19:47
  • Text files do not have multiple font sizes and bold face text. You must have some other kind of file, Word, html, pdf etc.`readLines` will read each line of a text file and then you have to decide how to parse it. – dcarlson Aug 15 '20 at 19:57
  • yep, you are correct. Let's say the file is a Word file. I corrected my question – Nini Aug 15 '20 at 20:00

1 Answers1

1

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")
JBGruber
  • 11,727
  • 1
  • 23
  • 45
  • Thanks for your comments. The version of R-studio that I am using is 3.6.1 and it says the officer and readtext are for older versions. Do you know other libraries? – Nini Aug 15 '20 at 23:13
  • 1
    You have that backwards. The current version of R is 4.0.2 and those packages work fine with the current version. You should update R to the current version. The current version of R-Studio (an "integrated development environment", IDE, for R) is 1.3.1056. – dcarlson Aug 16 '20 at 01:38
  • Both packages are available for R 3.6.1. You must have a different problem while installing them. I would recommend you do a little research and post a new question if you still can't figure out how to install the packages. – JBGruber Aug 16 '20 at 07:01