【问题标题】:Shiny plotOutput dynamic properties闪亮的 plotOutput 动态属性
【发布时间】:2017-04-28 11:45:57
【问题描述】:

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

我可以动态控制绘图的高度吗? 我知道在 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 种不同的尺寸,这取决于需要改变尺寸的输入。

【问题讨论】:

    标签: r plot shiny conditional


    【解决方案1】:

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

    这是一段代码(我认为你需要什么)。顺便说一句 - 我还将“数据”部分放入它自己的反应函数中。您可以进一步修改我的代码,以使像素高度“计算”与硬编码等。

    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)
    

    【讨论】:

    • 非常感谢!我不知道我可以在服务器中渲染。完美:)
    猜你喜欢
    • 2016-10-12
    • 2018-07-01
    • 2014-10-15
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多