Shiny 根据窗口大小调整盒子的高度和重量

Shiny 根据窗口大小调整盒子的高度和重量,shiny,rstudio,Shiny,Rstudio,我正在尝试根据窗口大小自动调整盒子的高度和重量的代码,也就是说,在最大化窗口的同时,我闪亮的应用程序中的盒子也应该调整它的大小 tabitems( tabItem(tabName = "plot1", box( width = "100%" , height = "100%", solidHeader = FALSE, status = "primary", plotOutput("plot")) ) 框的宽度会根据应用程序窗口的大小而改变大小,但高度不会改

我正在尝试根据窗口大小自动调整盒子的高度和重量的代码,也就是说,在最大化窗口的同时,我闪亮的应用程序中的盒子也应该调整它的大小

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

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

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

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

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()
  }
))
库(闪亮)
图书馆(shinyjs)

shinyjscode如果您能提供用于在Rshinyth中固定盒子大小的代码,那将是一件非常棒的事情!
extendedshinyjs
函数现在需要一个functions参数,以便在fluidPage函数中替换(或至少可以替换)以下内容:
extendedshinyjs(text=shinyjscode,functions=c(“init”,“calcHeight”)
仅在未安装V8软件包的情况下不确定V8在@DeanAttali comment中的含义。这非常有效,只是在某些情况下,我遇到了plotHeight不是数字的问题。通常情况下,您需要使用plotHeight()表示被动,而不是直接使用plotHeight。但是如果我使用height=plotHeight()我得到一个错误:如果没有活动-反应上下文,则不允许操作。(您尝试执行的操作只能从反应表达式或观察者内部执行。)