【问题标题】:Shiny - How to test fileInput()Shiny - 如何测试 fileInput()
【发布时间】:2021-04-21 10:02:19
【问题描述】:

我正在尝试编写测试以检查闪亮的函数fileInput() 是否正确读取文件。 我的问题是我不知道在session$setInputs() 中写什么才能从我的系统中获取文件。

这是一个示例应用:

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)

现在,我希望能够使用testServer() 来设置文件地址并查看我的应用是否正确加载它,但我不知道该怎么做:

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

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

我认为这与fileInput() 将文件上传到临时文件夹并返回到闪亮的数据框有关,您可以在其中获取数据路径,但我无法模拟此传递以使测试工作

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我和你有同样的问题,我做了一些调查,但找不到任何使用 testServer 或 testthat 测试 fileInput 的方法。我发现的最佳解决方案是通过在使用 shinytest 包的 recordTest() 记录测试时拍摄快照来测试 fileInput。

    【讨论】:

      【解决方案2】:

      抱歉这么晚才回复。 我在 rstudio 的论坛上问了同样的问题,得到了答案here

      它的基础是将文件的数据路径设置为列表:

      address <- "path/to/text.csv"
      
      testServer(server, {   session$setInputs(file= list(datapath = address)) })
      

      【讨论】:

        猜你喜欢
        • 2017-10-27
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2018-08-26
        • 1970-01-01
        • 2018-02-17
        • 2020-10-19
        • 1970-01-01
        相关资源
        最近更新 更多