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

时间:2017-03-20 01:59:42

标签: r shiny

我正在构建一个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设置==&#34;一个&#34;时,我希望参数为600和600,当selectInput设置为==&#34;两个&#34;时,我希望参数为600和800.

之前是否有人遇到此问题,并知道如何处理它? 谢谢!

1 个答案:

答案 0 :(得分:1)

以下是解决方案:

fList[:] = []

我已将fList = [] 移至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) ,此外我已将plotOutputserver置于被动上下文中。