I am trying to create a shiny app where I can select the value that I want to plot into my map. It might be that this is a very easy task but I am new in Shiny and I can't figure it out.
I don't understand whether I have to use selectInput or if leaflet has a different way to interact with Shiny.
This is my code
library(RColorBrewer)
library(rgdal)
library(shiny)
pal <- colorQuantile("YlGn", NULL, n = 5)
state_popup <- paste0("<strong>State: </strong>",
africa$COUNTRY,
"<br><strong>Resource Revenue(IMF)</strong>",
africa$Africa_r_1)
leaflet_africa <- leaflet(data = africa) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(fillColor = ~pal(Africa_r_1),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = state_popup)
ui <- fluidPage(
leafletOutput("map1")
)
server <- function(input, output, session) {
output$map1 <- renderLeaflet({
leaflet_africa
})
}
shinyApp(ui = ui, server = server)
Related
I am trying to plot the word cloud of sub-categories of that state in popup window when user clicks on that state in map.
ui <- bootstrapPage(
leafletOutput("mymap", height = 300)
)
server <- function(input, output, session){
output$mymap <- renderLeaflet({
leaflet(Sales) %>%
addTiles() %>%
addCircles(lng = ~longitude, lat = ~latitude,
popup= popupGraph(p),
weight = 3,
radius = ~Sales,
color=~cof(newdata$Category), stroke = TRUE, fillOpacity = 0.8)) )
})
shinyApp(ui = ui, server = server)
Here is my leaflet code, I have used it in r shiny. Can anyone please suggest how i render wordcloud on popup when user clicks on a state on map? I don't know how write that function 'p' for word-cloud in popupGraph(p) but i want to pass it as an argument like this, where p is word-cloud
It's been a while since i'm having this problem. My current shiny app is not able to communicate with the user selection. It is supposed to show a colour fill variation across different region when a user makes a selection. However, it seems like the map is not showing it. My best guess is that it is not reading the 'pal' function correctly, and not able to capture the selection that the user input.
any kind of help would be great.
library(datasets)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(leaflet)
library(leaflet.extras)
library(Matrix)
library(readxl)
library(rgdal)
library(shiny)
library(stringr)
library(tidyverse)
library(tidyr)
library(RColorBrewer)
cf <- read.csv("datafile.csv")
sgmap55 <-readOGR("shapefile.kml")
bins <-c(1,50,100,150,200)
pal <- colorBin("Blues", domain = NULL, bins = bins, na.color = "#808080")
#5) setting for the labels.
labels <- sprintf(
"<strong>%s</strong><br/>%g respondents </sup>",
cf$planarea, cf$planarea)%>% lapply(htmltools::HTML)
##Section C: ShinyApp starts here
ui <- fluidPage(
titlePanel("Brand Interaction with Regions"),
sidebarLayout(
sidebarPanel(
radioButtons("brand", "Select First Brand:", choices = colnames(cf[,c(3,4,5,6,7,8,9,10,11,12,13,14)]))),
mainPanel(
leafletOutput("sgmap2")
)
)
)
server <- function(input, output, session) {
output$sgmap2 <- renderLeaflet({
selected_brand <- input$brand
leaflet() %>%
addTiles() %>%
addResetMapButton()%>%
clearMarkers()%>%
addProviderTiles("OpenStreetMap") %>%
setView(103.8198,1.3521,11)%>%
addPolygons(data = sgmap55,
highlight = highlightOptions(
weight = 5,
color = "#666666",
fillOpacity = 0.7,
fillColor = pal(input$selected_brand),
bringToFront = TRUE))
})
}
shinyApp(ui = ui, server = server)
manage to solve it with below code in between the server portion:
req(input$brand)
cpop <- cf[[input$brand]]
I am still working on this R Leaflet self project to learn and I'm trying to color in some Polygons in the Wake County area of Raleigh, NC. Below is the image of what I am trying to color.
https://imgur.com/a/xdvNLvM
Basically I am trying to get each of those polygons colored differently. I've tried addPolygons but I guess I didn't have correct Polygon data. I've looked at color binning but I seem to be out of ideas. Below is my code. I even tried to unnest the GeoJSON data and create a factor palette but that hasn't seemed to work.
library(shiny)
library(leaflet.extras)
library(geojsonio)
library(rgdal)
dataurl <- 'https://opendata.arcgis.com/datasets/f5c3b84a6fcc499d8f9ece78602258eb_0.geojson'
data <- geojson_read(dataurl, method = 'web', parse = FALSE, what = 'list')
wake <- readOGR(dataurl)
wake$zips <- factor(sample.int(39L, nrow(wake), TRUE))
#bikedata <- 'D:/bicycle-crash-data-chapel-hill-region.geojson'
#bike <- geojson_read(bikedata)
vtdata <- 'http://geodata.vermont.gov/datasets/4c206846699947429df59c8cb552ab5c_11.geojson'
vt <- geojson_read(vtdata)
factpal <- colorFactor(topo.colors(39), wake$zips)
ui <- shinyUI(
fluidPage(
leafletOutput("map", width = "100%", height = "900px")
)
)
server <- function(input, output) {
wakegeojson <- reactive({
data
})
#bikegeojson <- reactive({
# bike
#})
vtgeojson <- reactive({
vt
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(-93.65, 42.0285, zoom = 4)
})
observe({
leafletProxy("map") %>%
addWMSTiles("http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
layers = "nexrad-n0r-900913",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "") %>%
addGeoJSON(wakegeojson(), weight = 3, fill = factpal) %>%
#addGeoJSON(bikegeojson()) %>%
addGeoJSON(vtgeojson(), fill = FALSE, color = "black")
})
}
app <- shinyApp(ui = ui, server = server)
runApp(app, launch.browser = TRUE)
I think I need to explore the addPolygons feature more but I'm not exactly sure how to do that or how to parse/unnest my GeoJSON data in order to accomplish filling in the Wake County Zipcodes with different colors. Any help is always appreciated. Thank you.
I would switch to sf. You can directly load the geojson and produce a Multipolygon and a Multilinestring object which will also read much faster than readOGR.
Then you can just put those objects in addPolygons and addPolylines.
The following example should work:
library(shiny)
library(leaflet.extras)
library(geojsonio)
library(rgdal)
library(sf)
dataurl <- 'https://opendata.arcgis.com/datasets/f5c3b84a6fcc499d8f9ece78602258eb_0.geojson'
wake <- st_read(dataurl)
wake$zips <- factor(sample.int(39L, nrow(wake), TRUE))
vtdata <- 'http://geodata.vermont.gov/datasets/4c206846699947429df59c8cb552ab5c_11.geojson'
vt <- st_read(vtdata)
factpal <- colorFactor(topo.colors(39), wake$zips)
ui <- shinyUI(
fluidPage(
leafletOutput("map", width = "100%", height = "900px")
)
)
server <- function(input, output) {
wakegeojson <- reactive({
wake
})
vtgeojson <- reactive({
vt
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data=wakegeojson(), color=factpal(wake$zips)) %>%
addPolylines(data=vtgeojson(), color="red")
})
}
app <- shinyApp(ui = ui, server = server)
runApp(app, launch.browser = TRUE)
I am building a shiny app where I would like to get the coordinates of a polygon from a leaflet map. Specifically, the shape is drawn using the Drawtoolbar from the leaflet.extras package. A simple example app is below.
My question is, how can I get the coordinates from the shape drawn on the map by the user? Thank you in advance.
library(shiny)
library(leaflet)
library(leaflet.extras)
# Define UI
ui <- fluidPage(
leafletOutput("mymap",height=800)
)
# Define server logic
server <- function(input, output) {
output$mymap <- renderLeaflet(
leaflet() %>%
addProviderTiles("Esri.OceanBasemap",group = "Ocean Basemap") %>%
setView(lng = -166, lat = 58.0, zoom = 5) %>%
addDrawToolbar(
targetGroup='draw',
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addLayersControl(overlayGroups = c('draw'), options =
layersControlOptions(collapsed=FALSE))
)
observeEvent(input$mymap_shape_click,{
print(input$mymap_shape_click)
})
observeEvent(input$mymap_click,{
print(input$mymap_click)
})
}
# Run the application
shinyApp(ui = ui, server = server)
You need to observe the _draw_new_feature function
library(leaflet.extras)
# Define UI
ui <- fluidPage(
leafletOutput("mymap",height=800)
)
# Define server logic
server <- function(input, output) {
output$mymap <- renderLeaflet(
leaflet() %>%
addProviderTiles("Esri.OceanBasemap",group = "Ocean Basemap") %>%
setView(lng = -166, lat = 58.0, zoom = 5) %>%
addDrawToolbar(
targetGroup='draw',
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addLayersControl(overlayGroups = c('draw'), options =
layersControlOptions(collapsed=FALSE))
)
observeEvent(input$mymap_draw_new_feature,{
feature <- input$mymap_draw_new_feature
print(feature)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I am creating a shiny application for DBSCAN of data consisting of geographical coordinates and use leaflet for showing these clusters on the map. My code is following.
ui <- fluidPage(
pageWithSidebar(
headerPanel('clustering of Data'),
sidebarPanel(
numericInput('eps', 'Please enter Eps value', 0.05),
numericInput('minpts', 'Please enter MinPts value', 10),
mainPanel(
leafletOutput('plot2')
)
)
)
server <- function(input, output, session) {
# Combine the selected variables into a new data frame
pp <- reactive({
dbscan(select(data, lat, lon), eps = input$eps, MinPts = input$minpts)
})
selectedData <- reactive({
data
})
output$plot2 <- renderLeaflet(
{
leaflet() %>%
addTiles() %>%
setView(lng = -93.232256, lat = 44.973692, zoom = 10) %>%
addMarkers(lat = as.numeric(selectedData()$lat),
lng = as.numeric(selectedData()$lon), color = color
)
}
)
}
shinyApp(ui = ui, server = server)
I guess I can perform the DBSCAN and get clusters, but I am not able to show these clusters with different color on the map using leaflet. Please help