How to Read and Create Word Documents in R | IBKR Quant Blog (2024)

Reading and creating word documents in R

In this post we’ll talk about how to use R to read and create word files. We’ll primarily be using R’sofficerpackage.For reading data from Word Documents with Python, click here.

Creating Word reports with the officer package

The first thing we need to do is to install theofficerpackage.

install.packages("officer")

We’ll also be using thedplyrpackage, so you’ll need to install that the same way if you don’t have it already. Next, let’s load each of these packages.

library(officer)library(dplyr)

Now, we’ll get started creating a report! First, we will use theread_docxfunction to create an empty Word file.

# create empty Word filesample_doc <- read_docx()

Adding paragraphs

Next, let’s add a few sample paragraphs. We can do that using thebody_add_parfunction like below. The syntax is similar to that of thetidyverse.

sample_doc <- sample_doc %>% body_add_par("This is the first paragraph") sample_doc <- sample_doc %>% body_add_par("This is the second paragraph")sample_doc <- sample_doc %>% body_add_par("This is the third paragraph")

Now, we can add a table to our document using thebody_add_tablefunction. Before we do that, we just need to have a data frame ready, so we’ll create a sample one like below.

# create sample data framedf <- data.frame(a = 1:10, b = 11:20, c= 21:30) # add table containing the data frame's contentssample_doc <- sample_doc %>% body_add_table(df, style = "table_template")

Adding images to the document

We can also add images to our Word Document. This is done by creating a temp file with an R plot and then adding the image to our document object. Though we’re using base R for plotting here,ggplotcould also be used.

set.seed(0) # create a temp filesrc <- tempfile(fileext = ".png") # create PNG objectpng(filename = src, width = 4, height = 4, units = 'in', res = 400) # create plotplot(sample(100, 10)) # save PNG filedev.off() # add PNG image to Word documentsample_doc <- sample_doc %>% body_add_img(src = src, width = 4, height = 4, style = "centered")

Lastly, we can save our Word Document usingprint.

print(sample_doc, target = "sample_file.docx")

How to modify existing Word Documents

To modify existing Word Documents, all we need to change is to input the filename intoread_docx. Then, we can continue modifying our Word Document object like we were previously.

sample_doc <- read_docx("sample_file.docx") # add another paragraphsample_doc <- sample_doc %>% body_add_par("This is another paragraph")

How to read Word Documents with R

What if we want to read in the Word Document we just created? We can do that using the sameread_docxfunction like we did above to modify an existing file. Secondly, we use thedocx_summarywith this object to get the content within the file.

sample_data <- read_docx("sample_file.docx") content <- docx_summary(sample_data)
How to Read and Create Word Documents in R | IBKR Quant Blog (1)

docx_summaryreturns a dataframe with the content in the Word file, as can be seen above. For example, to get the text in the paragraph of the document, we just need to filter the content_type field on “paragraph”, like below:

paragraphs <- content %>% filter(content_type == "paragraph")paragraphs$text
How to Read and Create Word Documents in R | IBKR Quant Blog (2)

Extracting tables from Word Documents

Now, let’s extract the table from our document. We can do this similarly to the above in that we just need to filter content_type for “table cell”:

content %>% filter(content_type == "table cell")
How to Read and Create Word Documents in R | IBKR Quant Blog (3)

As you can see, the table’s columns are stacked in a single column. We need to do a little transformation to get this result into the needed format.

table_cells <- content %>% filter(content_type == "table cell")table_data <- table_cells %>% filter(!is_header) %>% select(row_id, cell_id, text) # split data into individual columnssplits <- split(table_data, table_data$cell_id)splits <- lapply(splits, function(x) x$text) # combine columns back together in wide formattable_result <- bind_cols(splits) # get table headerscols <- table_cells %>% filter(is_header)names(table_result) <- cols$text
How to Read and Create Word Documents in R | IBKR Quant Blog (4)

Conclusion

officercan also be used to interact with PowerPoint files, which we’ll cover in a future post. That’s all for now!Click here to follow my blog Twitter and get notified of new posts!For more onofficer, check outthis link.

Visit TheAutomatic.net for additional insight on this topic.

Related Tags

Data Science R rstats RStudio

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circ*mstances and, as necessary, seek professional advice.

How to Read and Create Word Documents in R | IBKR Quant Blog (2024)

FAQs

Can R read Word files? ›

R is also capable of handling more challenging situations, such as working with hundreds or thousands of data files, reading tabular data from Microsoft Word documents or PDFs, and reading in data from unstructured plain text files.

How do I open a Word document in read mode? ›

If you're reading a document, not writing or major editing, click or tap View > Read Mode to hide the writing tools and menus and to leave more room for the pages themselves. Read Mode automatically fits the page layout to your device, using columns and larger font sizes, both of which you can adjust.

How can I read Word documents online? ›

How To View Word File Online
  1. Upload your Word file from the indicated area at the top of the page. ...
  2. Wait a brief moment for the file to be uploaded and processed on our secure servers.
  3. View the file on any device and in any resolution within our responsive, intuitive file viewer.

How do I create a Word document in R? ›

Create a simple Word document
  1. addTitle: Add a title.
  2. addParagraph: Add paragraphs of text.
  3. addFlexTable: Add a table.
  4. addPlot: Add a plot generated in R.
  5. addImage: Add external images.
  6. addTOC: Add a table of contents.
  7. addRScript: highlight and add R code.
  8. addMarkdown: Add markdown.

How do I convert an R file to Word? ›

First step: open a new R Markdown file which you will paste the code into. For the output format, select Word. Second step: paste the code into a block with chunk options eval=FALSE, echo=TRUE . Third step: compile to Word.

How to create a Word document? ›

Create a document
  1. Open Word. Or, if Word is already open, select File > New.
  2. In the Search for online templates box, enter a search word like letter, resume, or invoice. Or, select a category under the search box like Business, Personal, or Education.
  3. Click a template to see a preview. ...
  4. Select Create.

How to read Word documents without Microsoft Office? ›

The best way to view a Word document if you do not have Microsoft Word installed on your computer is to open the . docx file from Google Drive. This will ensure all comments and edits from your editor will be visible to you.

Can I open a Word document as read-only? ›

Select File > Info > Protect Document. 2. Select Always open Read-Only.

How do I get my Word document to read? ›

Read Aloud is only available for Office 2019, Office 2021, and Microsoft 365.
  1. On the Review tab, select Read Aloud.
  2. To play Read Aloud, select Play in in the controls.
  3. To pause Read Aloud, select Pause.
  4. To move from one paragraph to another, select Previous or Next.
  5. To exit Read Aloud, select Stop (x).

What app can i use to read Word document? ›

Doc Opener lets you read, print, and edit Doc files created by Microsoft Word and other office applications. It also includes a built-in PDF viewer for quick printing and sharing of PDF files. This lightweight app is built for speed - it opens your docs quickly and gets out of your way.

What program opens doc files? ›

Microsoft Word, supported by Windows and macOS, is the primary programme for opening DOC files. To open a . DOC file in Word: Launch Microsoft Word.

How do I open a Word document in my computer? ›

Open File Explorer, and click Documents. A list of documents appears. If the document you want to work on is on the list, click the file name to open the document. If the document isn't on the list, navigate to the location where you stored your file, and double-click the file.

How do I create a text File in R? ›

How to write a text file through R?
  1. Step 1 -Creating a 4*5 matrix in a vector m. m <- matrix(c(1:10), nrow = 4, ncol = 5, byrow = TRUE) print(m)
  2. Step 2 -Create a dataframe for the matrix. df <- data.frame(m)
  3. Step 3 -Write a text file.
Feb 24, 2021

How do I create a Word cloud in RStudio? ›

3. Main steps to create Word Cloud in R
  1. Step 1: Starting by creating a text file. ...
  2. Step 2: Install and loading the WordCloud package in R. ...
  3. Step 3: Text Mining in R: Cleaning the data. ...
  4. Step 4: Creating a document-term-matrix. ...
  5. Step 5: Generating the Word Cloud.

How to convert Word to PDF in R? ›

file(package = "doconv", "doc-examples/example. docx") out <- docx2pdf(input = file, output = tempfile(fileext = ". pdf")) if (file. exists(out)) { message(basename(out), " is existing now.") } }

What type of files can R read? ›

R is capable of reading data from most formats, including files created in other statistical packages. Whether the data was prepared using Excel (in CSV, XLSX, or TXT format), SAS, Stata, SPSS, or others, R can read and load the data into memory.

How do I open Word in Windows R? ›

Press the Windows key and "R" on the keyboard at the same time to bring up the Run dialogue. Type "WinWord.exe" without quotation marks into the box.

Can you read txt files in R? ›

R programming language can load TXT files. If you are wondering how to read TXT files in R, the most basic function you can use is the read. table function.

Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 6438

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.