简体   繁体   English

将r Shiny ObserveEvent与downloadHandler一起使用

[英]Using r shiny observeEvent with a downloadHandler

I am challenged by the process of using an observer to observe the action of creating a CSV file for download. 使用观察员观察创建CSV文件以供下载的操作的过程给我带来了挑战。 I think that the R-Studio documentation states that eventExpr may be a "complex expression inside curly braces." 我认为R-Studio文档指出eventExpr可能是“花括号内的复杂表达式”。 Is this a valid eventExpression? 这是有效的eventExpression吗? I am using this construct 5 times, and all five conditions are triggered when my shinyApp initiates. 我使用此构造5次,并且在我的ShinyApp启动时将触发所有五个条件。

    observeEvent(
        {
        ### Observe the download handler preparing for CSV download
        output$Wire_Centers.csv <- downloadHandler(
            filename = "Wire_Centers.csv",
            content = function(file) {
                write.table(WC_List_2(), file, row.names=FALSE, col.names = TRUE, sep=',') ### end write.table
                } # End content function
            ) # End downloadHandler
            }, { # End observered event, start log
        logUse("WC_Download")
        }) # end observeEvent output condition

Any ideas or suggestions? 有什么想法或建议吗?

As you noted, all five are being called at startup. 如您所述,这五个都在启动时被调用。 The code isn't tied to an input$ or other change, so it just runs and it reports that it started . 该代码与输入$或其他更改无关,因此它只运行并报告已启动 Consider if you want it to report after the download finishes or if you want to run the code on user input. 考虑是否要在下载完成后报告它,还是要根据用户输入运行代码。

I accomplished adding the activity log action by adding the log function call as the first clause of the content function for the downloadHandler. 通过将log函数调用添加为downloadHandler的内容函数的第一子句,我完成了添加活动日志操作的操作。 The call logUse("WC_Download") operates as required, and the activity log is appropriately augmented. 调用logUse("WC_Download")根据需要进行操作,并且适当地增加了活动日志。

    output$Wire_Centers.csv <- downloadHandler(
        filename = "Wire_Centers.csv",
        content = function(file) {
            logUse("WC_Download")
            write.table(WC_List_2(), file, row.names=FALSE, col.names = TRUE, sep=',') ### end write.table
            }, # End content function
        ) # End downloadHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM