【问题标题】:How to dynamically change the size of leaflet map in Shiny R?如何在 Shiny R 中动态更改传单地图的大小?
【发布时间】:2019-04-09 10:57:33
【问题描述】:

我想知道,我们如何在闪亮的 R 中更改传单地图的大小。例如考虑以下代码:

library(leaflet)
library(shiny)

app = shinyApp(
  ui = fluidPage(
    sidebarLayout(
      sidebarPanel( sliderInput("obs",
                    "Number of observations:",
                    min = 0,
                    max = 1000,
                    value = 500)
        ),
      mainPanel(
        leafletOutput('myMap', width = "200%", height = 1400)
        )
    )
  ),
  server = function(input, output) {
    map = leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 17)
    output$myMap = renderLeaflet(map)
  }
)

if (interactive()) print(app)

为了改变地图的大小,我可以改变 ui 中的宽度和高度参数。当我尝试在服务器中更改相同内容时,它没有成功。

我不知道,有什么方法可以通过 server.xml 更改 ui 中的参数。我尝试了这种方法,但没有奏效。

library(leaflet)
library(shiny)

Height = 1000 
app = shinyApp(
  ui = fluidPage(
    sidebarLayout(
      sidebarPanel( sliderInput("Height",
                    "Height in Pixels:",
                    min = 100,
                    max = 2000,
                    value = 500)
        ),
      mainPanel(
        leafletOutput('myMap', width = "200%", height = Height)
        )
    )
  ),
  server = function(input, output) {
    Height <- reactive(input$Height)
    map = leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 17)
    output$myMap = renderLeaflet(map)
  }
)

if (interactive()) print(app)

我只是想知道,如何使地图的大小动态化以便我可以控制它。非常感谢任何帮助。

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    你需要在服务器端渲染leafletOutput 喜欢

    app = shinyApp(
      ui = fluidPage(
        sidebarLayout(
          sidebarPanel( sliderInput("Height",
                                    "Height in Pixels:",
                                    min = 100,
                                    max = 2000,
                                    value = 500)
          ),
    
          mainPanel(
            uiOutput("leaf")
    
          )
        )
      ),
      server = function(input, output) {
        output$leaf=renderUI({
          leafletOutput('myMap', width = "200%", height = input$Height)
        })
    
        output$myMap = renderLeaflet(leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 17))
      }
    )
    

    【讨论】:

      【解决方案2】:

      但是以后不能使用leafletProxy!!

      app = shinyApp(
        ui = fluidPage(
          sidebarLayout(
            sidebarPanel( sliderInput("Height",
                                      "Height in Pixels:",
                                      min = 100,
                                      max = 2000,
                                      value = 500),
                          actionButton("mbutton", "show marker")
            ),
      
      
            mainPanel(
              uiOutput("leaf")
      
            )
          )
        ),
        server = function(input, output) {
          output$leaf=renderUI({
            leafletOutput('myMap', width = "200%", height = input$Height)
          })
      
          output$myMap = renderLeaflet(leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 17))
      
          observeEvent(input$mbutton,{
      
              leafletProxy("myMap") %>%
                addMarkers(-93.65, 42.0285)
          })
          }
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-18
        • 2018-08-31
        • 2014-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-12
        • 2017-02-08
        相关资源
        最近更新 更多