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

时间:2019-06-18 09:43:07

标签: r user-interface shiny shinyjs

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

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

  • 如果输入为空,则为空

  • 如果输入不为null,则生成表并显示它

  • 如果该表已经显示,请将其隐藏。

我尝试将表格输出作为const EditForm = ({ reduxstore: {state, loading}, callreduxfunction, match, history}) => { const [formData, setFormData] = useState({ _id: '', field1: '' }) useEffect( () => { callreduxfunction(match.params.id); setFormData({ _id: match.params.id, field1: !loading ? state.field1 : '' }); }, [loading, callreduxfunction, match]); ) } 放置,并在单击按钮时切换它。可以,但是当我再次按下按钮以隐藏表格时,表格会重新加载。

我的用户界面中有这个

hidden

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

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

如您所知,单击 observeEvent(input$viewFile,{ output$fileTable <- renderTable({...)}) #generating the table toggle("fileTable") }) 按钮时,将呈现viewFile输出,然后将其切换(不再隐藏),因此显示)。第一次生成输出是完美的。

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

有什么办法可以保持切换正常工作,但是会阻止tableOutput的再生?我曾考虑过在输出上使用renderTable,但是您无法评估服务器上的输出项目。

({if


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

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

1 个答案:

答案 0 :(得分:1)

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

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

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")
})

会发生什么:

  • 用户加载文件

  • 表是从文件生成的,但未显示

  • 出现“查看文件”按钮

  • 用户单击按钮

  • 表已显示

  • 用户再次单击按钮

  • 表已隐藏

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