简体   繁体   English

应用程序启动时闪亮的observeEvent触发器

[英]Shiny observeEvent triggers on app launch

I have an app with observeEvent that triggers when the app starts, it doesn't wait for the button to be clicked. 我有一个带有observeEvent的应用程序,它会在应用程序启动时触发,它不会等待单击该按钮。 In this example it doesn't appear to have a difference but in my real app it causes the busyIndicator to show twice on the initial load. 在这个例子中它似乎没有区别,但在我的真实应用程序中它导致busyIndi​​cator在初始加载时显示两次。

name<-sample(c('a','b','c'),replace=T,5)
LAT<-runif(5,min=-26, max=-22)
LONG<-runif(5,min=-54, max=-48)
data<-data.frame(name,LAT,LONG)

ui <- shinyUI(fluidPage(
  selectInput('muni',label='Select city',
              choices=c('Show all',sort(levels(data$name)),selected=NULL)),
  htmlOutput('box'),
  leafletOutput('map')
  ))

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

data1<-reactive({
       if (input$muni!='Show all') {
           data<-data[which(data$name==input$muni),]
           }
       return(data)
})

output$box <- renderUI({

data<-data1()
num<-as.integer(nrow(data))

lapply(1:num, function(i) {
       bt <- paste0('go_btn',i)
       fluidRow(
                HTML(paste0('<div style="border: 1px solid #00000026; 
                border-radius: 10px; padding: 10px;">
                <span style="font-size:14px font-weight:bold;">',
                data$name[i],' - areas: a1, a2, a3</span></br>',
                actionButton(bt,'See map',icon=icon('map-marker',lib='font-awesome')),
                HTML('</div></br>')
                )))
       })
    })

output$map<-renderLeaflet({

data<-data1()
rownames(data)<-seq(1:nrow(data))

leaflet(data) %>%
  addProviderTiles("Esri.WorldTopoMap") %>% 
  setView(-51.5,-24.8,zoom=7) %>% 
  addMarkers(lng=~data$LONG,lat=~data$LAT)

})

lapply(1:nrow(data), function(i) {
       observeEvent(input[[paste0('go_btn',i)]], {
                    data<-data1()
                    rownames(data)<-seq(1:nrow(data))

                    leafletProxy('map',data=data,session=session) %>%
                                 clearMarkers() %>%
                                 setView(data$LONG[i],data$LAT[i],zoom=15) %>%
                                 addMarkers(lng=data$LONG[i],lat=data$LAT[i])
        },ignoreInit = T)
     })
   }

  shinyApp(ui, server)

Using options(shiny.trace = TRUE) I see that the process runs twice: SEND {"busy":"busy"} SEND {"busy":"idle"}. 使用options(shiny.trace = TRUE)我看到该过程运行两次:SEND {“busy”:“busy”} SEND {“busy”:“idle”}。 Can anyone tell me why my app is having this behavior? 任何人都可以告诉我为什么我的应用程序有这种行为?

I couldn't run your example so I made my own: 我无法运行你的例子所以我自己做了:

library(shiny)

options(shiny.trace = TRUE)

ui <- shinyUI(fluidPage(
  uiOutput("content")
))

server <- function (input, output, session) {
  output$content <- renderUI({
    actionButton("btn", "Button")
  })

  observeEvent(input$btn, {
    print("btn")
  })
}

shinyApp(ui, server)

The console output for this is: 控制台输出为:

SEND {"config":{"workerId":"","sessionId":"1ceb44576d353c33bdc92e1eebba7ad0","user":null}}
RECV {"method":"init","data":{".clientdata_output_content_hidden":false,".clientdata_pixelratio":1,".clientdata_url_protocol":"http:",".clientdata_url_hostname":"127.0.0.1",".clientdata_url_port":"5326",".clientdata_url_pathname":"/",".clientdata_url_search":"",".clientdata_url_hash_initial":"",".clientdata_url_hash":"",".clientdata_singletons":"",".clientdata_allowDataUriScheme":true}}
SEND {"busy":"busy"}
SEND {"recalculating":{"name":"content","status":"recalculating"}}
SEND {"recalculating":{"name":"content","status":"recalculated"}}
SEND {"busy":"idle"}
SEND {"errors":[],"values":{"content":{"html":"<button id=\"btn\" type=\"button\" class=\"btn btn-default action-button\">Button<\/button>","deps":[]}},"inputMessages":[]}
RECV {"method":"update","data":{"btn:shiny.action":0}}
SEND {"busy":"busy"}
SEND {"busy":"idle"}

There are two "busy" messages. 有两条“忙”的消息。 The first is from observeEvent executing its eventExpr (which is NULL at this time, so it doesn't execute handlerExpr ). 第一个是来自observeEvent执行它的eventExpr (此时为NULL ,因此它不执行handlerExpr )。 It always runs to check eventExpr even if ignoreInit = TRUE . 即使ignoreInit = TRUE它总是运行以检查eventExpr The second busy comes from the initial rendering of dynamic UI. 第二个繁忙来自动态UI的初始渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 闪亮的observeEvent是否可以等待它触发的另一个观察者? - Can a shiny observeEvent wait for another observer that it triggers? 在闪亮的应用程序中提高observeEvent的性能 - Improve performances of observeEvent in shiny app 在闪亮的应用程序中使用观察事件进行 numericInput - using observeEvent for numericInput in shiny app 在 Shiny 应用程序中使用 observeEvent 显示 plot - Showing plot using observeEvent in Shiny app 替换observeEvent() 以在闪亮的应用程序中恢复自动功能 - Replace observeEvent() to restore automatic functionality in a shiny app 传单代理无法在闪亮的应用程序中使用 observeevent 正确更新 - leaflet proxy does not update correctly with observeevent in shiny app ObserveEvent中的闪亮应用程序错误? [.default:错误的下标类型&#39;列表&#39;中的错误 - Shiny App Error in ObserveEvent? Error in [.default: invalid subscript type 'list' 使用一个 if() 而不是多个 observeEvent() 在闪亮的应用程序中显示消息 - Use one if() instead of multiple observeEvent() to display message in a shiny app 无法使用observeEvent和eventReactive更新闪亮应用程序中的输入 - Can't update inputs in shiny app with observeEvent and eventReactive 调用 function,并将值返回给 shiny app 中的 observeEvent - Call function, and return the value back to observeEvent in shiny app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM