1

Using this 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 as 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 is the result

enter image description here

And I created leaflet map as 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 the result

enter image description here

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 but there was no data on the leaflet map.

enter image description here

Any suggestion would be appreciated?

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
shiny
  • 3,380
  • 9
  • 42
  • 79
  • 1
    you don't have either `dat` or `dat1` in your `shiny` function – SymbolixAU Apr 20 '16 at 03:18
  • 1
    I have a suspicion there's a conflict between `rCharts` and `leaflet` somewhere. If you remove the `chart1` from the app then the map plots as expected – SymbolixAU Apr 20 '16 at 03:36
  • note that `rCharts` has its own leaflet functions: [`rCharts::Leaflet`](https://github.com/ramnathv/rCharts/blob/master/demo/leaflet.R) – SymbolixAU Apr 20 '16 at 03:38
  • 1
    It could be related to [this issue](https://github.com/ramnathv/rCharts/issues/388) ? – SymbolixAU Apr 20 '16 at 03:58
  • @Symbolix Thanks for your time and help. This means I can't have leaflet map and rCharts in the same shiny app – shiny Apr 20 '16 at 04:59
  • 1
    this is indeed a `css` conflict as described in http://stackoverflow.com/questions/23151171/rcharts-conflict-between-leaflet-and-nvd3-in-shiny. I'll supply a patch in a Github branch that will solve the problem, but in general I would recommend using `htmlwidgets` libraries instead of the legacy `rCharts`. – timelyportfolio Apr 20 '16 at 14:00
  • 1
    please see https://github.com/ramnathv/rCharts/issues/388#issuecomment-212444065 – timelyportfolio Apr 20 '16 at 14:18
  • 1
    thanks @timelyportfolio , I need to start making use of `htmlwidgets`! – SymbolixAU Apr 21 '16 at 01:14
  • @timelyportfolio Can the nvd3 plot above be created by htmlwidgets? I tried plotly with ggplot2 but I had a problem with y axis values. It is not reactive as rCharts – shiny Apr 21 '16 at 01:55
  • not nvd3 but there are other options such as rbokeh, plotly, taucharts – timelyportfolio Apr 21 '16 at 02:44
  • @timelyportfolio Thanks for your time and suggestions. The reason I'm interested in using rCharts nvd3 is the fast interactive response of y axis to removal and/or addition of one of the parameters. For example if we change the value in the above data.frame to be value <- c(104365, 500, 43, 135465, 365, 27, 132466, 269, 53, 123045, 421, 71) dat <- data.frame(stationID, x, y, parameter, value) and use plotly library(ggplot2) library(plotly) gg <- ggplot(dat, aes(x=stationID, y=value, fill=parameter )) + geom_bar(stat= "identity", position = "dodge")+ theme_bw() ggplotly(gg) TBC – shiny Apr 21 '16 at 03:26
  • I found y axis values are not responsive if I remove the parameter which has the largest values (A) – shiny Apr 21 '16 at 03:28
  • 1
    `nvd3` does have some very nice features. Using `plotly` directly through `plot_ly` or on the `JavaScript` side will probably provide a smoother result. If I have some time, I will demonstrate. – timelyportfolio Apr 21 '16 at 12:50
  • @timelyportfolio Thanks. Looking forward to see your example of plot_ly – shiny Apr 23 '16 at 05:27

0 Answers0