Generating Word Reports with R Shiny using ReporteRs Package
Introduction
In this blog post, we will explore how to generate word reports with R Shiny using the ReporteRs package. We will start by understanding the basics of Shiny and ReporteRs, and then dive into the code to generate a word report.
What is Shiny?
Shiny is an open-source R package for creating web applications that can be used to visualize data and share insights with others. It provides a user-friendly interface for building interactive dashboards and reports.
What is ReporteRs Package?
ReporteRs is another popular R package that allows us to generate reports in various formats, including Word, PDF, and HTML. It integrates well with Shiny and can be used to create interactive reports.
Requirements
Before we start, you need to have the following packages installed:
shinyReporteRs
You can install these packages using the following commands in your R console:
install.packages("shiny")
install.packages("ReporteRs")
Understanding the Code
The code provided is a basic Shiny app that allows users to upload a CSV file and select several columns from it. The selected data is then displayed in two tables: one with flextables and another vanilla table.
However, instead of generating an HTML file, we want to generate a Word document (.docx) when the user clicks on the download button. To achieve this, we will use the downloadHandler function provided by Shiny.
output$downloadData <- downloadHandler(
filename=paste0("Data_", input$selectA, input$selectB, "_", Sys.Date(), ".docx"),
content = function(file) {
# Code to generate word report goes here
}
)
Generating Word Report using ReporteRs
To generate a Word report, we need to use the docx package provided by ReporteRs.
First, let’s install and load the necessary packages:
install.packages("docx")
library(docx)
library(ReporteRs)
Now, let’s create a word document using flextables and vanilla tables. We will add two tables to our Word report: one with flextables and another vanilla table.
# Create word report
doc <- docx()
data<- data_df()[1:10, 1:10]
# Add a first table : Default table
doc <- addTitle(doc, "Default table")
doc <- addFlexTable( doc, FlexTable(data))
doc <- addParagraph(doc, c("", "")) # 2 line breaks
# Add a second table, theme : vanilla table
doc <- addTitle(doc, "Vanilla table")
doc <- addFlexTable( doc, vanilla.table(data))
Writing the Word Report to a File
Now that we have generated our word report, we need to write it to a file.
writeDoc(doc, file = "r-reporters-word-document-add-table.docx")
However, this approach has some limitations. The downloadHandler function only allows us to specify the filename and content of the download file, but not the format of the content itself.
To overcome this limitation, we can use the ReporteRs package’s writeDoc function in combination with the Shiny app’s downloadHandler.
Here is an updated version of our code that generates a word report:
library(shiny)
library(ReporteRs)
# UI definition
ui <- fluidPage(
downloadButton('downloadData', 'Download')
)
# Server definition
server = function(input, output) {
# Create reactive data frame
inFile <- reactive({input$datasetA})
# Reactive data
data_df <- reactive({
if(is.null(inFile()))
return(NULL)
read.csv(inFile()$datapath, header = T, sep = ',',
stringsAsFactors = F)
})
# Generate word report
doc <- docx()
data<- data_df()[1:10, 1:10]
# Add a first table : Default table
doc <- addTitle(doc, "Default table")
doc <- addFlexTable( doc, FlexTable(data))
doc <- addParagraph(doc, c("", "")) # 2 line breaks
# Add a second table, theme : vanilla table
doc <- addTitle(doc, "Vanilla table")
doc <- addFlexTable( doc, vanilla.table(data))
# Write word report to file
output$downloadData <- downloadHandler(
filename = function() {
paste0("r-reporters-word-document-add-table.docx")
},
content = function(file) {
writeDoc(doc, file = file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
This updated code generates a word report and writes it to a file when the user clicks on the download button.
Conclusion
In this blog post, we explored how to generate word reports with R Shiny using the ReporteRs package. We covered the basics of Shiny and ReporteRs, and then dove into the code to generate a word report.
Last modified on 2024-02-28