Shiny App - Generate & Download PPTX Slides from Uploaded CSV
In this article, we’ll explore how to create a shiny app that generates PowerPoint slides (PPTX) from an uploaded CSV file. We’ll cover the necessary steps to read in the CSV file, generate the PPTX slides, and download them as a presentation.
Introduction
PowerPoint is a popular presentation software used for creating engaging slideshows. However, working with PowerPoint files can be cumbersome, especially when it comes to generating slides from data. This is where R and its Shiny package come into play. In this article, we’ll show you how to create a shiny app that generates PPTX slides from an uploaded CSV file.
Prerequisites
To follow along with this article, you’ll need the following:
- R
- Shiny package (install using
install.packages("shiny")) - Pptx package (install using
install.packages("pptx"))
The pptx package is used to generate PPTX files from R.
Creating the Shiny App
First, let’s create a new shiny app that includes the necessary UI elements for uploading a CSV file and generating PPTX slides. We’ll use the fluidPage function to create a responsive layout with a title panel, sidebar, and main panel.
ui <- fluidPage(
# Title panel
titlePanel("PPTX Slides Generator"),
# Sidebar for uploading the CSV file
sidebarLayout(
sidebarPanel(
fileInput("file1", "Upload CSV File", accept = c("text/csv", "text/comma-separated-values", ".csv"))
),
# Main panel for generating and downloading PPTX slides
mainPanel(
downloadButton('downloadData', 'Generate and Download')
)
)
)
In the above code, we’ve added a fileInput element to upload CSV files. We’ll use this file input in our server function to read in the uploaded CSV file.
Server Function
The server function is where the magic happens. In this case, we’ll define a function that generates PPTX slides from the uploaded CSV file and downloads them as a presentation.
server <- function(input, output, session) {
# Initialize reactive values for data storage
r_values <- reactiveValues(data = NULL)
# Observe the upload event and read in the CSV file
observeEvent(input$file1, {
req(input$file1)
df <- read.csv(input$file1$datapath)
# Store the data in reactive values
r_values$data <- df
})
# Generate PPTX slides from the stored data
output$downloadData <- downloadHandler(
filename = "file.pptx",
content = function(file) {
# Use pptx package to generate PPTX slides
require(pptx)
# Create a new presentation
p <- new_presentation()
# Add title slide
add_slide(p, 1, "Title Slide")
# Add data slides
for (i in 1:nrow(df)) {
add_slide(p, i + 1, paste("Slide ", i))
assign(i, data.frame(x = df$x[i], y = df$y[i]))
}
# Save the presentation as a PPTX file
save_presentation(file, p)
}
)
}
In the above code, we’ve defined an observeEvent function that reads in the uploaded CSV file and stores it in reactive values. We then use this stored data to generate PPTX slides using the pptx package.
Conclusion
In this article, we’ve shown you how to create a shiny app that generates PowerPoint slides (PPTX) from an uploaded CSV file. By following these steps, you can create a user-friendly interface for uploading CSV files and generating PPTX slides in R using Shiny.
Last modified on 2023-09-11