Subset a reactive datafrane by selectinput choices that come from the reactive dataframe


#1

im new to shiny and i would like to make a (theoretical?) question that probably is wrong but in any case after searching i would like to know. Can you subset a reactive dataframe by using the values of a selectinput() that was created based on this dataframe? It seems like a paradox to me because every time the dataframe is updated the selectinput() is updated too. I will use a small example of my thought that may help someone understand. Probably what im asking is naive but I would like to know.

location = c("100 ail","16th and Whitmore","40AB01 - ANTWERPEN","Abington") 
last = c("2018-02-01 09:30:00", "2018-02-01 03:00:00", "2017-03-07 10:00:00","2018-02-01 03:00:00") 
first = c("2015-09-01 00:00:00","2016-03-06 19:00:00","2016-11-22 15:00:00","2016-06-09 19:00:00") 
locations = data.frame(location, last, first)



library(shiny)
library(shinythemes)
library(htmltools)
library(DT)
library(utilr)
library(openair)
library(dplyr)
library(ropenaq)
library(worldmet)

shinyUI(fluidPage(theme = shinytheme("slate"),

  # title
  titlePanel("Select Dates of interest and location"),

  # first row; This allows a user to enter their start and stop dates
  fluidRow(
    column(3, wellPanel(

    )
    ),
    column(3, wellPanel(

    )
    ),
      column(
          width = 6

      )
  ),



  fluidRow(
      tabsetPanel(
          type = "tabs",
          # summary tab
          tabPanel(
              "  Select Dates and Location",
              uiOutput("loc"),
              uiOutput("dt"),
              shiny::dataTableOutput("merged")




          ),




          # scenario tab.  Needs work.  Panel should include a summary of user choices (selected on previous panels) and then 
          # allow a user to enter their email address.  An action button would be pressed to create the output from OpenAir.
          tabPanel(
              "Selection Summary and Process Data",
              fluidRow(
                  # actionButton("goButton", "OpenAir Local!"),
                  # helpText("When you click the button above, you should see",
                  #          "the output below update to reflect the value you",
                  #          "entered at the top:"),

                  )


                  )
              )
          )
      )
  )



#server.r
shinyServer(function(input, output, session) {
 #initial selectinput to create the initial dataframe
output$loc<-renderUI({
     selectInput("loc", label = h4("Choose location"),
                 choices = locations$location ,selected = 1
     )
   })

    rt<-reactive({
#dataframe creation
    AQ<- aq_measurements(location = input$loc) 
    
    met <- importNOAA(year = 2014:2018)
    colnames(AQ)[9] <- "date"
    merged<-merge(AQ, met, by="date")

    merged$location <- gsub( " " , "+" , merged$location)
    merged


   })





#selectinput based on the dataframe
output$dt<-renderUI({
     selectInput("dt", label = h4("Choose Dates"), 
                 choices = as.character(rt()[,1]),
                 selected = 1,
                 multiple = T)
   })


#attempt to create a datatable by subseting the dataframe by the choices of selectinput   
   output$merged <- shiny::renderDataTable({
     a <- subset(rt(), rt()[,1] %in% input$dt)
   })
})

#2

Yes, you certainly can generate user inputs based on a reactive dataset. I stripped out some of the packages, functionality, and data from your example to show it:

library(shiny)
library(dplyr)

location = c("Hobbiton", "Hobbiton", "Rivendell", "Rivendell", "Minas Tirith", "Minas Tirith") 
last = c("A", "B", "C", "D", "E", "F") 
first = c("G", "H", "I", "J", "K", "L") 
locations = data.frame(location, last, first)


# Define UI for application that draws a histogram
ui <- fluidPage(
   
   # Application title
   titlePanel("Select Dates of interest and location"),
   
   fluidRow(
           column(3, wellPanel(
           )
           ),
           column(3, wellPanel(
           )
           ),
           column(
                   width = 6
           )
   ),
   
   fluidRow(
           tabsetPanel(
                   type = "tabs",
                   # summary tab
                   tabPanel(
                           "  Select Dates and Location",
                           uiOutput("loc"),
                           uiOutput("dt"),
                           shiny::dataTableOutput("merged")
                   )
           )
   )
)


# Define server logic required to draw a histogram
server <- function(input, output, session) {
   
        output$loc<-renderUI({
                selectInput("loc", label = h4("Choose location"),
                            choices = locations$location ,selected = 1
                )
        })
        
        rt<-reactive({
                #dataframe creation
                locations %>%
                        filter(location == input$loc)
        })
        
        output$dt<-renderUI({
                selectInput("dt", label = h4("Choose Dates"), 
                            choices = as.vector(rbind(as.character(rt()$first),as.character(rt()$last))),
                            selected = 1,
                            multiple = T)
        })
        
        output$merged <- shiny::renderDataTable({
                locations %>%
                        filter(location == input$loc,
                               first %in% input$dt | last %in% input$dt)
        })
}

# Run the application 
shinyApp(ui = ui, server = server)