scdavis50
scdavis50

Reputation: 111

Shiny App - Generate & download PPTX slides from uploaded CSV

I've got an R script that generates various graphs on powerpoint slides by compiling data from a csv file. I'm trying to convert this to a shiny app that generates the deck after uploading a csv file but can't figure out how to read in the csv file and then generate the pptx download.

Here's my UI:

ui <- (fluidPage(
  titlePanel("Title"),
  title = "File Upload",

  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "File1:",
                accept = c("text/csv", "text/comma-separated-values, 
text/plain", ".csv")),
    ),
mainPanel(
  downloadButton('downloadData', 'Download')
    )

  )
)
  )

And my server function:

server<- function(input, output,session) {

 output$downloadData <- downloadHandler(
data_file <- reactive({
  inFile <- input$file1
  if (is.null(inFile)) return(NULL)
  read.csv(inFile$datapath, na.strings = "null")
}),


filename = "file.pptx",

content = function(file)

When reference a local file, the code generates a deck. When I upload the file,I get the error below. I've also moved the datafile portion outside the download handler but then nothing happens.

Warning: Error in downloadHandler: unused argument (datafile <- reactive({ inFile <- input$file3 if (is.null(inFile)) return(NULL) read.csv(inFile$datapath, na.strings = "null") }))

Any suggestions?

Upvotes: 1

Views: 607

Answers (2)

divibisan
divibisan

Reputation: 12155

The problem you're having is that downloadHandler, like all functions, only accepts specific arguments as described in its help file: ?downloadHandler:

downloadHandler(filename, content, contentType = NA, outputArgs = list())

By sticking a block of R code (the data_file <- reactive({...) inside the function call, R considers that code to be an argument and tries to figure out how to pass it into the function. Since it's the first argument, it would normally try to pass it to the first argument, filename in this case (which would give an error, since filename accepts a string, not an R expression), but you've already defined filename using a named argument later in the call, so R has no idea what to do with this argument and returns an unused argument error.

You should move this block of code outside the downloadHandler function (but inside the server function), then call on the value of the reactive expression from inside the function you pass to the content argument.

Upvotes: 0

Charlie
Charlie

Reputation: 11

I usually avoid this by using reactiveValues with observeEvent, rather than reactive.

server <- function(input, output){
r_values <- reactiveValues(data=NULL) # reactive values just acts as a list

    observeEvent(input$file1,{
        req(input$file1)
        df <- read.csv(input$file1$datapath)
    })
}

Then you can extract your data using r_values$data.

Upvotes: 1

Related Questions