1
votes

I have an app that displays a data frame of NBA statistics organized by team and player. When the user selects a team, a data frame of the statistics for the best players from that team are displayed. When the user selects a player, a data frame of the statistics for the player is displayed.

All 30 NBA teams are displayed in selectInput. When a team is chosen, a different selectInput offers displays a list of certain players from that team. There's a tab for the dataframe that displays a list of players and a tab that solely displays a single player's statistic.

How can I make it so that, once a user selects a player, the app updates to the player tab? What I am doing currently with observeEvent does not work. I tried using observe with input$var1 but I was receiving an "argument is of length zero" error.

See the tabs here, https://binlee.shinyapps.io/test_app/

# ui.R 

shinyUI(fluidPage(
  titlePanel("My App"),

  sidebarLayout(
sidebarPanel(
  uiOutput("teamInput")
  , uiOutput("playerInput")),

mainPanel(
  tabsetPanel(
    tabPanel("Team", tableOutput("table")), 
    tabPanel("Player", tableOutput("table1"),
             id = "panelTabs")
  )))))

df <- read.csv("data.csv", header = TRUE, stringsAsFactors = FALSE)


# server.R
shinyServer(
  function(input, output, session) 
  {output$teamInput <- renderUI({selectInput("var",
                                         label = "Choose Team",
                                         choices = c(unique(df$Team)),
                                         selected = "unique(df$Team)")})

output$table <- renderTable({df[df$Team == input$var,4:14]})

output$playerInput <- renderUI({selectInput("var1",
                                          label = "Choose Player",
                                          choices = df[df$Team == input$var, 4])})

observeEvent(input$var1,  
            {updateTabsetPanel(session, inputId="panelTabs", selected="Player")})

output$table1 <- renderTable({df[df$Name == input$var1, 4:14]})
})
1

1 Answers

0
votes

Please refer below app code for your custom UI Flow.
In my flow, you initially have to select the team from the Team tab, then go to the Player tab to choose your player and similarly will showcase the datatable as well.

UI

shinyUI(fluidPage(
  titlePanel("My App"),

  sidebarLayout(
    sidebarPanel(
      conditionalPanel("input.tabSetPane == 'teamTAB'",
                       uiOutput("teamInput")
                       ),
      conditionalPanel("input.tabSetPane == 'playerTAB'",
                       uiOutput("playerInput")
                       )
      ),

    mainPanel(
      tabsetPanel(id = "tabSetPane", 
        tabPanel("Team", value = "teamTAB",
                 h1("Team - table")
                 #Call Team Table HERE
                 ), 
        tabPanel("Player", value = "playerTAB",
                 h1("Player - table")
                 #Call Player Table HERE
                 )
      )
    )
  )

))

SERVER

shinyServer(function(input, output, session) {

  output$teamInput <- renderUI({
    selectInput("teamVar", label = "Choose Team",
                choices = c("Team1", "Team2", "Team3"))
  })

  output$playerInput <- renderUI({
    selectInput("playerVar", label = "Choose Player", 
                choices = c("Player-A", "Player-B", "Player-C"))
  })

})

Due to unavailability of reproducible data input, provided a generic flow only
Hope this helps.