Help understanding req() and reactivity

So, I've created the following reprex, but hopefully someone can explain it to me. Basically I have 2 inputs, one to select a row from a data frame (Player), and one to filter the data frame (Team). I want to display the selected row in table format, bur only if the row has been selected so as not to only display the column headers. If I filter then select a row, or just select a row, it works just fine. If at that point I change the filter input (Team) , the player data is cleared, but the table is still displayed with no data, so obviously req(player()$Name) is staying true forever once it is satisfied once. Any ideas how to "clear" or "reset" that?

library(reprex)

ui <- shinyUI(fluidPage(
    selectizeInput("team","Team",c(Team = " ",Blue = "B", Red = "R")),
    selectizeInput("player","Player",c(Player='')),
    tableOutput("player_info"),
    br(),
    verbatimTextOutput("playah")
))

server <- shinyServer(function(input, output,session) {
    observe({
        req(player()$Name)
        output$player_info <<- renderTable(player())
    })
    output$playah <-renderPrint(player())
    
    player_df <- tibble(Name = c("Abe","Bob"), Team = c("B","R"))
    player_list <- reactive({
        if(input$team == " "){
            player_df %>% .$Name
        }else{
            player_df %>% filter(Team == input$team) %>% .$Name
    }})
    observe({ player_list
        updateSelectizeInput(session,"player",choices = c(' '='',player_list()))
    })
    player <- reactive(player_df %>% filter(Name == input$player))
})

shinyApp(ui = ui, server = server)

Hi @DJG. If I'm understanding correctly, I believe the issue is resolved by removing the first observe() statement and adding req(input$player) inside of output$player_info. This requirement ensures the table will show only when input$player is populated. Please see the updated code below.

library(tidyverse)
library(shiny)

ui <- shinyUI(fluidPage(
  selectizeInput("team","Team",c(Team = " ",Blue = "B", Red = "R")),
  selectizeInput("player","Player",c(Player='')),
  tableOutput("player_info"),
  br(),
  verbatimTextOutput("playah")
))

server <- shinyServer(function(input, output,session) {
  
  output$player_info <- renderTable({
    req(input$player)
    player()
  })
  
  output$playah <-renderPrint(player())
  
  player_df <- tibble(Name = c("Abe","Bob"), Team = c("B","R"))
  
  player_list <- reactive({
    if(input$team == " "){
      player_df %>% .$Name
    }else{
      player_df %>% filter(Team == input$team) %>% .$Name
    }})
  
  observe({ player_list
    updateSelectizeInput(session,"player",choices = c(' '='',player_list()))
  })
  
  player <- reactive(player_df %>% filter(Name == input$player))
  
  })

shinyApp(ui = ui, server = server)

Hi @scottyd22,

Sorry for the tardy response. I have been off the grid for a few days.

I feel like a bit of an idiot for not doing it that way from the beginning, so thank you for the solution. I am still curious about how req() works and if there is a way to clear that state, but I'll leave that for another time.

Thanks again.

No problem! The Details section in the link below gives a good description of how req() works. Hope it helps.

https://shiny.posit.co/r/reference/shiny/0.14/req.html

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.