I am new to R shiny. Trying to show a line chart upon clicking onto the marker on the map. However, the id does not seem to work. Please assist. Any help would be appreciated.
library(shiny) # for shiny apps
library(leaflet) # renderLeaflet function
library(readxl)
library(ggplot2)
server = function(input, output) {
Cookedfood_R <- readRDS("~/hawkermaster.rds")
line <- readRDS("~/line.rds")
#Reshape the data for ggplot
traff2 <- melt(line,id=c("TYPE","newname"),variable.name = "Year")
#Remove the X in the Year column and convert it to number
traff2$Year <- as.numeric(gsub(pattern="X",replacement = "",x = as.character(traff2$Year)))
# create a reactive value that will store the click position
data_of_click <- reactiveValues(clickedMarker=NULL)
#getColor <- function(Cookedfood_R) {
# sapply(Cookedfood_R$TYPE, function(TYPE) {
# if(TYPE == 1) {"blue"}
# else {"orange"} })
#}
icons <- awesomeIcons(
icon = 'ion-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(Cookedfood_R)
)
output$map = renderLeaflet({
leaflet() %>% addTiles() %>%
addMarkers(data = Cookedfood_R,
lat = ~ LATITUDE,
lng = ~ LONGITUDE,
icon = icons,
popup = paste(Cookedfood_R$HAWKER, "<br>",
"Total no. of stalls:", Cookedfood_R$total, "<br>",
"Avg bid $:", trunc(Cookedfood_R$avgp,"<br>")))})
# store the click
observeEvent(input$map_marker_click,{
data_of_click$clickedMarker <- input$map_marker_click
})
# Make a line chart depending of the selected point
output$plot=renderPlot({
ggplot(subset(traff2$newname==data_of_click$clickedMarker))+
facet_grid(facets = newname~., scales = "free_y")+
geom_line(aes(Year, value, color = TYPE))+theme_bw()+
geom_point(aes(shape=TYPE, size=1))
})
}
ui <- fluidPage(
br(),
column(8,leafletOutput("map", height="700px")),
column(4,br(),br(),br(),br(),plotOutput("plot", height="400px")),
br()
)
shinyApp(ui = ui, server = server)