【问题标题】:R Shiny, monitoring a variable and updating uiR Shiny,监视变量并更新 ui
【发布时间】:2015-12-11 01:39:01
【问题描述】:

我正在尝试在闪亮的应用程序中创建一个消息系统,当某些任务完成时会收到通知,但目前它不起作用:

用户界面:

library(shinydashboard)
dashboardPage(
  dashboardHeader(title = "My Dashboard",
                  dropdownMenuOutput("messageMenu")
),
dashboardSidebar(),
dashboardBody("Hi", actionButton("go", label="DO STUFF!"), actionButton("go2", label="DO MORE STUFF!"))
)

服务器:

library(shinydashboard)

shinyServer(function(input, output) {

  M_Store <- reactiveValues(DF = data.frame(
    from = c("Admininstrator", "New User", "Support"),
    message = c(
      "Sales are steady this month.",
      "How do I register?",
      "The new server is ready."
    ),
    stringsAsFactors = FALSE
  ))

  output$messageMenu <- renderMenu({
    msgs <- apply(M_Store$DF, 1, function(row) {
      messageItem(from = row[["from"]], message = row[["message"]])
    })
    dropdownMenu(type = "messages", .list = msgs)
  })

  reactive({
    input$go
    message("Button pressed. Execute analysis!")
    message("Pretend analysis got done!")
    message("Now want to send a message that the analysis is done!")
    M_Store$DF <- rbind(isolate(M_Store$DF), data.frame(from="Meee", message="Done message!"))
    })

  reactive({
    input$go2
    message("Second button pressed. Execute second analysis!")
    message("Some computation!")
    message("Want to update the user on progress with a message!")
    M_Store$DF <- rbind(isolate(M_Store$DF), data.frame(from="Someone else!", message="Progress message2!"))
    message("More computations....")
    message("Done, want to tell user I'm done!")
    M_Store$DF <- rbind(isolate(M_Store$DF), data.frame(from="Someone else!", message="Done message2!"))
  })
})

你看出我的意图了吗?我希望能够推送分析或行动进展的消息。我认为在 M_Store 中有一个反应式 DF 意味着每当它被操纵时,任何依赖于它的东西也是如此,即 output$messageMenu。

我想做的类似于闪亮的进度条:当您进行计算时,您只需更新它们的变量,它们就会在屏幕上发生变化。

谢谢, 本。

【问题讨论】:

    标签: r shiny reactive-programming shinydashboard


    【解决方案1】:

    您可以将reactiveValues 函数与isolate 结合使用。您的案例看起来像:

    messageData <- data.frame(from = character(0),  message = character(0), stringsAsFactors = F) #simplified this a bit
    
    shinyServer(function(input, output) {
    
      M_Store <- reactiveValues(DF = messageData)
    
      newMessage <- reactive(data.frame(from = as.character(input$from),message = as.character(input$message)))
      observe({
        M_Store$DF <- rbind(isolate(M_Store$DF), newMessage())
      })
    
      output$myUI <- renderUI({
    
       #some code
       ... M_Store$DF ...
       #some code
    
      })
    })
    

    M_Store$DF 现在是一个反应值。当input$frominput$message 中的一个 改变时 , newMessage 将发生变化,这又将 rbind 一个新行。如果M_Store 在任何renderUI 函数中,它将更新该值。

    如果我得到您想要做的事情,您可能希望将 newMessage 更改为 eventReactive,以便用户需要按下按钮来提交任何更改(相对于每次输入更改时都会触发)。

    编辑:

    根据您上面的编辑,您需要以下内容而不是您的反应函数:

    observeEvent(input$go,{
        message("Button pressed. Execute analysis!")
        message("Pretend analysis got done!")
        message("Now want to send a message that the analysis is done!")
        M_Store$DF <- rbind(isolate(M_Store$DF), data.frame(from="Meee", message="Done message!"))
      })
    

    每次input$go 的值发生变化时(每次按下按钮)都会触发。如果在这里放置其他输入或反应值,则可以将其绑定到其他进程或 ui 元素

    【讨论】:

    • 我想做的是当有人做一些动作时,比如加载数据,或者做一些分析,动作完成后,一条消息会被推送到messageData ,并显示在 UI 中。当某些反应性表达时,将生成特定消息,例如依赖于输入的 t 检验完成。
    • 我刚刚编辑了这个问题,用一个玩具示例来展示我想要做什么。 (虽然不起作用)
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2020-12-13
    • 2015-01-17
    • 2013-04-14
    相关资源
    最近更新 更多