繁体   English   中英

动态 plot 高度调整不适用于 shiny 仪表板

[英]Dynamic plot height adjustment does not work for shiny dashboard

我想将闪亮的 output 高度和宽度调整为当前 window 大小。 我曾尝试使用以下内容,但根据这篇文章没有用,但它会引发错误: Error in UseMethod: no applicable method for 'plotly_build' applied to an object of class "shiny.tag" 我做的完全一样,但我使用了 shinydashboard。

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


  }
)

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

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM