Css 在R页面中显示/隐藏侧边栏后,绘图不会调整100%宽度

Css 在R页面中显示/隐藏侧边栏后,绘图不会调整100%宽度,css,r,shiny,Css,R,Shiny,我有一个绘图,它被设置为100%宽度(默认值)在一个双面板页面的主面板中。侧边栏可通过切换操作按钮隐藏 当侧栏可见时(默认),绘图将填充主面板的宽度。当侧边栏隐藏时,我希望绘图展开以填满现在可用空间的100%,即整个浏览器窗口。但这不会发生!它保持相同的大小 library(shiny) library(shinyBS) UI <- fluidPage( bsButton("showpanel", "Show/hide sidebar", type = "toggle", val

我有一个绘图,它被设置为100%宽度(默认值)在一个双面板页面的主面板中。侧边栏可通过切换操作按钮隐藏

当侧栏可见时(默认),绘图将填充主面板的宽度。当侧边栏隐藏时,我希望绘图展开以填满现在可用空间的100%,即整个浏览器窗口。但这不会发生!它保持相同的大小

library(shiny)
library(shinyBS)

UI <- fluidPage(
    bsButton("showpanel", "Show/hide sidebar", type = "toggle", value = TRUE),
    sidebarLayout(
        conditionalPanel(condition = "input.showpanel == true",
                         sidebarPanel("This is my sidebar.")
                         ),
        mainPanel(plotOutput("plot", width = "100%"))
        )
    )

SERVER <- function(input, output) {
        output$plot <- renderPlot({
        plot(1:10, main = "The width of this plot adjusts\nto window resizes but not to\nshow/hide sidepanel!")
    })
}

runApp(shinyApp(UI,SERVER))
库(闪亮)
图书馆(shinyBS)

UI实现这一点的一种方法是使用反应式布局(这方面有很多问题)。在你的情况下,可能是这样的

library(shiny)
library(shinyBS)

UI <- shinyUI(
        fluidPage(
            bsButton("showpanel", "Show/hide sidebar", type = "toggle", value = TRUE),
            uiOutput('ui')
        )
    )

SERVER <- function(input, output) {

    output$ui <- renderUI({
        if (input$showpanel) {
            sidebarLayout(
                sidebarPanel("This is my sidebar."),
                mainPanel(plotOutput('plot'))
            )
        } else {
            plotOutput('plot')
        }
    })

    output$plot <- renderPlot({
        plot(1:10, main = "The width of this plot adjusts\nto window resizes and to\nshow/hide sidepanel!")
    })
}

runApp(shinyApp(UI,SERVER))
库(闪亮)
图书馆(shinyBS)

UI@konvas answer非常好,可能是您不想这样做的方式,但是如果您想使用
侧边栏布局
(为了给出另一个答案),您可以使用jQuery切换引导列,如:

library(shiny)

ui <- fluidPage(
  tags$head(
    tags$script(
      HTML("
            $(document).ready(function(){
              // Mark columns we want to toggle
              $('body').find('div [class=col-sm-4]').addClass('sidebarPanel');
              $('body').find('div [class=col-sm-8]').addClass('mainPanel');
            })


            Shiny.addCustomMessageHandler ('resize',function (message) {
              $('.sidebarPanel').toggle();
              $('.mainPanel').toggleClass('col-sm-8 col-sm-12');
              $(window).trigger('resize')
            });

           ")
    )
  ),
  actionButton("showpanel", "Show/hide sidebar"),
  sidebarLayout(
    sidebarPanel("This is my sidebar."),
    mainPanel(plotOutput("plot", width = "100%"))
  )
)

server <- function(input, output, session) {
  observeEvent(input$showpanel,{
    session$sendCustomMessage(type = 'resize', message = 1)

  })
  output$plot <- renderPlot({
    plot(1:10, main = "The width of this plot adjusts\nto window resizes but not to\nshow/hide sidepanel!")
  })
}

runApp(shinyApp(ui,server))
库(闪亮)

ui问题是,使用侧边栏布局可以创建两个引导列,一个是侧边栏的class
col-sm-4
,另一个是绘图的class
col-sm-8
,这就是为什么绘图的大小不会重新调整到屏幕的66%以上。这也是一个很好的答案,我对Java没有太多经验,但这正是我所期望的解决方案。出于节俭的原因,选择了@konvas答案。谢谢非常感谢。我觉得他的回答很好,很高兴我能帮上忙!工作完美-谢谢!还有一个好处,就是基本上把我所有的代码都保存在一个文件中。我只是没有跳出框框思考。。。