test fileInput with testServer()

Hi
I wanted to ask the same question I did at stackoverflow

Is there any way we can make use of the testServer() function in order to test if a file is correctly uploaded with fileInput() ?

Hi,

I'm new to the testServer function, but after playing with it for a bit I found something that seems to work

library(shiny)

ui <- fluidPage(
  tagList(
    fileInput("file", "Please upload a file"),
    tableOutput("text")
  )
)

server <- function(input, output, session){
  file <- reactive({
    input$file
  })
  
  output$text <- renderTable({
    req(file())
    read.csv(file()$datapath)
  })
}

shinyApp(ui, server)

address <- "path/to/text.csv"

testServer(server, {
  session$setInputs(file= list(datapath = address))
})

The issue was that you did not set the structure of the input correctly as a fileInput object is not just the path, but has multiple settings in a list. Since we only need the datapath, that's all I set here.

The default path will throw the "file not found error", but if you replace it with a real path, it runs without issue.

Hope this helps,
PJ

Thank you so much!
I can now test fileinputs in shiny!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.