Subset a data frame based on user-input, Shiny

前端 未结 1 1441
既然无缘
既然无缘 2021-02-13 20:58

I\'m trying to build a Shiny app that subsets a data frame (to only include rows where a categorical variable matches the user-select input from the UI) before the data is proce

相关标签:
1条回答
  • 2021-02-13 21:08

    I had the same issue and spent a couple of hours trying to figure it out. Once you have assigned the reactive object, you need to use target_inventory() in order to refer to it (as BenBarnes mentioned in the comment section).

    Here is a MWE (minimum working example)

    ui.R

    #ui
    library(shiny)
    
    shinyUI(fluidPage(
      #User dropbox
      selectInput("state", "Choose state", choices=c("MA", "CA", "NY"))
      #Print table to UI
      ,tableOutput("table1")
    ))
    

    server.r

     #server
    library(shiny)
    
    shinyServer(function(input,output){
    
      category <- c("MA", "CA", "NY")
      population <- c(3,8,4)
    
      df <- data.frame(category,population)
    
      df_subset <- reactive({
        a <- subset(df, category == input$state)
        return(a)
      })
    
      output$table1 <- renderTable(df_subset()) #Note how df_subset() was used and not df_subset
    
    
    })
    
    0 讨论(0)
提交回复
热议问题