hrbrmstr / metricsgraphics

:chart_with_upwards_trend: htmlwidget interface to the MetricsGraphics.js D3 chart library
http://hrbrmstr.github.io/metricsgraphics/
Other
132 stars 35 forks source link

Issue with multiple mjs graphs in shiny #10

Closed hrbrmstr closed 9 years ago

hrbrmstr commented 9 years ago
library(shiny)
library(metricsgraphics)

ui = shinyUI(fluidPage(
  h3("MetricsGraphics Example", style="text-align:center"),
  metricsgraphicsOutput('mjs1'),
  metricsgraphicsOutput('mjs2')
))

server = function(input, output) {
  output$mjs1 <- renderMetricsgraphics(
    mtcars %>%
      mjs_plot(x=wt, y=mpg, width=400, height=300) %>%
      mjs_point(color_accessor=carb, size_accessor=carb) %>%
      mjs_labs(x="Weight of Car", y="Miles per Gallon")
  ),
  output$mjs2 <- renderMetricsgraphics(
    set.seed(1492)
    stocks <- data.frame(
      time = as.Date('2009-01-01') + 0:9,
      X = rnorm(10, 0, 1),
      Y = rnorm(10, 0, 2),
      Z = rnorm(10, 0, 4))
    stocks %>%
      mjs_plot(x=time, y=X) %>%
      mjs_line() %>%
      mjs_add_line(Y) %>%
      mjs_add_line(Z) %>%
      mjs_axis_x(xax_format="date") %>%
      mjs_add_legend(legend=c("X", "Y", "Z"))
  )
}

shinyApp(ui = ui, server = server)

ends up casing:

Listening on http://127.0.0.1:4326 Warning in func() : Ignoring explicitly provided widget ID "mjs-e4befd1929ef888b07e1654d3e2432"; Shiny doesn't use them

and not displaying the second graphic.

hrbrmstr commented 9 years ago

Resolved in 0.5

library(shiny)
library(metricsgraphics)

ui <- shinyUI(fluidPage(
  h3("MetricsGraphics Example", style="text-align:center"),
  metricsgraphicsOutput('mjs1'),
  metricsgraphicsOutput('mjs2')
))

server <- function(input, output) {

  mtcars %>%
    mjs_plot(x=wt, y=mpg, width=400, height=300) %>%
    mjs_point(color_accessor=carb, size_accessor=carb) %>%
    mjs_labs(x_label="Weight of Car", y_label="Miles per Gallon") -> m1

  set.seed(1492)  
  stocks <- data.frame(
    time = as.Date('2009-01-01') + 0:9,
    X = rnorm(10, 0, 1),
    Y = rnorm(10, 0, 2),
    Z = rnorm(10, 0, 4))

  stocks %>%
    mjs_plot(x=time, y=X) %>%
    mjs_line() %>%
    mjs_add_line(Y) %>%
    mjs_add_line(Z) %>%
    mjs_axis_x(xax_format="date") %>%
    mjs_add_legend(legend=c("X", "Y", "Z")) -> m2

  output$mjs1 <- renderMetricsgraphics(m1)

  output$mjs2 <- renderMetricsgraphics(m2)

}

shinyApp(ui = ui, server = server)