【问题标题】:In RShiny, change plot width / height for separate plots within same renderPlot()在 RShiny 中,更改同一 renderPlot() 中单独绘图的绘图宽度/高度
【发布时间】:2017-08-11 04:57:19
【问题描述】:

我正在构建一个 RShiny 应用程序,其中包括 1 个 renderPlot 调用(在服务器中)及其 1 个相应的 plotOutput 调用(在 ui 中)。但是,在 renderPlot 代码中,有一个来自 ui 的切换开关,用于在两个不同的绘图之间切换。我希望地块有不同的坐标。下面是一个可重现的 RShiny 应用程序,它使用通用图来突出我的问题的各个方面:

selector = c("one", "two")
names(selector) = c("one", "two")

plot.width = 600
plot.height = 600

ui <- fluidPage(
                fluidRow(
                  # Organizes the title of the whole shiny app 
                  # ==========================================
                  column(width = 12, align = 'center',
                         h2('NBA Shot Chart and Movement Tracking Application'))
                ),

                fluidRow(
                  # This coordinates the location of the LHS widgets
                  # ================================================                 
                  column(width = 4, align = 'center', 
                           selectInput(inputId = 'shooter.input', label = 'Select Shooter:', multiple = FALSE, 
                                       choices = selector, selected = 'one')),

                  column(width = 8, align = 'left',
                         plotOutput('shot.chart', width = plot.width, height = plot.height)
                  )
                )
)

server <- shinyServer(function(input, output) {

  # renderPlot for the charts (shot charts and movement charts)
  output$shot.chart <- renderPlot({

    if(input$shooter.input == "one") {
      plot(c(1,2,3,4,5), c(6,7,8,9,10))
    }
    else {
      plot(c(1,2,3,4,5), c(1,1,1,1,1))
    }
  })
})

shinyApp(ui = ui, server = server)

好的,我的问题与 ui 中 plotOutput 中设置的 plot.width 和 plot.height 参数有关。我希望这些参数针对两个图中的每一个都进行更改。当selectInput设置==“one”时,我希望参数为600和600,当selectInput设置==“two”时,我希望参数为600和800。

有没有人遇到过这个问题,并且知道如何处理它? 谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    解决办法如下:

    library(shiny)
    
    selector = c("one", "two")
    names(selector) = c("one", "two")
    
    
    ui <- fluidPage(
      fluidRow(
        # Organizes the title of the whole shiny app 
        # ==========================================
        column(width = 12, align = 'center',
               h2('NBA Shot Chart and Movement Tracking Application'))
      ),
    
      fluidRow(
        # This coordinates the location of the LHS widgets
        # ================================================                 
        column(width = 4, align = 'center', 
               selectInput(inputId = 'shooter.input', label = 'Select Shooter:', multiple = FALSE, 
                           choices = selector, selected = 'one')),
    
        column(width = 8, align = 'left',
               uiOutput('shot.chart_ui')
        )
      )
    )
    
    server <- shinyServer(function(input, output) {
    
    
      output$shot.chart_ui <- renderUI({
        if(input$shooter.input == "one") {
          plot.width = 600
          plot.height = 600
        }else{
          plot.width = 600
          plot.height = 800
        }
        plotOutput('shot.chart', width = plot.width, height = plot.height)
    
      })
      # renderPlot for the charts (shot charts and movement charts)
      output$shot.chart <- renderPlot({
    
        if(input$shooter.input == "one") {
          plot(c(1,2,3,4,5), c(6,7,8,9,10))
        }
        else {
          plot(c(1,2,3,4,5), c(1,1,1,1,1))
        }
      })
    })
    
    shinyApp(ui = ui, server = server)
    

    我已将 plotOutput 移至 server,此外,我已将 plot.widthplot.height 放入响应式上下文中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 2022-01-15
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      相关资源
      最近更新 更多