如何在Shiny R中动态更改传单地图的大小?

时间:2016-08-02 07:05:53

标签: r shiny leaflet

我想知道,我们如何在闪亮的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中更改width和height参数。当我试图在服务器中更改相同时,它没有成功。

我不知道,任何人都可以通过服务器更改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)

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

2 个答案:

答案 0 :(得分:3)

您需要在服务器端呈现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))
  }
)

答案 1 :(得分:1)

但是以后不能使用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)
    })
    }
)
相关问题