wide data format as input to a Shiny mapview app - reactive wrapper(s) needed?

dbo picture dbo · Jun 26, 2019 · Viewed 17.6k times · Source

I'm having trouble using columns from data originating in wide format as dynamic inputs to a Shiny map app.

In the app I'm hoping to be able to:

  • select a parameter of point data (sample data below: 16 locations, 6 parameters) in a drop down type menu and adjust the symbol size to represent the selected parameter's absolute values with a slider (to help visualize positive and negative differences from zero)
  • with any parameter selected, retain ability to see all parameters (the columns) in mapview's popup feature (mapview turns the columns into rows for the popup). It seems a filtered long format data.frame would be missing data from the popup/viewing perspective
  • retain the (non absolute) original value on the mouseover hover label (eg the -7.3 in the image)

In addition to having those features, I don't know if/where I need to set reactive wrapper(s)? Or, maybe I could do everything more easily with another map-centric library (even though mapview is awesome for many things)?

My attempts are commented out below - the UI works as intended except without drop down selectability - the app is limited to only one working dropdown parameter with mapview(df["param1"] and cex = param1 * input$cex.

Here's the reproducible app.r:

library(tidyverse)
library(sf)
library(shiny)
library(shinydashboard)
library(leaflet)
library(mapview)

## sample earthquake data ##
set.seed(6)
lat <- rnorm(16,-34, 9)
lon <- rnorm(16,-67,.3)
param1 <- rnorm(16, 10, 40)   %>% round(1)
param2 <- rnorm(16, 25, 3)    %>% round(1)
param3 <- rnorm(16, -18, 10)  %>% round(1)
param4 <- rnorm(16, -200, 93) %>% round(1)
param5 <- rnorm(16, 0.1, .09) %>% round(1)
param6 <- rnorm(16, 417, 33)  %>% round(1)
df <- data.frame(lat, lon, param1, param2, param3, param4, param5, 
param6) 
df <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326) 
paramchoices <- colnames(df) %>% .[.!="geometry"]
colorpal = mapviewPalette("mapviewSpectralColors") 

ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(  
sliderInput("cex", "Symbol Size",
            min = 0.000001, max = 10, value = 1, step = 0.000001
),
selectizeInput(

  "parameter", "Earthquake Parameter", choices = paramchoices, 
  selected = c("param1"), 
  multiple = FALSE)
),
dashboardBody(
tags$style(type = "text/css", "#mapplot {height: calc(100vh - 80px) !important;}"),
leafletOutput("mapplot")
)
)

server <- function(input, output) {

# df <- reactive ({
#   df %>% mutate(selectedparameter = input$parameter,
#                 selectedparameter_abs = abs(selectedparameter))
# })

output$mapplot <- renderLeaflet({
               m <-   mapview(df["param1"], #mouseover column
               #m <- mapview(df["selectedparameter"],
               cex = param1 * input$cex,  #marker size column
               #cex = df$selectedparameter_abs * input$cex, 
               col.regions = colorpal(100), 
               alpha.regions = 0.3,
               legend = TRUE,
               popup = popupTable(df),
               layer.name =  "selectedparam[unit]") 
 m@map     
}
)}
shinyApp(ui, server)

enter image description here

more info related to the absolute value part - Point color and symbol size based on different variables in mapview

thank you.

Answer