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

时间:2017-12-13 19:49:26

标签: r shiny reactive

我有一个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)我看到该过程运行了两次: 发送{“忙”:“忙”}发送{“忙”:“空闲”}。 任何人都可以告诉我为什么我的应用程序有这种行为?

1 个答案:

答案 0 :(得分:1)

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

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)。即使eventExpr,也始终会检查ignoreInit = TRUE。第二个繁忙来自动态UI的初始渲染。