4

几天以来,我试图找到解决方案,以在使用功能显示/隐藏侧边栏时在闪亮的应用程序中错误地显示(超出框宽度)。

这是示例代码:

library(shiny)
library(shinydashboard)
library(ggplot2)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    extendShinyjs(text = 'shinyjs.hideSidebar = function(params) { $("body").addClass("sidebar-collapse") }'),
    extendShinyjs(text='shinyjs.showSidebar = function(params) { $("body").removeClass("sidebar-collapse") }'),
    fluidRow(tabsetPanel(id='tabs',
                         tabPanel(value=1,title="Plot1",
                                  fluidRow(
                                    column(12,
                                  plotOutput('plot1', height=800)))),
                         tabPanel(value=2, title="Plot2",
                                  fluidRow(
                                    column(12,
                                  plotOutput('plot2', height=800))))
             )
      )))
  )
)

server <- function(input, output, session) { 
  output$plot1 <- renderPlot({
    out <- ggplot(data.frame(X1=rnorm(1000)),aes(X1))+
      geom_density(fill='light blue')+
      theme_minimal()
    print(out)
  })
  output$plot2 <- renderPlot({
    out <- ggplot(data.frame(X1=rnorm(1000)),aes(X1))+
      geom_density(fill='light blue')+
      theme_minimal()
    print(out)
  })

  observe({
    if (input$tabs == 1) {
      js$hideSidebar()
    }
    else {
      js$showSidebar()
    }
  })
}

shinyApp(ui, server)

正如我们在这个示例代码中看到的那样,我希望在用户打开第二个选项卡时出现侧边栏(侧边栏在 时折叠input$tabs == 1)。它工作得很好,除非我按下查看tab2,然后返回 tab1 并再次返回 tab2,调整绘图大小并切割 x 轴:

在此处输入图像描述

4

2 回答 2

3

执行此操作的“hacky”方法是在添加/删除侧边栏时触发窗口调整大小事件,以在显示/隐藏侧边栏后强制以正确的大小重绘绘图:

extendShinyjs(text = 'shinyjs.hideSidebar = function(params) { $("body").addClass("sidebar-collapse"); 
              $(window).trigger("resize"); }'),
extendShinyjs(text='shinyjs.showSidebar = function(params) { $("body").removeClass("sidebar-collapse"); 
              $(window).trigger("resize"); }')
于 2017-02-03T15:42:43.840 回答
0

我没有正确解决此问题,但解决方法是在您的调用中设置宽度plotOutput()(例如我试过width=800)。然后绘图的尺寸将被固定,并且(一旦将窗口调整到适当的尺寸)绘图不会被剪切。

于 2017-01-24T10:30:11.790 回答