owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
6 October 2022
Welcome!
## Share your code snippet
If you want to share your code snippet, copy paste your snippet within a section of three backticks (```):
As an **example**:
```
library(tidyverse)
```
(*you can copy paste this example and add your code further down*)
## Yellow sticky notes
No yellow sticky notes online. Put your name + " | " and add a "*" each time you solve a challenge (see below).
## Participants
Name | Challenges
--- | ---
Damiano Oldoni |
Lynn Pallemaerts |***
Cécile Herr |***
Hans Van Calster | *
Sarah Broos|
Adriaan Seynaeve | *
Raïsa Carmen | ***
Els Lommelen |
Floris Vanderhaeghe | ***
Wouter Van Landuyt
Laura Marquez | *
Toon Westra | ***
## Challenge 1
### floris
3. `shinyApp(ui = ui, server = server)`
4.
5. `input` defined and generated by `sliderInput()` in `ui`, output defined by `renderPlot()` in the `server` function and picked up in `ui` by `plotOutput()`
6. Change the default `app.R` as follows:
```diff
--- a/src/myfirstshinyapplication/app.R
+++ b/src/myfirstshinyapplication/app.R
@@ -37,13 +37,14 @@ server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
- x <- faithful[, 2]
+ x <- faithful[, 1]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white',
- xlab = 'Waiting time to next eruption (in mins)',
- main = 'Histogram of waiting times')
+ xlab = 'minutes',
+ ylab = 'number of eruptions',
+ main = 'Distribution of eruption times')
})
}
```
7. Change `app.R` as follows:
```diff
--- a/src/myfirstshinyapplication/app.R
+++ b/src/myfirstshinyapplication/app.R
@@ -22,7 +22,7 @@ ui <- fluidPage(
"Number of bins:",
min = 1,
max = 50,
- value = 30)
+ value = 20)
),
# Show a plot of the generated distribution
```
## Challenge 2
### floris
Note that my solution uses an external folder for the data (the original data folder of the coding club project); this works but actually the app should be self-contained, i.e. have the data folder inside the app folder.
<details><summary><code>app.R</code> (click to expand)</summary>
```r
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
butter <- read.csv("../../data/20221006/20221006_butterflies_data.txt")
butter$species <- factor(butter$species)
butter$biotope <- factor(butter$biotope)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Butterflies in different biotopes"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
radioButtons("buttons",
"Biotope:",
levels(butter$biotope)),
selectInput("dropdown",
"Species:",
levels(butter$species))
),
# Show a plot of the generated distribution
mainPanel(
textOutput("textoutput")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$textoutput <- renderText(
paste("You selected biotope",
input$buttons,
"and species",
input$dropdown)
)
}
# Run the application
shinyApp(ui = ui, server = server)
```
</details>
### Hans:
```
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(tidyverse)
## CHALLENGE 3
# code to be on top of app.R
# read dataset (path to be checked)
butterfly_data <- read_csv(here::here("./data/20221006/20221006_butterflies_data.txt", na = ""))
# biotopes
biotopes <- unique(butterfly_data$biotope)
# species
sp <- unique(butterfly_data$species)
biotopes_list <- as.list(biotopes)
biotopes_list <- set_names(biotopes_list, biotopes)
species_list <- as.list(sp)
species_list <- set_names(species_list, sp)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Butterflies biotope"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
radioButtons("biotope",
label = "Biotope",
choices = biotopes_list),
selectInput("species",
label = "Species",
choices = species_list)
),
# Show a plot of the generated distribution
mainPanel(
textOutput("selected_choices")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$selected_choices <- renderText(sprintf("You have selected the biotope %s and the species %s",
input$biotope,
input$species))
}
# Run the application
shinyApp(ui = ui, server = server)
```
## Challenge 3
### Lynn:
```r
library(shiny)
library(tidyverse)
df <- read_csv("./20221006_butterflies_data.txt", na = "")
biotopes <- unique(df$biotope)
sp <- unique(df$species)
# Define UI for application that draws a histogram
ui <- fluidPage(
#Application title
titlePanel("Butterfly biotopes"),
#Sidebar input
sidebarLayout(sidebarPanel(radioButtons(inputId = "btp",
label = "Select biotope:",
choices = c("Agriculture" = biotopes[1],
"Forest" = biotopes[2],
"Open" = biotopes[3],
"Urban" = biotopes[4]),
selected = character(0)),
radioButtons(inputId = "sp",
label = "Select species:",
choices = c("Limenitis camilla" = sp[1],
"Pararge aegeria" = sp[2]),
selected = character(0))),
#Main panel
mainPanel(textOutput(outputId = "infoText"),
plotOutput(outputId = "bioPlot"),
tableOutput(outputId = "tbl")))
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$infoText <- renderText({
infoText <- paste0("You have selected the biotope ",
input$btp,
" and the species ",
input$sp,
".")
})
output$bioPlot <- renderPlot({
df %>%
filter(species == input$sp,
biotope == input$btp) %>%
ggplot(aes(x = year,
y = meanArea)) +
geom_point() +
geom_smooth() +
labs(title = paste("Distribution of ", input$sp, "- biotope:", input$btp),
y = "Area (%)") +
facet_wrap(~ region)
})
output$tbl <- renderTable({
df %>%
filter(species == input$sp,
biotope == input$btp)
},
striped = T,
bordered = T)
}
# Run the application
shinyApp(ui = ui, server = server)
```
### floris
Change `app.R` from challenge 2 as follows:
```diff
--- a/src/shinyapp_challenge_2_3/app.R
+++ b/src/shinyapp_challenge_2_3/app.R
@@ -8,6 +8,7 @@
#
library(shiny)
+library(ggplot2)
butter <- read.csv("../../data/20221006/20221006_butterflies_data.txt")
butter$species <- factor(butter$species)
@@ -32,7 +33,7 @@ ui <- fluidPage(
# Show a plot of the generated distribution
mainPanel(
- textOutput("textoutput")
+ plotOutput("plots")
)
)
)
@@ -40,12 +41,19 @@ ui <- fluidPage(
# Define server logic required to draw a histogram
server <- function(input, output) {
- output$textoutput <- renderText(
- paste("You selected biotope",
- input$buttons,
- "and species",
- input$dropdown)
- )
+ output$plots <- renderPlot({
+ ggplot(subset(butter,
+ species == input$dropdown &
+ biotope == input$buttons),
+ aes(x = year,
+ y = meanArea)) +
+ geom_point() +
+ geom_smooth() +
+ labs(title = paste("Distribution of", input$dropdown, "in biotope", input$buttons),
+ y = "Area (%)") +
+ facet_wrap(~ region)
+
+ })
}
# Run the application
```