【问题标题】:Modularizing R Shiny code: ObserveEvent function in module模块化 R Shiny 代码:模块中的 ObserveEvent 函数
【发布时间】:2019-04-28 18:27:46
【问题描述】:

我正在尝试提高我的 app.R 代码在 R Shiny 中的可用性,该代码变得很长。

基本上,我想创建一个模块 (infras.R) 来包含大量与 checkboxInputs 链接的 observeEvent 函数。

我知道我需要在 app.R 中获取模块,将 observeEvent 包装在一个函数中,在 observeEvent 函数中包含输入 ID 的命名空间 (ns),并为该函数插入一个 callModule。我还将 callModule 包装在 ObserveEvent 中,这样它的功能就会持续存在,并且不会在启动 webapp 后仅触发一次。

运行 app.R 时输出以下错误,但我不确定如何解决:

Warning: Error in proxy: could not find function "proxy"
   81: eval
   80: eval
   79: %>%
   78: module [infras.R#153]
   73: callModule
   72: observeEventHandler  
    1: runApp

感谢您在这方面的帮助,因为我发现很难找到有关如何做到这一点的文献。

我的 R 脚本中的密钥 sn-ps。

infras.R(更新):

icons_pow <- awesomeIcons(
  iconColor = 'white',
  markerColor = 'green',
  text = "m"
)

mod <- function(input, output, session, pow_id, prox){

observeEvent(pow_id(),{
  if(pow_id() != 0){
     pow_id <- readOGR("../geospatial_files/ind", layer = "plants")
     pow_iddf <- as.data.frame(pow_id)
     prox %>%
       addAwesomeMarkers(lng=pow_iddf$coords.x1, lat=pow_iddf$coords.x2, group = "pow_idg", icon=icons_pow,
                    label = paste(pow_iddf$Name,pow_iddf$Power_type,sep = ", "))
  }
  else {prox %>% clearGroup("pow_idg") %>% removeControl(layerId="pow_idc")
  }
}
)
}

app.R(更新):

...
source("infras.R")
...

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

proxy <- leafletProxy("map")
callModule(mod, "mod", reactive(input$pow_id), proxy)


  })
...

}

【问题讨论】:

    标签: r shiny shiny-reactivity shinymodules


    【解决方案1】:

    您需要将 input 对象包装到 reactive 中,并将其用作模块的 输入 参数。另一个输入参数是您的传单代理。在模块内部,您可以使用observe 更改您的代理,然后立即更新:

    library(shiny)
    library(leaflet)
    library(RColorBrewer)
    
    # The module containing the observer. Input is the reactive handle of legend input and the proxy
    mod <- function(input, output, session, legend, prox){
    
      observe({
        prox %>% clearControls()
        if (legend()) {
          prox %>% addLegend(position = "bottomright",
                                pal = colorNumeric("Blues", quakes$mag), values = ~mag
          )
        }
      })
    
    }
    
    ui <- bootstrapPage(
      checkboxInput("legend", "Show legend", TRUE),
      tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
      leafletOutput("map", width = "100%", height = "100%")
    )
    
    server <- function(input, output, session) {
    
      output$map <- renderLeaflet({
        pal <- colorNumeric("Blues", quakes$mag)
        leaflet(quakes) %>% addTiles() %>%      
          addCircles(radius = ~10^mag/10, weight = 1, color = "#777777",
                     fillColor = ~pal(mag), fillOpacity = 0.7, popup = ~paste(mag)) %>% 
          fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
      })
    
      # This is the handle for map
      proxy <- leafletProxy("map", data = quakes)
      callModule(mod, "mod", reactive(input$legend), proxy)
    
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 感谢您的快速回复!我已经更新了上面的代码,现在可以正常工作了。
    猜你喜欢
    • 2017-05-21
    • 2017-12-23
    • 2021-05-21
    • 1970-01-01
    • 2022-01-25
    • 2021-05-14
    • 1970-01-01
    • 2021-05-28
    • 2016-11-15
    相关资源
    最近更新 更多