如何使用单个 actionButton 生成 tableOutput 并切换它而不每次都重新加载它?

How to use a single actionButton to generate a tableOutput and toggling it without reloading it each time?

我正在开发一款闪亮的应用程序。我需要用户输入一个文件,然后从那个文件中得到一个table。

我需要一个按钮来执行此操作:

我尝试将 table 输出设为 hidden 并在单击按钮时切换它。它有效,但是当我再次按下按钮隐藏 table 时,table 被重新加载。

我的UI里有这个:

actionButton(inputId = "viewFile", label = "View file"),
hidden(tableOutput("fileTable"))

这是我在我的服务器上尝试过的:

  observeEvent(input$viewFile,{
      output$fileTable <- renderTable({...)}) #generating the table
      toggle("fileTable")
  })

正如你所理解的,当viewFile按钮被点击时,fileTable输出被渲染,然后它被切换(它不是隐藏不再显示了)。第一次生成输出,很完美。

但是,如果我再次单击按钮以隐藏 table,则会再次计算 renderTable。这是无用的操作(您不想生成 table 输出只是为了隐藏它)。

有什么方法可以保持切换功能正常工作,但又可以防止 table 输出重新生成?我考虑过在输出上使用 if,但您无法评估服务器的输出项。

(Warning: Error in $.shinyoutput: Reading objects from shinyoutput object not allowed.)


最后一个细节:稍后,如果文件已更改,我将尝试通过重新生成 table 输出来改进我的应用程序,是否也有办法做到这一点?

谢谢您,对于初学者的问题感到抱歉。我有点迷路了。如果您对我的代码有任何疑问,请不要犹豫。

我找到了一种简单的方法来做我想做的事。

我只是在加载数据时生成 table,然后用我的按钮,我只切换它。

UI:

file <- fileInput(inputId = "file", label="myfile")
hidden(actionButton(inputId = "viewFile", label = "View file"))
hidden(tableOutput("fileTable"))

服务器:

observeEvent(input$file, {
    #The output is generated but not displayed
    output$fileTable<- renderTable({read.csv(file[4][[1]])})
    #We display the button
    toggle("viewFile")
})


observeEvent(input$viewFile,{
    toggle("fileTable")
})

事情是这样的:

  • 用户加载文件

  • Table由文件生成但不显示

  • 按钮 "View file" 出现

  • 用户点击按钮

  • Table显示

  • 用户再次点击按钮

  • Table被隐藏

无限重复。 祝你有美好的一天