'R Shiny read different tables based on drop down menu for table output

I have two dropdown menus on my app. Depending on the input of one of the dropdown menus, I want a different table to be read. When the user clicks the action button, I want to display the table's reading of the appropriate row based on the dropdown menu options.

library(shiny)

# Import the tables
Ans_yes <- read.csv("C:\\Users\\...\\IfYes.csv")
Ans_no <- read.csv("C:\\Users\\...\\IfNo.csv")

# Define UI for application 
ui <- fluidPage(
    # Application title
    titlePanel("Title Here"),
        tabsetPanel(
            tabPanel("Generator",
            mainPanel(
            selectInput("Number", "Number",
                c("1",
                  "2",
                  "3",
                  "4"),
                
        selectInput("Option1", "Option", 
                    c("Yes", "No")),
        
        actionButton("Enter", "Enter"))),
        
        )
    ))

# Define server logic 
server <- function(input, output) {

    NumClick <- reactive({
        input$Number
    })
    
    OptClick <- reactive({
        input$Option1
    })
    
    observeEvent(input$Enter, 
                 {req(input$Option1);
                 req(input$Number)}
                 )
    
# Run the application 
shinyApp(ui = ui, server = server)



Solution 1:[1]

In general, you want to store any variable that might change because of a user's input inside either a reactiveVal() or a reactiveValues(). This allows you to use functions such as observeEvent() to listen to user inputs then update the variables content based off whatever logic you want.

In the example below I've slightly edited your code to have a reactive Value called data_choice() that changes based of input$Option1. I've also added a table output so you can see the internal variable has changed when the user presses the button.

library(shiny)

# Import the tables
Ans_yes <- head(iris,5)
Ans_no <- head(iris,10)

# Define UI for application 
ui <- fluidPage(# Application title
  titlePanel("Title Here"),
  tabsetPanel(tabPanel(
    "Generator",
    mainPanel(
      selectInput("Number", "Number",
                  c("1",
                    "2",
                    "3",
                    "4"),
                  multiple = FALSE),
      
      selectInput("Option1", "Option",
                  c("Yes", "No"),
                  multiple = FALSE),
      
      actionButton("Enter", "Enter"),
      DT::dataTableOutput("table")
    )
  )))

# Define server logic 
server <- function(input, output) {
  data_choice <- reactiveVal(Ans_yes)
  
  observeEvent(input$Enter,{
               
               if (input$Option1 == "Yes") {
                 data_choice(Ans_yes)
               } else{
                 data_choice(Ans_no)
               }
               print(data_choice())})
  
  output$table <- DT::renderDataTable({
    data_choice()
  })
}
# Run the application 
shinyApp(ui = ui, server = server)
  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1