r语言 - 在 RShiny 中,更改同一 renderPlot() 中单独绘图的绘图宽度/高度



我正在构建一个 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。

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

这是解决方案:

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放到了反应性上下文中。

最新更新