6 App 4: Adding in a file loader: observe and update.
We’re going to make our file explorer more useful for other files by not hard coding variable names. We’ll take a .csv file as input, and populate our selectInputs with categories that are related to our selected variables.
observe/update programming pattern
6.1 Exercise
This will be more code review than activity. I apologize. Go through it and you will learn a lot.
Open the
app.Rin03_observe_update.Try running the following code. What is in
categoricalVarsand what is innumericVars? Is it what you expected? If you’re confused, please let me know.
#since we're in the shiny_pdx_rlang directory,
#we need to set our working directory to 03_observe_update
library(fivethirtyeight)
library(shiny)
library(tidyverse)
##Load in the helper functions
source("04/_observe_update/helper.R")
data(biopics)
myDataFrame <- biopics
##these functions return the categorical variables and
##the numeric variables, given a data.frame
##They're in helper.R
##Try running all the code before ui
##and look at what categoricalVars and numericVars contains
categoricalVars <- get_category_variables(myDataFrame)
numericVars <- get_numeric_variables(myDataFrame)
glimpse(myDataFrame)
categoricalVars
numericVars
Try running the app, and try loading the
datasetB.csvfile using the file browser (it should be in03_observe_update). Did thex_variableandy_variableselectInputschange?Let’s add a
selectInputto filter on a particular category within our category variable. Uncomment all of the areas that sayuncomment this code for step 4(there are 4 sections). As you do, follow the path from themyData()reactiveto theobservecode block to theselectInputs inui.RWhat is going on? Let’s start with the
fileInputinui.R. Where doesinput$file1appear? What do we do with it in the reactive?Now let’s look at the
observecodeblock. Is it dependent on a reactive? Where are we calling thereactive? What are we doing in the codeblock?What does the
updateSelectInputcode do? What are we passing into it? WhatselectInputsare we modifying?Try running the app again and make sure that it works before moving on.
6.2 More examples
The observe demo in the shiny gallery shows another example of observe: https://shiny.rstudio.com/gallery/observer-demo.html