闪亮的 plotOutput 动态属性 [英] Shiny plotOutput dynamic properties

查看:10
本文介绍了闪亮的 plotOutput 动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a plot that depends on user's input. Depending on the input, the plot size will be different.

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

Can I control dynamically the plot's height? I know that in plotOutput() I have a height argument, but I can't find a way to change it dynamically.

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

Reproducible example, when you choose A, the plot looks fine, but if you choose B it's much to high-

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 种不同的尺寸,这取决于需要改变尺寸的输入.

One last thing, in the real app it's not that I have 2 different sizes, depending on the input the size needs to change.

推荐答案

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

To do what you need, you need to use server side rendering. UI does not know what the plot has and how to adjust anything dynamically. It just takes what the server produced and pops it on the screen.

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

Here is a piece of code that does (I think what you need). BTW - I also put the 'data' part into it's own reactive function. You can modify my code further to make the pixel heights 'computed' vs. hard coded and such.

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)

这篇关于闪亮的 plotOutput 动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆