如何在R中使用Leaflet更改标题文本颜色?

3

我正在尝试改变幻灯片标题"大小"的样式/颜色,但我不知道该怎么做。 我尝试在tags $ style行中添加类似于p {color:red}之类的内容,如下所示:

  tags$style(type = "text/css", "html, body {width:100%;height:100%}", "p {color=white}"),

没有任何效果。有什么建议吗?我认为这不是在sliderInput函数本身中更改的内容,而是CSS,只是我无法完全弄清楚。

library(shiny)
library(leaflet)
library(RColorBrewer)

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
    sliderInput("range", "Magnitudes", min(quakes$mag), max(quakes$mag),
      value = range(quakes$mag), step = 0.1
    ),
    selectInput("colors", "Color Scheme",
      rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
    ),
    checkboxInput("legend", "Show legend", TRUE)
  )
)

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

  # Reactive expression for the data subsetted to what the user selected
  filteredData <- reactive({
    quakes[quakes$mag >= input$range[1] & quakes$mag <= input$range[2],]
  })

  # This reactive expression represents the palette function,
  # which changes as the user makes selections in UI.
  colorpal <- reactive({
    colorNumeric(input$colors, quakes$mag)
  })

  output$map <- renderLeaflet({
    # Use leaflet() here, and only include aspects of the map that
    # won't need to change dynamically (at least, not unless the
    # entire map is being torn down and recreated).
    leaflet(quakes) %>% addTiles() %>%
      fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
  })

  # Incremental changes to the map (in this case, replacing the
  # circles when a new color is chosen) should be performed in
  # an observer. Each independent set of things that can change
  # should be managed in its own observer.
  observe({
    pal <- colorpal()

    leafletProxy("map", data = filteredData()) %>%
      clearShapes() %>%
      addCircles(radius = ~10^mag/10, weight = 1, color = "#777777",
        fillColor = ~pal(mag), fillOpacity = 0.7, popup = ~paste(mag)
      )
  })

  # Use a separate observer to recreate the legend as needed.
  observe({
    proxy <- leafletProxy("map", data = quakes)

    # Remove any existing legend, and only if the legend is
    # enabled, create a new one.
    proxy %>% clearControls()
    if (input$legend) {
      pal <- colorpal()
      proxy %>% addLegend(position = "bottomright",
        pal = pal, values = ~mag
      )
    }
  })
}

shinyApp(ui, server)
1个回答

3
直截了当地说: 尝试将以下内容添加到您的 ui 中:
tags$style(type = "text/css", 'label[for="range"] {color: white;}'),

关于如何自行解决问题的更多细节:

以下是我的解决步骤。

  1. Use runApp() to run the code you've got, producing a very nice leaflet map in your browser.

  2. Right-click on that map and select "View Page Source" to see the source code that's producing the map.

  3. Search that source for the string "Magnitude", to find the HTML element that codes for the title you'd like to whiten. Here's what I find when I do that:

    <label class="control-label" for="range">Magnitudes</label>
    
  4. From that, construct a CSS selector (here including an "attribute selector") that'll find that element, and use it to change the color. Adding this, following the first line of your call to bootstrapPage(), does the trick for me:

    tags$style(type = "text/css", 'label[for="range"] {color: white;}'),
    
  5. Use runApp() again to confirm that the edit worked.


感谢HTML元素技巧--由于我已经用尽了RStudio,所以没有这个选项。非常好的描述,非常好的答案。 - skathan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接