R Shiny,监控变量并更新ui

时间:2015-12-11 01:39:02

标签: r shiny reactive-programming shinydashboard

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

UI:

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会意味着每当它被操纵时,那么依赖于它的任何东西也就是输出$ messageMenu。

我想要做的是类似于闪亮的进度条:当你进行计算时,你只需要更新它们的变量,然后在屏幕上进行更改。

谢谢, 本。

1 个答案:

答案 0 :(得分:2)

您可以将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函数内,则会将该值更新为wll。

如果我得到您要做的事情,您可能希望将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元素绑定