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

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

问题描述

我有一个图取决于用户的输入。
取决于输入内容,地块大小会有所不同。

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

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

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)

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

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