【问题标题】:No update on initial load of Shiny appShiny 应用程序的初始加载没有更新
【发布时间】:2016-01-28 15:23:26
【问题描述】:

在 RStudio Shiny 应用中,让观察者在初始应用加载后开始观察的最佳方式是什么。例如,在下面的应用程序中,我希望选择输入以 a1 的初始值开始,然后,一旦加载应用程序,当用户从 input1 进行选择时,我希望 input2 更改为“3” .但只有在应用加载后——我希望用户首先看到“1”。

这显然只是更复杂应用的一个非常简单的示例。在我更复杂的应用程序中,我设置了一个条件,将值与全局值进行比较,但必须有更简单的方法。

server<-function(session, input, output) {

  observeEvent(input$input1, {
    updateSelectInput(session, "input2", selected="3")

  })


}

ui<-fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
        selectInput("input1", label="Input1", choices=c("a", "b", "c"), selected="a"),
        selectInput("input2", label="Input2", choices=c("1", "2", "3"), selected="1")
    ),

    mainPanel(

    )
  )
)

shinyApp(ui=ui, server=server)

【问题讨论】:

    标签: r shiny rstudio


    【解决方案1】:

    可能有一种更优雅的方法可以做到这一点,但您可以使用随着 Input1 的每次更改而增加的反应值添加一个计数器。在第一次更改输入 1 时,此计数器增加到 2,输入 2 更改为“3”。当 rv$X == 2 时,我将 observeEvent 设置为仅在第一次单击时更改 Input2,因此当 Input1 被修改时,Input2 并不总是更改为 3。请参阅下面的服务器 fxn:

    server<-function(session, input, output) {
       rv<-reactiveValues(X = 1)
       observeEvent(input$input1,{
          if(rv$X==2) updateSelectInput(session, "input2", selected="3")
       rv$X<-rv$X+1
    })
    

    }

    【讨论】:

    • 谢谢。这是一个可能的解决方案,但你是对的,它并不理想。在我的应用程序中,我已经有一个全局值形式的等效项,我在应用程序初始化时设置它,然后检查观察者中的相等性,然后在观察者中重新设置。如果可能的话,我会尽量避免这样的黑客攻击。
    猜你喜欢
    • 2015-08-23
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2011-08-10
    • 2015-06-12
    • 2023-03-21
    相关资源
    最近更新 更多