Shiny plotOutput dynamic properties

后端 未结 1 1803
失恋的感觉
失恋的感觉 2021-01-14 15:00

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

Can I control dynamically the plot\'s height? I know that in

相关标签:
1条回答
  • 2021-01-14 15:45

    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)
    
    0 讨论(0)
提交回复
热议问题