动态绘图高度调整不适用于闪亮的仪表板

时间:2019-11-07 01:54:49

标签: r shiny shinydashboard

我想将闪亮的输出高度和宽度调整为当前窗口大小。我尝试使用以下内容,但没有根据this post使用它,但是它引发了错误:Error in UseMethod: no applicable method for 'plotly_build' applied to an object of class "shiny.tag"。我做的完全一样,但是我使用了闪亮的仪表板。

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]))
    })


  }
)

1 个答案:

答案 0 :(得分:0)

链接中的示例使用整个页面,您只是在方框中进行渲染,这意味着您应该只对方框进行调整,而不是整个窗口页面。我无法理解为什么默认行为可以接受,如下所示:

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