【问题标题】:Shiny observeEvent triggers on app launch应用启动时闪亮的 observeEvent 触发器
【发布时间】:2018-05-27 19:37:50
【问题描述】:

我有一个带有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) 我看到该进程运行了两次: 发送{“忙碌”:“忙碌”}发送{“忙碌”:“空闲”}。 谁能告诉我为什么我的应用会出现这种行为?

【问题讨论】:

  • 你能发布一个可重现的例子吗?该代码有几个问题阻止它运行。
  • 对不起!!我从数据框中删除了一些变量,忘记声明它们!现在我认为它有效......

标签: r shiny reactive


【解决方案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)。即使ignoreInit = TRUE,它也会一直运行以检查eventExpr。第二个忙来自于动态UI的初始渲染。

【讨论】:

  • 那么,是否不可能不显示加载消息两次,因为这是闪亮的正常行为,空闲然后再次忙碌?当我删除 observeEvent(input[[paste0('go_btn',i)]]) 时,它似乎没有忙两次。
  • 你是对的 - 其中一条繁忙的消息来自观察者,我修改了我的答案。观察者总是会收到一条繁忙的消息,检查其事件表达是否真实。但这并不一定意味着处理程序表达式已运行。
猜你喜欢
  • 2020-09-24
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 2018-05-02
  • 2021-03-04
  • 1970-01-01
相关资源
最近更新 更多