我有一个基于用户输入的图表。 根据输入,图表的大小将不同。
我能动态控制图表的高度吗?
我知道在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)
最后一件事,在实际应用中,我不是有两种不同的尺寸,而是根据输入需要改变尺寸。