I would like the icons on a leaflet map to be linked to the correspondent trace on a plotly line plot in a shiny app. Once I click on an icon, only the line with the same id should be displayed in plotly. Is this possible? I have been trying with crosstalk but I must be missing something.
library(shiny)
library(leaflet)
library(plotly)
library(crosstalk)
tmp1 <- data.frame(Date = seq(as.POSIXct("2016-06-18 10:00"),
length.out = 10, by = "mins"),
Temp = rnorm(n = 10, mean = 20, sd = 5),
lat=51.504162,
long=-0.130472,
id="first")
tmp2 <- data.frame(Date = seq(as.POSIXct("2016-06-18 10:00"),
length.out = 10, by = "mins"),
Temp = rnorm(n = 10, mean = 20, sd = 5),
lat=51.502858,
long= -0.116722,
id="second")
uktemp<-rbind(tmp1,tmp2)
#=========================================
ui <- fluidPage(
fluidRow(
column(6, leafletOutput("map")),
column(6, plotlyOutput("graph"))
)
)
server <- function(input, output, session) {
crossuktemp<- SharedData$new(uktemp)
output$map <- renderLeaflet({
leaflet(options = leafletOptions(minZoom = 15,maxZoom =18 ))%>%
addTiles()%>%
addCircles(data=crossuktemp,
lng= ~ long,
lat= ~ lat,
label=~id)
})
output$graph <- renderPlotly({
plot_ly(crossuktemp,x=~Date,y=~Temp, color =~id, mode="lines")%>%
layout(title = "",yaxis = list(title = "C°"),
xaxis = list(title = "Time")) %>%
highlight(off = "plotly_deselect")
})
}
shinyApp(ui, server)
I've hacked together a solution, making use of leaflets events it creates on the click.
ui <- fluidPage(
# add a reset button to undo click event
fluidRow(actionButton("reset", "Reset")),
fluidRow(
column(6, leafletOutput("map")),
column(6, plotlyOutput("graph"))
),
fluidRow()
)
server <- function(input, output, session) {
# create reactive data set based on map click
filteredData <- reactive({
event <- input$map_shape_click
if (!is.null(event)){
uktemp[uktemp$lat == event$lat & uktemp$long == event$lng,]
}
})
output$map <- renderLeaflet({
leaflet(options = leafletOptions(minZoom = 15,maxZoom =18 ))%>%
addTiles()%>%
addCircles(data=uktemp,
lng= ~ long,
lat= ~ lat,
label=~id)
})
# default graph
output$graph <- renderPlotly({
plot_ly(uktemp,x=~Date,y=~Temp, color =~id, mode="lines")%>%
layout(title = "",yaxis = list(title = "C°"),
xaxis = list(title = "Time")) %>%
highlight(off = "plotly_deselect")
})
# if clicked on map, use filtered data
observeEvent(input$map_click,
output$graph <- renderPlotly({
plot_ly(filteredData(),x=~Date,y=~Temp, color =~id, mode="lines")%>%
layout(title = "",yaxis = list(title = "C°"),
xaxis = list(title = "Time")) %>%
highlight(off = "plotly_deselect")
})
)
# if reset, then go back to main data
observeEvent(input$reset,
output$graph <- renderPlotly({
plot_ly(uktemp,x=~Date,y=~Temp, color =~id, mode="lines")%>%
layout(title = "",yaxis = list(title = "C°"),
xaxis = list(title = "Time")) %>%
highlight(off = "plotly_deselect")
})
)
}
To do so, have a read of these links
see the section: Inputs/Events
https://rstudio.github.io/leaflet/shiny.html
some SO questions
Click event on Leaflet tile map in Shiny
R shiny: reset plot to default state
To do undo the click event, I had to add a reset button in. Maybe there is a way of undoing a click in a more elegant way. I expect there are cleaner ways to build this if you read around it some more :)
Cheers,
Jonny
Related
In the relatively simple shiny application below I select a point on load. Once the user chooses a new number in the selector I'd like highcharter to select that point instead. In other words, if the user selects 1 then then it should select the 1st point.
Suggestions for how to do this?
library(shiny)
library(highcharter)
ui <- function(){
div(
selectInput('id', label = 'select', choices = 1:3, selected = 2),
highchartOutput("plot")
)
}
server <- function(session, input, output){
output$plot <- renderHighchart({
hc <- highchart() %>%
hc_chart(events = list(load = JS("function(){this.series[0].points[2].select()}"))) %>%
hc_add_series(data.frame(x = 1:3, y = 1:3), "scatter", hcaes(x, y)) %>%
hc_plotOptions(
allowPointSelect = TRUE
)
hc
})
observeEvent(input$id, {
# Here I'd like to send a message to the highchart
# to select the chosen point
})
}
shinyApp(ui, server)
This can be done using hcpxy_update_point function in the development version of {highcharter} (remotes::install_github("jbkunst/highcharter")).
Be sure to use the correct id for the chart which in this case is plot.
More examples in https://jbkunst.shinyapps.io/02-proxy-functions/.
library(shiny)
library(highcharter)
ui <- function(){
div(
selectInput('id', label = 'select', choices = 1:3, selected = 2),
highchartOutput("plot")
)
}
server <- function(session, input, output){
output$plot <- renderHighchart({
hc <- highchart() %>%
hc_chart(events = list(load = JS("function(){this.series[0].points[2].select()}"))) %>%
hc_add_series(
data.frame(x = 1:3, y = 1:3),
"scatter",
hcaes(x, y),
id = "someid",
) %>%
hc_plotOptions(
allowPointSelect = TRUE
)
hc
})
observeEvent(input$id, {
id_0_based <- as.numeric(input$id) - 1
highchartProxy("plot") %>%
# set all points unselected `selected = FALSE`
hcpxy_update_point(id = "someid", 0:2, selected = FALSE) %>%
# then set to selected the _selected_ point
hcpxy_update_point(
id = "someid",
id_point = id_0_based,
selected = TRUE
)
})
}
shinyApp(ui, server)
I've started using the Shiny package and I'm having problems with the reactive function. The goal is to create a map which shows the variable posdif per Belgian province, and which can be viewed reactively per sector (nace2d). I get a reactive map as output, but the numbers, colours and labels are not correct. I've played with the position of labels, bins and pals and tried to make them reactive, but nothing seems to work...
ui <- fluidPage(
sidebarLayout(position = 'right',
sidebarPanel(
selectInput("nace2d","Sector",choices = c("45",'46',"47"),selected = "45"),
width = 2),
mainPanel(leafletOutput("mymap",height = 650,width=605)))
)
# define server
server <- function(input,output, session){
labels <- sprintf(
"<strong>%s</strong><br/>%g jobcreatie",
projects.df$label, projects.df$posdif
) %>% lapply(htmltools::HTML)
bins <- c(0, 500,1000,1500,2000,2500,3000,3500,4000,4500,5000)
pal <- colorBin("YlOrRd", domain = projects.df$posdif, bins = bins)
df<-reactive({
projects.df%>%
dplyr::filter(nace2d %in% input$nace2d)
})
output$mymap <- renderLeaflet({
leaflet(data=df()) %>%
addTiles() %>%
addPolygons(
fillColor = ~pal(posdif),
label = labels,
labelOptions = labelOptions(
style = list("font-weight"))) %>%
addLegend(pal = pal, values = ~posdif, opacity = 0.7, title = NULL,
position = "bottomright")
})
}
shinyApp(ui = ui, server = server)
I am very new to shiny, and I have a question.
I have a simple dataset with observations (Number_Total) of species (Species), in a certain location (X,Y).
I would like to generate a map, that enables you to select the species in a dropdown menu. Shiny then shows you were the species occurs on the map.
I got pretty far (for my experience), but selecting species in the menu does not do anything...
ui <- (fluidPage(titlePanel("Species Checker"),
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE)
),
mainPanel(
leafletOutput("CountryMap",
width = 1000, height = 500))
)
))
The server side
server <- function(input, output, session){
output$CountryMap <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(lng = 10, lat = 40, zoom = 5) %>%
addCircles(lng = df$Y, lat = df$X, weight = 10,
radius =sqrt(df$Number_Total)*15000, popup = df$Species)
})
observeEvent(input$species, {
if(input$species != "")
{
leafletProxy("CountryMap") %>% clearShapes()
index = which(df$Species == input$species)
leafletProxy("CountryMap")%>% addCircles(lng = df$X[index],
lat = df$Y[index],
weight = 1,
radius =sqrt(df$Number_Total[index])*30, popup = df$Species[index])
}
})
}
And finally plot it
shinyApp(ui = ui, server = server)
I know my code is probably messy, but again, I blaim my experience =)
I did not manage to get an example dataset in here right away, so here it comes as picture
This is the result of the above code (with slightly different data)
enter image description here
Here's what you need. I think you are skilled enough to understand this but comment if you have any questions.
server <- function(input, output, session) {
# map_data <- reactive({
# req(input$species)
# df[df$Species %in% input$species, ]
# })
output$CountryMap <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(lng = 10, lat = 40, zoom = 5)
})
map_proxy <- leafletProxy("CountryMap")
observe({
md <- df[df$Species %in% input$species, ]
map_proxy %>%
addCircles(lng = md$Y, lat = md$X, weight = 10,
radius = sqrt(md$Number_Total)*15000, popup = md$Species)
})
}
I am trying out the R Streaming example for extendTraces on Plotly. I am trying to add a functionality to the chart such that it would clear all the data as the browser starts stalling after some time (eg., an actionButton, etc). Is there a way to stop the trace and clear the trace/data on a second click of the actionButton ? Alternatively, is it possible to make the chart incremental, such that the entire data isn't getting stored locally.
https://plot.ly/r/streaming/#streaming-in-r
library(shiny)
library(plotly)
rand <- function() {
runif(1, min=1, max=9)
}
ui <- fluidPage(
includeCSS("styles.css"),
headerPanel(h1("Streaming in Plotly: Multiple Traces", align = "center")),
br(),
div(actionButton("button", "Extend Traces"), align = "center"),
br(),
div(plotlyOutput("plot"), id='graph')
)
server <- function(input, output, session) {
p <- plot_ly(
type = 'scatter',
mode = 'lines'
) %>%
add_trace(
y = c(rand(),rand(),rand()),
line = list(
color = '#25FEFD',
width = 3
)
) %>%
add_trace(
y = c(rand(),rand(),rand()),
line = list(
color = '#636EFA',
width = 3
)
) %>%
layout(
yaxis = list(range = c(0,10))
)
output$plot <- renderPlotly(p)
observeEvent(input$button, {
while(TRUE){
Sys.sleep(1)
plotlyProxy("plot", session) %>%
plotlyProxyInvoke("extendTraces", list(y=list(list(rand()), list(rand()))), list(1,2))
}
})
}
shinyApp(ui, server)
Thanks in advance,
Raj.
Hi maybe you could do something like this?
library(shiny)
library(plotly)
library(shinyjs)
rand <- function() {
runif(1, min=1, max=9)
}
ui <- fluidPage(
# includeCSS("styles.css"),
headerPanel(h1("Streaming in Plotly: Multiple Traces", align = "center")),
br(),
div(actionButton("button", "Extend Traces"),actionButton("buttonReset", "Reset Traces"), align = "center"),
br(),
div(plotlyOutput("plot"), id='graph'),
useShinyjs()
)
server <- function(input, output, session) {
values <- reactiveValues()
values$p <- plot_ly(
type = 'scatter',
mode = 'lines'
) %>%
add_trace(
y = c(rand(),rand(),rand()),
line = list(
color = '#25FEFD',
width = 3
)
) %>%
add_trace(
y = c(rand(),rand(),rand()),
line = list(
color = '#636EFA',
width = 3
)
) %>%
layout(
yaxis = list(range = c(0,10))
)
output$plot <- renderPlotly({values$p})
observe({
invalidateLater(1000, session)
req(input$button > 0)
plotlyProxy("plot", session) %>%
plotlyProxyInvoke("extendTraces", list(y=list(list(rand()), list(rand()))), list(1,2))
})
observeEvent(input$buttonReset,{
values$p <- plot_ly(
type = 'scatter',
mode = 'lines'
) %>%
add_trace(
y = c(rand(),rand(),rand()),
line = list(
color = '#25FEFD',
width = 3
)
) %>%
add_trace(
y = c(rand(),rand(),rand()),
line = list(
color = '#636EFA',
width = 3
)
) %>%
layout(
yaxis = list(range = c(0,10))
)
runjs("Shiny.onInputChange('button',0)")
})
}
shinyApp(ui, server)
Hope this helps!!
Is there any way to select or highlight data on a leaflet map in by clicking on the legend in Rshiny?
example code:
library(shiny)
library(leaflet)
library(RColorBrewer)
library(leafletGeocoderRshiny)
ui <- fluidPage(
leafletOutput("map"),
p(),
actionButton("recalc", "New points")
)
server <- function(input, output, session) {
df = data.frame(x = rnorm(100), y = rexp(100, 2), z = runif(100))
pal = colorBin('PuOr', df$z, bins = c(0, .1, .4, .9, 1))
output$map <- renderLeaflet({ leaflet(df) %>%
addCircleMarkers(~x, ~y, color = ~pal(z)) %>%
addLegend(pal = pal, values = ~z)
})
}
shinyApp(ui, server)
The recent updates (0.2) to the mapedit package may help: http://r-spatial.org/r/2017/06/09/mapedit_0-2-0.html
I got close, but ran out of time now. But i decided to share anyway, maybe someone else sees a solution to the last step.
So far it works for the first click on any of the rectangles in the legend. It doesnt work for any following clicks as the map is redrawn and with it the onclick listeners are deleted. I didnt find a way to add them again so far,...
Its a hacky aprroach: I add onclick listener to the boxes and decided to update the colors via R as i didnt see a good way in JS.
library(shiny)
library(leaflet)
library(RColorBrewer)
library(leafletGeocoderRshiny)
library(shinyjs)
colors <- c("#000000", "#222222", "#888888", "#FFFFFF")
ui <- fluidPage(
useShinyjs(),
leafletOutput("map"),
p(),
actionButton("recalc", "New points")
)
server <- function(input, output, session) {
global <- reactiveValues(colors = colors,
bins = c(0, .1, .4, .9, 1))
observe({
print(input$interval)
isolate({
if(!is.null(input$interval)){
lowerBound <- as.numeric(unlist(input$interval))
global$colors <- colors
global$colors[which(global$bins == lowerBound)] <- "#FF0000"
}
})
})
session$onFlushed(function() {
runjs("
var legendButton = document.getElementsByTagName('i')
var elem; var interval;
for (nr = 0; nr < legendButton.length; nr++) {
elem = legendButton[nr]
elem.onclick = function(e){
console.log(e.target)
interval = e.target.nextSibling.nodeValue.split(' ');
Shiny.onInputChange('interval', interval[1]);
}
}
")
})
df = data.frame(x = rnorm(100), y = rexp(100, 2), z = runif(100))
pal = reactive({
colorBin(global$colors, df$z, bins = global$bins)
})
output$map <- renderLeaflet({ leaflet(df) %>%
addCircleMarkers(~x, ~y, color = ~pal()(z)) %>%
addLegend(pal = pal(), values = ~z)
})
}
runApp(shinyApp(ui, server), launch.browser = T)