Shiny is an R package that allows us to build and share interactive web applications through R. If you do not already have it, go ahead and install the following packages:
The basic structure of input functions are similar to each other.
The basic structure when rendering outputs are also similar to one another.
The key to using outputs is to properly ID them so we can call them back to our UI. We name them in the server with ‘output$outputID’.
We also want to make sure that we are using the correct render function based on our desired output.
You will see code later that uses the reactive function in the server. The main thing to know about the reactive function is that it allows us to streamline our computations. For example, say we filter all of our data based on a single value. Instead of repeating this computation within every render function, we can filter our data in the reactive function. We can then use the data from the reactive function throughout the server. This allows us to only perform the computation once instead of in every render function.
b.
One of the great things about shiny apps is that we can deploy them locally (on our own computers) or host them online (with URLs anyone can access). Deploying our apps locally doesn’t necessarily mean no one else can access them since we can share the code for people to also locally deploy the shiny app. For now we will focus on local deployment.
If you don’t already have the shiny skeleton code for the demo, go ahead and download it!
Working in small groups, create your own Shiny app using the Shiny Skeleton!
# A tibble: 6 × 9
...1 age sex blood_pressure cholesterol fbs_over_120 max_hr
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 70 1 130 322 0 109
2 2 80 0 115 564 0 160
3 3 55 1 124 261 0 141
4 4 65 1 128 263 0 105
5 5 45 0 120 269 0 121
6 6 30 1 120 177 0 140
# ℹ 2 more variables: exercise_angina <dbl>, heart_disease <chr>
# A tibble: 6 × 12
...1 id birth_year education marital_status income fruit_purchases
<dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
1 1 5524 1957 Graduation Single 58138 88
2 2 2174 1954 Graduation Single 46344 1
3 3 4141 1965 Graduation Together 71613 49
4 4 6182 1984 Graduation Together 26646 4
5 5 5324 1981 PhD Married 58293 43
6 6 7446 1967 Master Together 62513 42
# ℹ 5 more variables: meat_purchases <dbl>, fish_purchases <dbl>,
# candy_purchases <dbl>, sale_purchases <dbl>, complaint <dbl>
student = read_csv("https://bit.ly/ucb_biostat_student_data")
student = student[sample(nrow(student), size=500),]
head(student)
# A tibble: 6 × 14
...1 student_id hours_studied attendance parental_involvement
<dbl> <dbl> <dbl> <dbl> <chr>
1 6248 6248 24 76 High
2 5420 5420 13 65 Low
3 2768 2768 23 67 Medium
4 6527 6527 18 91 Low
5 4383 4383 16 67 Medium
6 1817 1817 7 92 High
# ℹ 9 more variables: access_to_resources <chr>, sleep_hours <dbl>,
# previous_scores <dbl>, internet_access <chr>, family_income <chr>,
# teacher_quality <chr>, school_type <chr>, gender <chr>, exam_score <dbl>