Shiny 应用程序的初始加载没有更新 [英] No update on initial load of Shiny app

查看:62
本文介绍了Shiny 应用程序的初始加载没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In an RStudio Shiny app what is the best way to have observers start observing AFTER the initial app load. For example, in the app below I want the select inputs to start with initial values of a and 1 and then, once the app is loaded, when the user makes a selection from input1 I want input2 changed to "3". But only after the app has loaded -- I want the user to see "1" first.

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

This is obviously just a very simple example of a more complex app. In my more complex app I set a condition where I compare the value against a global value but there must be an easier way.

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)

推荐答案

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

There is probably a more elegant way to do this, but you can add a counter using a reactive value that increases with every change to Input1. On the first change to Input 1 this counter increases to 2 and Input2 is changed to "3". I set the observeEvent to only change Input2 on that first click when rv$X == 2 so Input2 is not always changed to 3 when ever Input1 is modified. See the server fxn below:

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
})

}

这篇关于Shiny 应用程序的初始加载没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆