How to Generate Word Docs in Shiny with officer, flextable, and shinyglide
Posted on November 10, 2022 by Ivan Millanes in R bloggers | 0 Comments
[This article was first published on Tag: r - Appsilon | Enterprise R Shiny Dashboards , and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here )
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Share Tweet
Sometimes, when you develop a Shiny application you want to download the results of their analysis as a report. Generating reports from Shiny applications is a very broad topic. So in this tutorial, we’ll focus on generating Word documents from table data.
The example dataset used considers a scenario in which we want to report whether groups defined in a clinical trial are similar with respect to certain variables (e.g. age, gender, etc.).
Before you get started, is your data clean? Check out these two R packages for cleaning and validating datasets for a smooth experience .
The objective of this tutorial is to provide tools that can be extended or used in your project. The final application is not production-ready. But we have plenty of other tutorials on our blog to help you in your project’s journey.
In our application, we use a subset of synthetic CDISC data released during 2022 .
The following code was used to create the example ‘ csv ‘ file:
# Install packages remotes::install_github("insightsengineering/ [email protected] *release") remotes::install_github("insightsengineering/ [email protected] *release") # Load libraries library(scda) library(scda.2022) library(dplyr) library(readr) # Get adsl data adsl_data select(USUBJID, AGE, SEX, RACE, DTHFL, TRT01A, BMRKR1, DTHCAT) # Save local copy to upload to application write_csv(adsl_data, "adsl_data.csv")
If you want to see what the first few rows of the dataset look like, run:
```{r, eval=TRUE} readr::read_csv("adsl_data.csv") |> head(10) |> flextable::flextable() |> flextable::autofit() ```
Packages Overview for Generating Word Docs from Table Data
Below is a list of packages that implement the core functionality of the application.
atable – supports the analysis and reporting of controlled clinical trials
flextable – provides a framework to easily create tables for reporting and publications, with functions that let users create, modify, and format their tables
officer – lets R users manipulate Word (.docx) and PowerPoint (.pptx) documents
shinyglide – an R package that provides carousel-like or assistant-like components to Shiny applications, thanks to the Glide JavaScript library
Next, we’ll begin creating the Shiny application.
Building the Shiny Application for Generating Word Docs
In this section, we’ll go step-by-step showing how to create your own Shiny application and how to implement the packages mentioned above.
Step 0: Minimal Shiny App
Let’s start by defining the most minimal Shiny application:
library(shiny) ui