闪亮的 plotOutput 动态属性

Shiny plotOutput dynamic properties

我有一个取决于用户输入的情节。 根据输入的不同,绘图大小会有所不同。

我可以动态控制地块的高度吗? 我知道在 plotOutput() 中我有一个高度参数,但我找不到动态更改它的方法。

可重现的例子,当你选择A时,情节看起来不错,但如果你选择B,那就太高了-

library(shiny)
library(ggplot2)

df1 <- data.frame(x = 1:2000, y = rnorm(2000), type = rep(LETTERS[1:8], 250))
df2 <- data.frame(x = 1:100, y = rexp (100), type = rep(c('A','B'), 50))

ui <- shinyUI(fluidPage(title = '',
  fluidRow(selectInput("table",'', choices = c('A','B'))),
  fluidRow(plotOutput("my_plot", height = '1000px'))
  )
)

server <- shinyServer(function(input, output) {
  output$my_plot <- renderPlot({
    t <- if(input$table == 'A') df1
    else df2
    ggplot(t) + facet_grid(type~.) +
      geom_point(mapping = aes(x=x, y=y))
  }
  )
})
shinyApp(ui, server)

最后一件事,在真实的应用程序中,我没有 2 种不同的尺寸,尺寸需要根据输入而改变。

要完成您需要的工作,您需要使用服务器端渲染。 UI不知道情节有什么以及如何动态调整任何东西。它只获取服务器生成的内容并将其弹出到屏幕上。

这是一段代码(我想你需要的)。顺便说一句 - 我还将 'data' 部分放入它自己的反应函数中。您可以进一步修改我的代码,使像素高度 'computed' 与硬编码等相对。

library(shiny)
library(ggplot2)

df1 <- data.frame(x = 1:2000, y = rnorm(2000), type = rep(LETTERS[1:8], 250))
df2 <- data.frame(x = 1:100, y = rexp (100), type = rep(c('A','B'), 50))

ui <- shinyUI(fluidPage(title = '',
                        fluidRow(selectInput("table",'', choices = c('A','B'))),
                        fluidRow(uiOutput('myPlotUI'))
)
)

server <- shinyServer(function(input, output) {
  myData <- reactive({
    if (input$table == 'A')
      df1
    else
      df2
  })
  myPlot <- reactive({
    output$myPlot <- renderPlot({
      ggplot(myData()) + facet_grid(type~.) +
        geom_point(mapping = aes(x=x, y=y))
    })
    if (input$table == 'A') {
      plotOutput('myPlot', height = '1000px')
    } else {
      plotOutput('myPlot', height = '250px')
    }
  })
  output$myPlotUI <- renderUI({
    myPlot()
  })
})
shinyApp(ui, server)