如何将来自多个Shiny Widgets的输入提供给一个全局无功值?

时间:2016-05-04 21:49:42

标签: r shiny

我正在尝试构建一个Shiny小部件,以便用户可以从多个下拉列表中选择一个值或键入自己的值:

  textInput("select0", label = h5("Currently selected user")),
  actionButton("generateInsights", label = "Show info"),
  selectInput("select1", label = h5("Select high data volume user"), 
              choices = make_named_list(top_users$userid), 
              selected = 1),
  selectInput("select2", label = h5("Select new user"), 
              choices = make_named_list(new_users$objectid), 
              selected = 1)

然后我想制作一个对这些值中的任何一个调整都有反应的界面:

current_userid = ""

shinyServer(function(input, output) {

  current_userid_reactive = reactive({
    print("current userid got updated inside reactive expression")
    return(current_userid)
  })

  output$distPlot <- renderPlot({
    print("updating plot")
    user_data = get_user_data_from_user_id1(current_userid_reactive(), big_data)
    print(user_data)
    cat("number rows userdata is ", nrow(user_data))
    ggplot(data=user_data, aes(x=displaydate, y=measurementvalue, color=datatype)) +
      geom_point(size=.6) + geom_line(aes(displaydate, mav),color="black", size = .2) + 
      facet_grid(datatype ~ ., scales = "free_y", labeller=label_both) + labs(x = "time", y = "Measurement")
  })

  output$momentCountPlot <- renderPlot({
    plot(get_user_moment_count(input$select, big_data))
  })

  output$carbCountPlott <- renderPlot({
    plot(get_user_carb_count(input$select, big_data))
  })

  output$activityCountPlot <- renderPlot({
    plot(get_user_activity_count(input$select, big_data))
  })

  observe({
    current_userid <- input$select1
    cat("updated current_userid to ", current_userid)
  })

  observe({
    current_userid <- input$select2
    cat("updated current_userid to ", current_userid)
  })

  output$ui <- renderUI({
    h1(current_userid)
  })

})

我要做的是从input$select1input$select2更新current_userid的值,然后重新绘制,无论用户是否从其中一个或另一个中选择了一个新值选择。

但是,我发现observe函数按预期运行(当我进行下拉选择时),但是他们对全局current_userid的调整不会导致current_userid_reactive中的任何后续操作1}}或output$distPlot我如何实现我想要做的事情?为什么不使用reactive元素?

0 个答案:

没有答案