如何在有光泽的传单中使用非标准评估?

时间:2015-09-21 20:27:11

标签: r shiny leaflet

我希望我的宣传单闪亮应用用户指定哪个data.frame列是控制圆圈大小的变量。我从rst的第一个页面https://rstudio.github.io/leaflet/shiny.html获取示例,以用作示例应用程序。我已经在控制圆圈大小的代码中添加了一个selectInput元素和一些注释。它使用非标准评估(我相信它是但我可能在我的头脑中有我的NSE概念不正确。在ggplot中有像aes_string这样的函数来添加字符串到美学。是否有这样的函数存在于传单或是否存在一个更简单的方法?应用程序的代码在下面,列选择器作为输入$ STACKOVERFLOW,并注释掉应用程序中控制cicle大小的位置。谢谢。

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),
                  selectInput("STACKOVERFLOW", "USER INPUT FOR CIRCLE SIZE", 
                              choices = c("depth",    "mag",      "stations"))
    )
)

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() %>%
            ############ADD USER INPUT FOR CIRCLE SIZE HERE USING COLUMN NAME AS STRING##################
            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)

0 个答案:

没有答案