2

I would like to have the shiny-plotly output height and width adjusted to the current window size. I have tried to use the below but of no use based on this post but it throws an error: Error in UseMethod: no applicable method for 'plotly_build' applied to an object of class "shiny.tag". I do exactly the same but I use shinydashboard instead.

library(shiny)
library(shinydashboard)
library(plotly)
shinyApp(
  ui = dashboardPagePlus(
    header = dashboardHeaderPlus(
      enable_rightsidebar = TRUE,
      rightSidebarIcon = "gears"
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      tags$head(tags$script('
                        var dimension = [0, 0];
                        $(document).on("shiny:connected", function(e) {
                        dimension[0] = window.innerWidth;
                        dimension[1] = window.innerHeight;
                        Shiny.onInputChange("dimension", dimension);
                        });
                        $(window).resize(function(e) {
                        dimension[0] = window.innerWidth;
                        dimension[1] = window.innerHeight;
                        Shiny.onInputChange("dimension", dimension);
                        });
                        ')),
      navbarPage("Navbar!",
                 tabPanel("Plot",
                          boxPlus(
                            plotlyOutput("plot1")
                          )

                 ),
                 tabPanel("Summary"

                 ))
    ),
    title = "Right Sidebar"
  ),
  server = function(input, output) {
    output$plot1 <- renderPlotly({
      p<-plot(cars, type=input$plotType)
      ggplotly(p, width = (0.95*as.numeric(input$dimension[1])), height = as.numeric(input$dimension[2]))
    })


  }
)
firmo23
  • 7,490
  • 2
  • 38
  • 114

1 Answers1

0

The example in the link uses the whole page, you're just rendering in the box, which means that you should only do the box adjustments not the whole window page. Im failing to understand why the default behaviour istt acceptable as below:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(plotly)
library(ggplot2)

shinyApp(
  ui = dashboardPagePlus(
    header = dashboardHeaderPlus(
      enable_rightsidebar = TRUE,
      rightSidebarIcon = "gears"
    ),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      navbarPage("Navbar!",
                 tabPanel("Plot",
                          boxPlus(
                            plotlyOutput("plot1")
                          )
                 ),
                 tabPanel("Summary"))
      ),
    title = "Right Sidebar"
    ),
  server = function(input, output) {

    output$plot1 <- renderPlotly({
      p <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species)
      p <- ggplotly(p)
      hide_legend(p)
    })
  }
)

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77