繁体   English   中英

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

[英]Shiny observeEvent triggers on app launch

我有一个带有observeEvent的应用程序,它会在应用程序启动时触发,它不会等待单击该按钮。 在这个例子中它似乎没有区别,但在我的真实应用程序中它导致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)

使用options(shiny.trace = TRUE)我看到该过程运行两次:SEND {“busy”:“busy”} SEND {“busy”:“idle”}。 任何人都可以告诉我为什么我的应用程序有这种行为?

我无法运行你的例子所以我自己做了:

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)

控制台输出为:

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

有两条“忙”的消息。 第一个是来自observeEvent执行它的eventExpr (此时为NULL ,因此它不执行handlerExpr )。 即使ignoreInit = TRUE它总是运行以检查eventExpr 第二个繁忙来自动态UI的初始渲染。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM