【问题标题】:tags$style applied to all Leaflet Maps in R shinytags$style 应用于 R 中的所有 Leaflet Maps 闪亮
【发布时间】:2021-02-16 12:36:20
【问题描述】:

我可以在 R 应用程序中更改关于 的光标,但似乎无法弄清楚如何仅针对特定地图执行此操作。似乎最后一个tags$style 应用于所有以前的地图?我添加了一个只有两张地图的代表,但同样的逻辑可以应用于更多。任何帮助将不胜感激,因为它让我大吃一惊!谢谢。

library(shinydashboard)
library(leaflet)
library(tidyverse)

header = dashboardHeader(title = "Hydroclimatic Data")


# * sidebar ----
sidebar = dashboardSidebar(
  sidebarMenu(
    menuItem(
      "Map",
      tabName = "map",
      menuSubItem("Map", tabName = "map", icon = icon("chart-line"))
    ),
    menuItem(
      "Map2",
      tabName = "map2",
      menuSubItem("Map2", tabName = "map2", icon = icon("chart-line"))
    )))



body = dashboardBody(
  tabItems(
    tabItem(
      tabName = "map",
      tags$style(type = 'text/css', '
      .leaflet-container {
  cursor: crosshair !important;}'),
      fluidRow(
        tabBox(width = 12, id = "tab",
               tabPanel("Map", style = "height:92vh;",leafletOutput("swe_maps"))))
      ),
    
    
    # * * -- Snotel - Raw ----
    tabItem(
      tabName = "map2",
      tags$style(type = 'text/css', '
      .leaflet-container {
  cursor: help !important;}'),
      fluidRow(
        tabBox(width = 12, id = "tabchart",
               tabPanel("Map", style = "height:92vh;",leafletOutput("swe_maps2")))))))
  
  # UI ----------------------------------------------------------------------
  
  ui <- dashboardPage(header = header, sidebar = sidebar, body = body)
  
  # Server ------------------------------------------------------------------
  
  
  server <- function(input, output, session) {
    
    output$swe_maps <- renderLeaflet({leaflet("map1") %>% addTiles()})
    
    
    output$swe_maps2 <- renderLeaflet({leaflet("map2") %>% addTiles()})
    
    
  }



##### RUN APPLICATION #####
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: shiny r-leaflet r shiny leaflet shinydashboard r-leaflet


    【解决方案1】:

    不要在 css 中使用类选择器 .leaflet-container,而是使用 Id 选择器 #swe_maps 和 #swe_maps2。

    library(shinydashboard)
    library(leaflet)
    library(tidyverse)
    library(shiny)
    
    header = dashboardHeader(title = "Hydroclimatic Data")
    
    
    # * sidebar ----
    sidebar = dashboardSidebar(
      sidebarMenu(
        menuItem(
          "Map",
          tabName = "map",
          menuSubItem("Map", tabName = "map", icon = icon("chart-line"))
        ),
        menuItem(
          "Map2",
          tabName = "map2",
          menuSubItem("Map2", tabName = "map2", icon = icon("chart-line"))
        )))
    
    
    
    body = dashboardBody(
      tabItems(
        tabItem(
          tabName = "map",
          tags$style(type = 'text/css', '
          #swe_maps {
      cursor: crosshair !important;}'),
          fluidRow(
            tabBox(width = 12, id = "tab",
                   tabPanel("Map", style = "height:92vh;",leafletOutput("swe_maps"))))
        ),
        
        
        # * * -- Snotel - Raw ----
        tabItem(
          tabName = "map2",
          tags$style(type = 'text/css', '
          #swe_maps2 {
      cursor: help !important;}'),
          fluidRow(
            tabBox(width = 12, id = "tabchart",
                   tabPanel("Map", style = "height:92vh;",leafletOutput("swe_maps2")))))))
    
    # UI ----------------------------------------------------------------------
    
    ui <- dashboardPage(header = header, sidebar = sidebar, body = body)
    
    # Server ------------------------------------------------------------------
    
    
    server <- function(input, output, session) {
      
      output$swe_maps <- renderLeaflet({leaflet("map1") %>% addTiles()})
      
      
      output$swe_maps2 <- renderLeaflet({leaflet("map2") %>% addTiles()})
      
      
    }
    
    
    
    ##### RUN APPLICATION #####
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 感谢您的评论。我尝试了上面建议的代码,但无法让它在示例代码上运行。逻辑是有道理的,但由于某种原因,leaflet() 中的 elementId 和样式不是“点击”。有什么建议?你能让它在我提供的代码上工作吗?
    • @JoshErickson 我已经修改了我的答案以纠正错误。似乎地图 ID 已在 leafletOutput() 函数调用中分配。你应该在 css 中使用这些。
    猜你喜欢
    • 2017-08-08
    • 1970-01-01
    • 2018-06-04
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    相关资源
    最近更新 更多