以观察者的身份在shinydashboard中动态调整ggplot的高度

以观察者的身份在shinydashboard中动态调整ggplot的高度,r,ggplot2,shinydashboard,R,Ggplot2,Shinydashboard,我正在尝试调整GGPlot的大小,但使用height=as.numeric(输入$dimension[1])在renderPlot中作为观察者,它们的高度值不会改变 我希望在第一行的第三个高度部分显示两个流体行(每个屏幕高度为1/2),在第二个流体行的100%高度处显示三个绘图(Box2、Box3、Box4) 我的身体屏幕应该是这样的(没有滚动): 库(ggplot2) 图书馆(闪亮) 图书馆(shinyjs) 图书馆(shinydashboard) 条形图您想要的是height=as.num

我正在尝试调整GGPlot的大小,但使用
height=as.numeric(输入$dimension[1])
renderPlot
中作为观察者,它们的高度值不会改变

我希望在第一行的第三个高度部分显示两个流体行(每个屏幕高度为1/2),在第二个流体行的100%高度处显示三个绘图(Box2、Box3、Box4)

我的身体屏幕应该是这样的(没有滚动):

库(ggplot2)
图书馆(闪亮)
图书馆(shinyjs)
图书馆(shinydashboard)

条形图您想要的是
height=as.numeric(输入$dimension[2])
,而不是
输入$dimension[1]

您可以使用
exprToFunction
,而不是将每个
renderPlot
封装在观察者中:

output$Box2 <- renderPlot(
  bar,
  height = exprToFunction(as.numeric(input$dimension[2]) *  1/ 2  * 1 / 3)
)

output$Box2问题在于jsis
维度[2]
中的
维度[1]

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

bar <- ggplot(data=iris, aes(x=Species), ) + geom_bar()

ui <- shinyUI(
  dashboardPage(
    header = dashboardHeader(),
    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);
              });'
      )),
      fluidRow(
        column(
          width = 6,
          fluidRow(
            box("Box1", width = 12, background = "aqua")
          )
        ),
        column(
          width = 6,
          fluidRow(
            box(width = 12, plotOutput("Box2", width = "auto", height = "auto"))
          ),
          fluidRow(
            box(width = 12, plotOutput("Box3", width = "auto", height = "auto"))
          ),
          fluidRow(
            box(width = 12, plotOutput("Box4", width = "auto", height = "auto"))
          )
        )
      ),
      fluidRow(
        column(
          width = 12,
          fluidRow(
            box(width = 12, plotOutput("Box5", width = "auto", height = "auto"))
          )
        )
      )
    )
  )
)

server <- shinyServer(function(input, output){
  output$Box2 <- renderPlot(
    bar,
    height = exprToFunction(input$dimension[2] *  1/ 2  * 1 / 3)
  )
  output$Box3 <- renderPlot(
    bar,
    height = exprToFunction(input$dimension[2] *  1/ 2  * 1 / 3)
  )
  output$Box4 <- renderPlot(
    bar,
    height = exprToFunction(input$dimension[2] *  1/ 2  * 1 / 3)
  )
  output$Box5 <- renderPlot(
    bar,
    height = exprToFunction(input$dimension[2] *  1/ 2)
  )
})

shinyApp(ui=ui,server=server)
库(ggplot2)
图书馆(闪亮)
图书馆(shinyjs)
图书馆(shinydashboard)

酒吧也一样!非常感谢!现在我正在尝试解决滚动条问题,因为window.innerHeight不是仪表板页面的高度。当使用
exprToFunction
而不是
observe
,获取
警告:if:argumento tiene longitud cero[无堆栈跟踪可用]
library(ggplot2)
library(shiny)
library(shinyjs)
library(shinydashboard)

bar <- ggplot(data=iris, aes(x=Species), ) + geom_bar()

ui <- shinyUI(
  dashboardPage(
    header = dashboardHeader(),
    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);
              });'
      )),
      fluidRow(
        column(
          width = 6,
          fluidRow(
            box("Box1", width = 12, background = "aqua")
          )
        ),
        column(
          width = 6,
          fluidRow(
            box(width = 12, plotOutput("Box2", width = "auto", height = "auto"))
          ),
          fluidRow(
            box(width = 12, plotOutput("Box3", width = "auto", height = "auto"))
          ),
          fluidRow(
            box(width = 12, plotOutput("Box4", width = "auto", height = "auto"))
          )
        )
      ),
      fluidRow(
        column(
          width = 12,
          fluidRow(
            box(width = 12, plotOutput("Box5", width = "auto", height = "auto"))
          )
        )
      )
    )
  )
)

server <- shinyServer(function(input, output){
  output$Box2 <- renderPlot(
    bar,
    height = exprToFunction(input$dimension[2] *  1/ 2  * 1 / 3)
  )
  output$Box3 <- renderPlot(
    bar,
    height = exprToFunction(input$dimension[2] *  1/ 2  * 1 / 3)
  )
  output$Box4 <- renderPlot(
    bar,
    height = exprToFunction(input$dimension[2] *  1/ 2  * 1 / 3)
  )
  output$Box5 <- renderPlot(
    bar,
    height = exprToFunction(input$dimension[2] *  1/ 2)
  )
})

shinyApp(ui=ui,server=server)