0

I am building an RShiny app that includes 1 renderPlot call (in server) with its 1 corresponding plotOutput call (in ui). However, within the renderPlot code, there is a toggle from the ui that switches between two different plots. I would like the plots to have different coordinates. Below is a reproducible RShiny app using generic plots to highlight the aspects of my question:

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)

Okay, my question has to do with the plot.width and plot.height parameters set in plotOutput in ui. I want these parameters to change for each of the two plots. When selectInput is set == "one", I want the parameters to be 600 and 600, and when the selectInput is set == "two", I want the parameters to be 600 and 800.

Has anybody run into this problem before, and knows how to deal with it? Thanks!

1 Answer 1

2

Here is the solution:

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)

I have moved the plotOutput to the server and furthermore i have put plot.width and plot.height into reactive context.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.