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

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.

  1. Open the app.R in 03_observe_update.

  2. Try running the following code. What is in categoricalVars and what is in numericVars? 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
  1. Try running the app, and try loading the datasetB.csv file using the file browser (it should be in 03_observe_update). Did the x_variable and y_variable selectInputs change?

  2. Let’s add a selectInput to filter on a particular category within our category variable. Uncomment all of the areas that say uncomment this code for step 4 (there are 4 sections). As you do, follow the path from the myData() reactive to the observe code block to the selectInputs in ui.R

  3. What is going on? Let’s start with the fileInput in ui.R. Where does input$file1 appear? What do we do with it in the reactive?

  4. Now let’s look at the observe codeblock. Is it dependent on a reactive? Where are we calling the reactive? What are we doing in the codeblock?

  5. What does the updateSelectInput code do? What are we passing into it? What selectInputs are we modifying?

  6. 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