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 selectInput
s with categories that are related to our selected variables.
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.R
in03_observe_update
.Try running the following code. What is in
categoricalVars
and 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.csv
file using the file browser (it should be in03_observe_update
). Did thex_variable
andy_variable
selectInputs
change?Let’s add a
selectInput
to 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()
reactive
to theobserve
code block to theselectInput
s inui.R
What is going on? Let’s start with the
fileInput
inui.R
. Where doesinput$file1
appear? What do we do with it in the reactive?Now let’s look at the
observe
codeblock. Is it dependent on a reactive? Where are we calling thereactive
? What are we doing in the codeblock?What does the
updateSelectInput
code do? What are we passing into it? WhatselectInputs
are 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