ramazon-ec2shinyr-leaflet

How to select input matrix dynamically?


I am trying to create a shiny app to generate dynamic maps. I want to use selectInput function to select matrix dynamically. I will integrate these maps in a Rmarkdown file where several apps are already there.

I am using Amazon ec2 Ubuntu machine for hosting my shiny apps and rstudio. All the working apps are at /srv/shiny-server.

I am getting following error:
Error

Error in maptab[, "long"] : incorrect number of dimensions Error in data.frame(lng = maptab[, "long"], lat = maptab[, "lat"], category = factor(maptab[, : object 'maptab' not found

Code

### Top Doctors Map Leaflet
library(shiny)
library(leaflet)

##```{r, echo=FALSE, warning=FALSE, message=FALSE}
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

shinyApp(
  ui =shinyUI(fluidPage(

    # Sidebar with a slider input for number of bins
    sidebarPanel(
      selectInput("var", "1, Select the variables from top doc summary file", 
                  choices =c( "top_docs" = 1, 
                              "docs" = 2,
                              "orgs" = 3, 
                              "phys_summ" = 4, 
                              "pm" = 5), selected= 1 ),
      br(),
      sliderInput("bins", "2, Select the number of BINs for Map", min = 40, max = 100000, value=20),
      br(),
      # 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 <- input$var
      m <- leaflet() %>%
        addTiles() %>%  # Add default OpenStreetMap map tiles
        addMarkers(lng=maptab[,long], lat=maptab[,lat],popup=maptab[,name])
      m  # Print the map
    })
    output$circular_map <- renderLeaflet({
      ### Top Doctors circular map

      m = leaflet() %>% addTiles()
      df = data.frame(
        lng=maptab[,'long'], 
        lat=maptab[,'lat'],
        # size = runif(40, 5, 20),
        category = factor(maptab[,'state']),
        color = sample(colors(), 40)
      )
      m = leaflet(df) %>% addTiles()
      m %>% addCircleMarkers(radius = runif(40, 4, 10), color = c('red','blue','green'))
    })
  }), 
  options = list(height = 480, width = 1050, dpi=200)

)
##

Sample Data input

library(data.table)
top_docs = data.table(long = runif(40, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) 
docs = data.table(long = runif(9123, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) 
orgs = 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) 
pm = data.table(long = runif(99999, -87, -80), lat = runif(40, 25, 42), name = letters, state= letters) 

Solution

  • Finally, I figured it out. Thanks @NicE for your input.

    The error was due to not using get for character input and incorrect selectInpud id assignment. The revised working code:

    {r, echo=FALSE, message=FALSE}
    options(warn=-1)
    library(shiny)
    library(leaflet)
    # {r, echo=FALSE, warning=FALSE, message=FALSE}
    r_colors <- rgb(t(col2rgb(colors()) / 255))
    names(r_colors) <- colors()
    var=c("Top Doctors" = "top_docs", 
          "Doctors" = "docs",
          "Provider Orgs." = "orgs", 
          "All Providers" =  "pm" )
    
    shinyApp(
      ui =shinyUI(fluidPage(
    
        # Sidebar with a slider input for number of bins
        sidebarPanel(
          selectInput("pid", "1. Select the providers ", var, selected= "top_docs", selectize = TRUE ),
          br(),
          sliderInput("bins", "2, Select the number of BINs for Map", min = 40, max = 100000, value=20)
          # 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'))
    })
    

    }) # ,options = list(height = 480, width = 1050, dpi=200)

    )