The following code produces a shiny app with (almost) the same output twice. One uses package "rcharts", the other package "leaflet"
The first map has a fullsreen button. Is this available with package leaflet()?
library(shiny)
library(rCharts)
library(leaflet)
runApp(
## UI ####
list(ui = (basicPage(
headerPanel("tests"),
mainPanel(
chartOutput("map1", "leaflet"),
"some text...",
leafletOutput('map2')
)
)),
## server ####
server = function(input, output) {
output$map1 <- renderMap({
map1 <- Leaflet$new()
map1$fullScreen(TRUE)
map1$setView(c(39.603609, -8.415081), 10)
map1
})
output$map2 <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = -8.415081, lat = 39.603609, zoom = 10)
})
}
))
Thanks
For future readers
With the package leaflet.extras you can add a full screen control to your map.
library(leaflet)
library(leaflet.extras)
leaflet() %>%
addTiles() %>%
addFullscreenControl()
There is a plugin for the Leaflet javascript library called Leaflet.Control.FullScreen.
However, this plugin hasn't (yet) been implemented in the R version of leaflet. I have posted a feature request on the leaflet Github page, but haven't heard anything back so far.
Related
I'm currently using addSearchOSM() from the leaflet.extras package to search addresses:
How can I change the colour of the circle marker? Will accept CSS solutions as well - I attempted to manually update the .leaflet-interactive{} css, but that changes all interactive elements, including polygons.
Reproducible example here:
library(shiny)
library(leaflet)
library(tidyverse)
library(leaflet.extras)
ui <- fluidPage(
fluidRow(
column(
width = 12,
leafletOutput("map")
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(
lng = -73.9888,
lat = 40.72905,
zoom = 12
) %>%
addSearchOSM()
})
}
# Run the application
shinyApp(ui = ui, server = server)
I would like to add a logo to a leaflet map in a shiny app.
The addLogo function of the leafem package allows this, when I generate a map outside the shiny environment the function works perfectly, however, when applying the function in shiny it does not work.
I do not know what I can be obviating or if there is another way to do it.
├── app.R
└── www
└── Logo.png
library(leaflet)
library(shiny)
library(leafem)
ui <- fluidPage(
leafletOutput("map")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = -79.442471,
lat = 43.6857,
zoom = 12) %>%
addLogo("Logo.png",
src= "local")
})
}
shinyApp(ui, server)
Use src= "remote" in addLogo. Even though the Shiny app and image is in your local computer, you need to use it as remote. Using local will point to ../graphs/Logo.png instead of only Logo.png (that is the default for files under the www directory).
library(leaflet)
library(shiny)
library(leafem)
ui <- fluidPage(
leafletOutput("map")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = -79.442471,
lat = 43.6857,
zoom = 12) %>%
addLogo("Logo.png",
src= "remote")
})
}
shinyApp(ui, server)
I am very new to shiny and the mapping function.
Based on the code of the link bellow, I did a simple one, which does not work.
Can someone tell me why is not working?
There is no error shown. However, it doesn't show the map.
I am working with the dataset of NYC bikes.
library(shiny)
library(leaflet)
bikes <- read.csv("Data/201501-citibike-tripdata.csv")
ui <- fluidPage(
leafletOutput("mymap")
)
server <- function(input, output) {
output$mymap <- renderLeaflet({
leaflet(bikes) %>%
setView(lng = -73.98928, lat = 40.75042, zoom = 10) #NYC
})
}
shinyApp(ui=ui, server = server)
link: http://rstudio-pubs-static.s3.amazonaws.com/133599_c0d5471268584d47b53298f0ad27e8d3.html
I want to sync two maps in an R shiny web app(zooming in on one map should zoom in on the other map and panning etc), I managed to do this interactively using the code shown below but I can't figure out how to do this in a Shiny web app. Any help would be appreciated
my_map <- function(x){
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=x[1], lat=x[2], popup="The birthplace of R")
m
}
y <- c(174.968, 37.852)
x <- c(0.112281, 51.523001)
sync(my_map(x), my_map(y), no.initial.sync = TRUE)
Using sync() as an UI output, not as leafletOutput worked for me.
In ui:
uiOutput("synced_maps")
In server:
output$synced_maps <- renderUI({
m1 <- leaflet() %>% addTiles() %>% addMarkers(~lon1, ~lat1)
m2 <- leaflet() %>% addTiles() %>% addMarkers(~lon2, ~lat2)
sync(m1, m2)
})
Will the maps always be made prior to user interface creation? If so:
library(leaflet)
library(leafsync)
library(shiny)
my_map <- function(x){
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=x[1], lat=x[2], popup="The birthplace of R")
m
}
y <- c(174.968, 37.852)
x <- c(0.112281, 51.523001)
ui <- sync(my_map(x), my_map(y), no.initial.sync = TRUE)
server = function(input,output){
}
shinyApp(ui, server)
Edit:
In response to your comment, I have looked at options to sync maps including a javascript approach (https://github.com/jieter/Leaflet.Sync) and syncWith (https://github.com/rte-antares-rpackage/leaflet.minicharts). I have not spent time with these.
A quick workaround could be this below (one map's bounds matches the other map's bounds, but not vice versa). It requires adding observe to the server function and setting the bounds from one map to the other. From http://rstudio.github.io/leaflet/shiny.html:
input$MAPID_bounds provides the latitude/longitude bounds of the
currently visible map area; the value is a list() that has named
elements north, east, south, and west
library(leaflet)
library(leafsync)
library(shiny)
my_map <- function(x){
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=x[1], lat=x[2], popup="The birthplace of R")
m
}
y <- c(174.968, 37.852)
x <- c(0.112281, 51.523001)
ui <- fluidPage(
leafletOutput("mymap1"),
leafletOutput("mymap2")
)
server = function(input, output){
output$mymap1 = renderLeaflet({
my_map(x)
})
output$mymap2 = renderLeaflet({
my_map(y)
})
observe({
coords <- input$mymap1_bounds
if (!is.null(coords)) {
leafletProxy("mymap2") %>%
fitBounds(coords$west,
coords$south,
coords$east,
coords$north)
}
})
}
shinyApp(ui, server)
I am trying to create an interactive webmap in R to display locations using Shiny and Leaflet
The idea is that the user selects one input and the markers corresponding to that input(lat/long which are to be fetched from data set of the corresponding input) are displayed in a Leaflet map (with zoom in/out function).
Any help/advice would be greatly appreciated!
(sample data file uploaded here):
enter code here
Server.R
library(shiny)
library(rpart.plot)
library(leaflet)
shinyServer(
function(input, output) {
output$dtmplot <- renderPlot({
dtmplot <- rpart.plot(dtm, type=4, extra=101)
})
observe({
output$map <- renderLeaflet( {
for(j in 1:nrow(df))
{
if(df[j, "col1"]==input$input1) {
map <- leaflet() %>%
addTiles() %>%
addMarkers(lng=df[j,"Longitude"], lat=df[j,"Latitude)
}
}
})
})
}
)
enter code here
UI.R
library(shiny)
library(leaflet)
shinyUI(
pageWithSidebar(
headerPanel("Sample project"),
sidebarPanel(
plotOutput("dtmplot"),
selectInput("input1",
label = "label1:",
choices = c(“choice1”,”choice2”),
selected = " choice1"),
sliderInput("slider","Please select slider input", min=1,max=100,value=20,step=10)
),
mainPanel(
leafletOutput("map")
)
))
The basic code to handle custom points in a leaflet map is available below. The code utilises the official example available on the leaflet GitHub and provided end-user with the functionality to display custom location on the map.
app.R
library(shiny)
library(leaflet)
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
ui <- fluidPage(
leafletOutput("mymap"),
p(),
h1("Added example to add more points here:"),
p(),
numericInput("long", label = h3("Longitude:"), value = 11.242828),
numericInput("lat", label = h3("Latitude:"), value = 30.51470),
actionButton("recalc", "Show point")
)
server <- function(input, output, session) {
points <- eventReactive(input$recalc, {
cbind(input$long, input$lat)
}, ignoreNULL = FALSE)
output$mymap <- renderLeaflet({
leaflet() %>%
setView(lat = 30, lng = 11, zoom = 4) %>%
addProviderTiles("Stamen.TonerLite",
options = providerTileOptions(noWrap = TRUE)
) %>%
addMarkers(data = points())
})
}
shinyApp(ui, server)
Results
The obtained map looks like that:
Explanation
The mechanics is fairly simple and can be summarised in the following steps:
You need to pas lat and lon to your map to addMarkers. In my example this is done via primitive input files but it can be done in a number of ways.
You have to decide on the logic of dynamically adding markers to your map; in the presented case this is done with use of an actionButton.
Side notes
As at the time of drafting this answer there was no clarity with respect to the actual data that should be represented on the map, I found it more informative to generate the desired functionality following the official example instead of trying to modify the provided code.
The thing worth noting is that the lat/lon values have to be of correct format to appear on the map.
The map setView to make the example more presentable but in an actual solution, default lat/lon values should be generated dynamically.