r - Shiny: leaflet map is not working with rCharts -
using dat
data.frame
set.seed(123) stationid <- c(rep("station1", 3), rep("station2", 3), rep("station3", 3),rep("station4", 3)) x <- c(rep(-77.2803, 3), rep(-79.1243, 3), rep(-93.4991, 3),rep(-117.632, 3)) y <- c(rep(35.8827, 3), rep(42.4356, 3), rep(30.0865, 3),rep(34.0905, 3)) parameter <- rep(c("a", "b", "c"), 4) value <- c(sample(1200:1800, 3), sample(900:1300, 3), sample(1400:2200, 3), sample(850:1400, 3)) dat <- data.frame(stationid, x, y, parameter, value) head(dat)
i created multi bar chart using rcharts
below
library(rcharts) n1 <- nplot(value ~ stationid, group = "parameter", data = dat, type = "multibarchart") n1$xaxis(axislabel = 'station') n1$yaxis(axislabel = 'value') n1$chart(margin=list(left=100,bottom=100)) n1$params$width <- 1700 n1$params$height <- 700 n1
here's result
and created leaflet map below
library(tidyr) dat1 <- tidyr::spread(dat, parameter, value) library(leaflet) leaflet(dat1) %>% addprovidertiles("openstreetmap", group = "openstreetmap") %>% addcirclemarkers(~x, ~y, radius= ~a/50, color ="red", stroke = false, fillopacity = 0.4, group = "a", popup = ~as.character(a)) %>% addcirclemarkers(~x, ~y, radius= ~b/100, color ="green", stroke = false, fillopacity = 0.4, group = "b", popup = ~as.character(b)) %>% addcirclemarkers(~x, ~y, radius= ~c/150, color ="blue", stroke = false, fillopacity = 0.4, group = "c", popup = ~as.character(c)) %>% addlayerscontrol(position = "bottomleft", basegroups = c("openstreetmap"), overlaygroups = c("a","b","c"))
here's result
i combined both of them in shiny app
library(shiny) library(rcharts) library(leaflet) ui <- fluidpage(sidebarlayout( sidebarpanel(h1("shiny app"), tags$hr(), h2("several options here"), width =2), mainpanel(width = 10, uioutput("tb")))) server <- function(input,output){ output$chart1 <- renderchart({ n1 <- nplot(value ~ stationid, group = "parameter", data = dat, type = "multibarchart") n1$xaxis(axislabel = 'station') n1$yaxis(axislabel = 'value') n1$chart(margin=list(left=100,bottom=100)) n1$addparams(dom="chart1") n1$params$width <- 1700 n1$params$height <- 700 return(n1) }) output$map1 <- renderleaflet({ leaflet(dat1) %>% addprovidertiles("openstreetmap", group = "openstreetmap") %>% addcirclemarkers(~x, ~y, radius= ~a/50, color ="red", stroke = false, fillopacity = 0.4, group = "a", popup = ~as.character(a)) %>% addcirclemarkers(~x, ~y, radius= ~b/100, color ="green", stroke = false, fillopacity = 0.4, group = "b", popup = ~as.character(b)) %>% addcirclemarkers(~x, ~y, radius= ~c/150, color ="blue", stroke = false, fillopacity = 0.4, group = "c", popup = ~as.character(c)) %>% addlayerscontrol(position = "bottomleft", basegroups = c("openstreetmap"), overlaygroups = c("a","b","c")) }) output$tb <- renderui({ tabsetpanel(tabpanel("first plot", tags$br(), tags$br(), tags$br(), showoutput("chart1", lib ="nvd3"), tags$br(), tags$br(), tags$br(), leafletoutput("map1", height = 750))) }) } shinyapp(ui = ui, server = server)
rcharts
plot worked fine there no data on leaflet
map.
any suggestion appreciated?
Comments
Post a Comment