R-shiny:变化滑块输入范围

时间:2015-07-26 09:06:26

标签: r leaflet shiny-server shiny

我正在使用闪亮的传单来绘制地图。我通过selectInput函数动态地获取地图输入。我所采用的所有矩阵都有不同的尺寸。现在我想通过sliderInput函数限制行数。

为此,我想确保范围(“已选择”)=范围(“sliderInput”)。

为此,可以输入以下数据:
数据输入

library(data.table)

    mat1 = data.table(long = runif(40, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) 
    mat2 = data.table(long = runif(9123, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) 
    mat3 = data.table(long = runif(722, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) 
    phys_summ = data.table(long = runif(9845, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) 
    mat4 = data.table(long = runif(99999, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) 

知道我该怎么做?

首次修改
我的r代码动态地采用矩阵。但滑块输入无法正常工作:
R代码:

  library(shiny)
    library(leaflet)
    {r, echo=FALSE, warning=FALSE, message=FALSE}
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
var=c("matrix 1" = "mat1", 
      "matrix 2" = "mat2",
      "matrix 3" = "mat3", 
      "matrix 4 " =  "mat4" )

shinyApp(
  ui =shinyUI(fluidPage(

    # Sidebar with a slider input for number of bins
    sidebarPanel(
      selectInput("pid", "1. Select the providers ", var, selected= "mat1", selectize = TRUE ),
      br(),
      #sliderInput("bins", "2, Select the number of BINs for Map", min = 5, max = nrow(get(var$input)), value=10)
      # radioButtons("color", "3, Select the color of histogram", choices =c("Green", "Red", "Yellow"), selected= "Green")
    ),
    # Show a plot of the generated distribution
    mainPanel(
      tabsetPanel(type = "tabs", 
                  tabPanel('Leaflet Map', leafletOutput("leaflet_map")),
                  tabPanel('Circular Map', leafletOutput("circular_map"))
      )))),
  server = shinyServer(function(input, output) {
    output$leaflet_map <- renderLeaflet({
      maptab <- get(input$pid)
      m <- leaflet() %>%
        addTiles() %>%  # Add default OpenStreetMap map tiles
        addMarkers(lng=maptab[,'long'][1:input$bins], lat=maptab[,'lat'][1:input$bins],popup=maptab[,'name'])
      m  # Print the map
    })
    ### Top Doctors circular map
    output$circular_map <- renderLeaflet({
      maptab <- get(input$pid)
      m = leaflet() %>% addTiles()
      df = data.frame(
        lng=maptab[,'long'][1:input$bins], 
        lat=maptab[,'lat'][1:input$bins],
        size = runif(input$bins, 5, 20),
        category = factor(maptab[,'state']),
        color = sample(colors(), input$bins)
      )
      m = leaflet(df) %>% addTiles()
      m %>% addCircleMarkers(radius = runif(input$bins, 4, 10), color = c('red','blue','green'))
    })
  }) 

)

1 个答案:

答案 0 :(得分:1)

您需要在server.R的renderUI({})部分中使用shinyServer({})来创建依赖于在服务器进程中创建的对象的控制器,然后在正确的位置使用uiOutput() ui.R显示该控制器。因此,使用您的脚本,这将嵌套在shinyServer中与您的其他输出生成步骤相同的级别:

output$setSlider <- renderUI({

    sliderInput("bins", "2, Select the number of BINs for Map",
        min = 5, max = nrow([selected data frame]), value=10)

})

然后这将进入ui.R,其中滑块控制代码现在是:

uiOutput("setSlider")