【问题标题】:Adjust height and weight of a box according to windows size in shiny根据窗口大小调整盒子的高度和重量
【发布时间】:2015-08-09 07:28:04
【问题描述】:

我正在尝试使用此代码根据窗口大小自动调整框的高度和重量,即在最大化窗口的同时,我闪亮的应用程序中的框也应该调整其大小。

tabitems(
 tabItem(tabName = "plot1", 
       box( width = "100%" , height = "100%", solidHeader = FALSE, status = "primary",
         plotOutput("plot"))
)

框的宽度会根据应用窗口大小而改变其大小,但高度不会?

【问题讨论】:

    标签: shiny rstudio


    【解决方案1】:

    请注意,plotOutput 的文档说“请注意,对于高度,使用“自动”或“100%”通常不会按预期工作,因为高度是使用 HTML/CSS 计算的。”

    这是一个解决方案,它使用 JavaScript 来计算浏览器窗口的高度,将该数字保存为 Shiny 输入,并将其用作 heightplotOutput。请记住,可能还有其他方法,我很想看看还能做些什么,但这是首先想到的。

    library(shiny)
    library(shinyjs)
    
    shinyjscode <- "
    shinyjs.init = function() {
      $(window).resize(shinyjs.calcHeight);
    }
    shinyjs.calcHeight = function() { 
      Shiny.onInputChange('plotHeight', $(window).height());
    }
    "
    
    runApp(shinyApp(
      ui = fluidPage(
        shinyjs::useShinyjs(),
        shinyjs::extendShinyjs(text = shinyjscode),
        plotOutput("plot")
      ),
      server = function(input, output, session) {
        plotHeight <- reactive({ 
          ifelse(is.null(input$plotHeight), 0, input$plotHeight)
        })
    
        output$plot <- renderPlot({
          plot(1:10)
        },
        height = plotHeight)
    
        js$calcHeight()
      }
    ))
    

    【讨论】:

    • 如果您能提供在 RShiny 中修复 Box 大小的代码,那就太好了
    • 这非常有效! extendShinyjs 函数现在需要一个函数参数,以便以下内容应该(或至少可以)替换到 fluidPage 函数中:extendShinyjs(text = shinyjscode, functions = c("init", "calcHeight")
    • 仅在未安装 V8 包的情况下
    • 不确定 V8 在@DeanAttali 评论中的含义。这工作得很好,除了在某些情况下我遇到 plotHeight 不是数字的问题。通常,您需要将 plotHeight() 用于反应性,而不是直接使用 plotHeight。但是如果我使用 height=plotHeight() 我会得到一个错误:没有活动的反应上下文就不允许操作。 (你试图做一些只能从反应式表达式或观察者内部完成的事情。)
    猜你喜欢
    • 2012-06-19
    • 2013-06-06
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 2015-09-29
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多