In this practical you’ll practice creating interactive graphics with shiny
.
By the end of this practical, you’ll try to create the app above!
File | Rows | Columns |
---|---|---|
baselers.csv | 260 | 24 |
kc_house.csv | 21613 | 21 |
Package | Installation |
---|---|
tidyverse |
install.packages("tidyverse") |
DT |
install.packages("DT") |
Open your baselrbootcamp
R project.
Create a new folder in your project called 5_Shiny
.
Inside of your 5_Shiny
folder, create a new folder called 1_Data
(not the one you had previously)
In your computer browser, copy and paste the baselers.csv
file into your new 1_Data
folder.
In RStudio, click File – New File – Shiny Web App
5_Shiny
folder:app.R
should now be open! Click the “Run App” button to open the app.We have a new app template we’d like you to use. You can find it at this link: Shiny Template - app.R. Open this link and copy all of the code it contains. Then, go back to your app.R
file and delete all of the content in the file. After, paste all of the code you just copied.
Click the “Run App” button to run the app! You should now see the following app
textInput(inputID = "Title, ...)
code, add a single comma (like you see below). Then, just below that in the section where you see # Add subtitle
, add the following code:textInput(inputId = "title",
label = "Title",
value = "Plot Title"), # Add the comma here!!!
# Add subtitle
textInput(inputId = "subtitle",
label = "Subtitle",
value = "")
Run your app! If you get an error, ask for help!
Do you see the new text input field? Play around with the input. Do you see any changes to the plot? I predict you won’t!
Don’t worry we’ll fix it! We need to change the plotting code to recognize our new input$subtitle
input. In the ggplot()
code in the server section, change subtitle = "subtitle"
to subtitle = input$subtitle
like you see below.
labs(title = input$title,
subtitle = "subtitle", # Change to: input$subtitle
x = "age",
y = "income",
caption = "My Caption")
selectInput()
widgets to the user interface (below your ‘caption’ input)# Add this to your ui code!!
# Add x
selectInput(inputId = "x",
label = "x",
choices = names(data)),
# Add y
selectInput(inputId = "y",
label = "y",
choices = names(data))
ggplot
code so that it sees your new widgets. Replace the exising values of "age"
and "income"
with input$x
and input$y
as follows: p <- ggplot(data = data,
aes_string(x = "age", # Change to: input$x
y = "income")) # Change to: input$y
selectInput()
as follows:# Add color
selectInput(inputId = "color",
label = "Point colors",
choices = colors(),
selected = "black"),
ggplot
code, change col = "black"
to col = input$color
p <- ggplot(data = data,
aes_string(x = input$x,
y = input$y)) +
geom_point(alpha = 1,
col = "black", # Change to: input$color
size = input$size)
sliderInput()
to the user interface that allows the user to control the alpha
level of the points using the following code:# Add alpha sliderInput to user interface
sliderInput(inputId = "alpha",
label = "alpha",
min = 0,
max = 1,
value = 1)
ggplot()
server code, replace alpha = 1
with alpha = input$alpha
geom_point(alpha = 1, # Change to: input$alpha
col = input$color,
size = input$size) +
checkboxInput()
to the user interface:# Add smooth
checkboxInput(inputId = "smooth",
label = "Add Regression line?",
value = FALSE)
ggplot()
code. This will add geom_smooth()
to the existing plotting object p
# ggplot() code above...
# ADD smooth
# --------------------------
if(input$smooth) {
p <- p + geom_smooth(method = "lm")
}
# ---------------------------
# Return the plotting object!
print(p)
Data
tab. Replace the current value tabPanel("Data")
with the following:# Table tab
tabPanel("Data", br(), dataTableOutput("table")) # Add this!!!
# Generate a summary of the data ----
output$table <- renderDT({
datatable(data %>%
select(1:8))
})
Because your code is totally flexible, you can easily try your app on a new dataset. Copy the kc_house.csv
file into your 1_Data
folder inside of your 5_Shiny
folder. Then, in your Shiny code, remove the baselers.csv
data and replace it with the kc_house.csv
data.
Re run the app! Can you now analyse the kc_house.csv
data instead of the baselers.csv
data?