I am self-taught in R and computer coding, so I apologize if this question is poorly worded or already answered elsewhere.
I am trying to plot a reactive dataset in editMod, while retaining the ability to use the edit (draw) functions provided by editMod. So far I have not found a way to plot reactive data using editMod.
Below is a reproducible example using the supplementary "quakes" dataset. In the below map, I can generate summary data based on a reactive dataset selected by the slider bar, but the reactive dataset is not reflected in what is plotted on the map.
library(mapview)
library(leaflet)
library(shiny)
library(ggplot2)
library(mapedit)
library(leaflet.extras)
library(sf)
ui.q <- fluidPage(
# Application title
titlePanel("Quake data exploration"),
sidebarLayout(
sidebarPanel(
sliderInput("range",
label = "Range of earthquake magnitude:",
min = 4.0, max = 6.4, value = c(4.0, 6.4), step = 0.1)
),
mainPanel(
fluidRow(
editModUI("editor", height = 860),
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = 120, left = "auto", right = 20, bottom = "auto",
width = 400, height = "auto",
h4("Summary data"),
plotOutput("plot")))
)
)
)
# Define server logic
server.q <- function(input, output){
#create a sf of the quake data.
quake_mx <- data.matrix(quakes[,2:1])
quake_mp <- st_multipoint(quake_mx)
quake_sf <- st_sf(st_cast(st_sfc(quake_mp), "POINT"), quakes, crs=4326)
#trying to subset the quake_sf data by the range of magnitude inputted by the slider bar.
#this creates a reactive dataframe and I can't seem to get it to be reflected in the editMod output.
#I can get this to work when I use leafletProxy but then we lose the ability to draw.
datasetMag <- reactive({
quake_sf = subset(quake_sf, mag %in% c(input$range[1]:input$range[2]))
})
#generate the leaflet map
lf.quakes <- leaflet(options = leafletOptions(minZoom = 4, maxZoom = 10)) %>%
addTiles() %>%
addProviderTiles("Esri.OceanBasemap",group="OceanBasemap") %>%
addProviderTiles("Esri.WorldImagery",group="WorldImagery") %>%
addCircleMarkers(data = quake_sf,
color = "red",
weight = 1,
fillOpacity = 0.7,
popup = popupTable(quake_sf, zcol = "mag"),
radius = quake_sf$mag) %>%
addLayersControl(baseGroups = c("OceanBasemap","WorldImagery"),
options = layersControlOptions(collapsed = FALSE))
#call the editMod function from 'mapedit' to use in the leaflet map. I think this may be the line where I am going wrong
edits <- callModule(editMod, "editor", leafmap = lf.quakes)
#generate the reactive dataset based on what is drawn on the leaflet map
datasetInput <- reactive({
mag.range = c(input$range[1]:input$range[2])
req(edits()$finished)
quake_intersect <- st_intersection(edits()$finished, quake_sf)
df <- data.frame(quake_intersect)
df = df[df$mag %in% mag.range,] #make the dataset reactive to the slider input range
})
#render a histogram with the reactive dataset as the output
output$plot <- renderPlot({
hist(datasetInput()$mag, col = "grey", border = "black")
})
}
shinyApp(ui.q, server.q)