Search code examples
rshinyshinydashboardggvis

How to constrain ggvis charts to their shinydashboard box and column?


I didn't quite find anything that lined up with my issue and keywords. I cannot get ggvis charts to constrain themselves to their given area of a shinydashboard. The sample app (Windows 7) shows three ggvis plots each exceeding the right border of their respective dashboard box and colum - unless the window is very wide.

library(shiny)
library(shinydashboard)
library(ggvis)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(
          width = 4,
          title = "fred",
          ggvisOutput("plot1")
      ),
      
      box(
        width = 4,
        title = "george",
        ggvisOutput("plot2")
      ),
      
      box(
        width = 4,
        title = "harold",
        ggvisOutput("plot3")
      )
    )
  )
)

server <- function(input, output) {
  mtcars %>%
    ggvis(~wt, ~mpg) %>%
    layer_points() %>%
    bind_shiny("plot1")

  mtcars %>%
    ggvis(~cyl, ~mpg) %>%
    layer_points() %>%
    bind_shiny("plot2")

  mtcars %>%
    ggvis(~disp, ~mpg) %>%
    layer_points() %>%
    bind_shiny("plot3")
}

shinyApp(ui, server)

Solution

  • You need to set options saying that width and height is auto. You can do that as following:

    mtcars %>%
        ggvis(~wt, ~mpg) %>%
        layer_points() %>%
        set_options(width = "auto", height = "auto") %>%
        bind_shiny("plot1")