Skip to content

Commit

Permalink
changed folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mictes committed Apr 9, 2024
1 parent 13c294f commit 97ab96b
Show file tree
Hide file tree
Showing 11 changed files with 390 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
39 changes: 39 additions & 0 deletions R_Code/exercises/01_calculator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
library(shiny)

# Define UI
ui <- fluidPage(
titlePanel(""),

sidebarLayout(
sidebarPanel(
# input here
# check: https://shiny.posit.co/r/gallery/widgets/widget-gallery/
),

mainPanel(
# output here
# check: https://github.com/rstudio/cheatsheets/blob/main/shiny.pdf
)
)
)

# Define server logic
server <- function(input, output) {

output$result <- renderText({
num1 <- 2
num2 <- 1

result <- switch(input$operation,
"add" = num1 + num2,
"subtract" = num1 - num2,
"multiply" = num1 * num2,
"divide" = num1 / num2
)

paste("Result:", result)
})
}

# Run the application
shinyApp(ui = ui, server = server)
55 changes: 55 additions & 0 deletions R_Code/misc/k_means_reactlog.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
library(shiny)
library(reactlog)

reactlog_enable()

ui <- function() {

# k-means only works with numerical variables,
# so don't give the user the option to select
# a categorical variable
vars <- setdiff(names(iris), "Species")

pageWithSidebar(
headerPanel('Iris k-means clustering'),
sidebarPanel(
selectInput('xcol', 'X Variable', vars),
selectInput('ycol', 'Y Variable', vars, selected = vars[[2]]),
numericInput('clusters', 'Cluster count', 3, min = 1, max = 9)
),
mainPanel(
plotOutput('plot1')
)
)}




server <- function(input, output, session) {

# Combine the selected variables into a new data frame
selectedData <- reactive({
iris[, c(input$xcol, input$ycol)]
})

clusters <- reactive({
kmeans(selectedData(), input$clusters)
})

output$plot1 <- renderPlot({
palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))

par(mar = c(5.1, 4.1, 0, 1))
plot(selectedData(),
col = clusters()$cluster,
pch = 20, cex = 3)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})

}

shinyApp(ui = ui, server = server)

shiny::reactlogShow()

46 changes: 46 additions & 0 deletions R_Code/solutions/01_calculator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
library(shiny)

# Define UI
ui <- fluidPage(
titlePanel("Shiny Calculator"),

sidebarLayout(
sidebarPanel(
numericInput("num1", label = "Enter first number:", value = 0),
numericInput("num2", label = "Enter second number:", value = 0),
radioButtons("operation", label = "Choose operation:", choices = list(
"Add" = "add",
"Subtract" = "subtract",
"Multiply" = "multiply",
"Divide" = "divide"
))
),

mainPanel(
textOutput("result")
)
)
)

# Define server logic
server <- function(input, output) {

output$result <- renderText({
if (input$operation == "divide") {
req(input$num2 != 0)
}

result <- switch(input$operation,
"add" = input$num1 + input$num2,
"subtract" = input$num1 - input$num2,
"multiply" = input$num1 * input$num2,
"divide" = input$num1 / input$num2
)

paste("Result:", result)
})

}

# Run the application
shinyApp(ui = ui, server = server)
51 changes: 51 additions & 0 deletions R_Code/solutions/01_calculator_reactive.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
library(shiny)

# Define UI
ui <- fluidPage(
titlePanel("Shiny Calculator"),

sidebarLayout(
sidebarPanel(
numericInput("num1", "Enter first number:", value = 0),
numericInput("num2", "Enter second number:", value = 0),
radioButtons("operation", "Choose operation:",
choices = list("Add" = "add",
"Subtract" = "subtract",
"Multiply" = "multiply",
"Divide" = "divide"))
),

mainPanel(
textOutput("result")
)
)
)

# Define server logic
server <- function(input, output) {

result <- reactive({
# Handling division by zero with req()
if (input$operation == "divide") {
req(input$num2 != 0)
}

switch(input$operation,
"add" = input$num1 + input$num2,
"subtract" = input$num1 - input$num2,
"multiply" = input$num1 * input$num2,
"divide" = input$num1 / input$num2
)
})

print(input$num1)
# print(result())

output$result <- renderText({
paste("Result:", result())
})

}

# Run the application
shinyApp(ui = ui, server = server)
4 changes: 2 additions & 2 deletions Slides/01_Introduction.html
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ <h2>Output Rendering functions</h2>
<h2 class="highlight">Exercise 1</h2>
<p><strong>Resources:</strong></p>
<ul>
<li class="fragment">Exercises/<a href="../Exercises/1_calculator.R">1_calculator.R</a></li>
<li class="fragment">Exercises/<a href="../R_Code/exercises/01_calculator.R">1_calculator.R</a></li>
<li class="fragment">Input widgets: <a href="https://shiny.posit.co/r/gallery/widgets/widget-gallery/" target="_blank">Shiny widget gallery</a></li>
<li class="fragment">Render functions: <a href="https://github.com/rstudio/cheatsheets/blob/main/shiny.pdf" target="_blank">Shiny CHEATSHEET</a></li>
</ul>
Expand Down Expand Up @@ -1170,7 +1170,7 @@ <h2>Best practices</h2>
<li class="fragment">This also helps to avoid the accidental creation of loops</li>
<li class="fragment">Use <span class="highlight">req()</span> to avoid errors due to missing inputs, especially when opening the application</li>
<li class="fragment">Carefully check the IDs of the inputs and outputs</li>
<li class="fragment">Troubleshooting: use the <a href="../Misc/k_means_reactlog.R">library(reactlog)</a></li>
<li class="fragment">Troubleshooting: use the <a href="../R_Code/misc/k_means_reactlog.R">library(reactlog)</a></li>
<li class="fragment">Modularise, i.e. store ui and server as variables. This for example allows you to dynamically load parts of the UI later.</li>
</ul>
</section>
Expand Down
10 changes: 5 additions & 5 deletions Slides/02_ggplot.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<div class="reveal">
<div class="slides">

<section class="title">
<section class="title" data-menu-title="ggplot2 recap">
<h1 class="shiny">Interactive Data Visualization with R Shiny for Life Sciences</h1>
<h2>Day 1: ggplot2 recap</h2>
<strong>Michael Teske &amp; Jonas Schmid, Zurich 08 April 2024</strong>
Expand Down Expand Up @@ -228,7 +228,7 @@ <h3>Resources</h3>
</div>
</section>

<section data-auto-animate>
<section data-auto-animate data-menu-title="Violin plot">
<h2>Let's create a plot!</h2>
<div class="panel" style="width: 45%;">
<img src="materials/images/Iris_violin_point.png" alt="Iris_ViolinPlot" data-id="Iris_plot" width="600">
Expand Down Expand Up @@ -274,7 +274,7 @@ <h2>Let's create a plot!</h2>
</div>
</section>

<section data-auto-animate>
<section data-auto-animate data-menu-title="Volcano plot">
<h2>Let's create a plot! - Variant 2</h2>
<div class="panel" style="width: 45%;">
<img src="materials/images/Ruhland2016_volcano.png" alt="Ruhland2016_VolcanoPlot" data-id="Ruhland2016_plot" width="700">
Expand Down Expand Up @@ -321,7 +321,7 @@ <h2>Let's create a plot! - Variant 2</h2>
<h2>Questions?</h2>
</section>

<section>
<section data-menu-title="Exercise 2">
<h2 class="highlight">Exercise 2</h2>
<p><strong>Task:</strong> Create a plot with ggplot2!</p>
<div class="panel" style="width: 25%;">
Expand Down Expand Up @@ -358,7 +358,7 @@ <h2 class="highlight">Exercise 2</h2>
# Load data
ruhland2016 &lt;- read.csv('Ruhland2016.csv', row.names=1)

# Mark differentially expressed genes - HINT: Wrap this part in a reactive({})!
# Mark differentially expressed genes - Wrap this part in a reactive({})
log2FCthreshold &lt;- log2(1.5)
p_threshold &lt;- 0.05

Expand Down
39 changes: 39 additions & 0 deletions Slides/code/exercises/1_calculator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
library(shiny)

# Define UI
ui <- fluidPage(
titlePanel(""),

sidebarLayout(
sidebarPanel(
# input here
# check: https://shiny.posit.co/r/gallery/widgets/widget-gallery/
),

mainPanel(
# output here
# check: https://github.com/rstudio/cheatsheets/blob/main/shiny.pdf
)
)
)

# Define server logic
server <- function(input, output) {

output$result <- renderText({
num1 <- 2
num2 <- 1

result <- switch(input$operation,
"add" = num1 + num2,
"subtract" = num1 - num2,
"multiply" = num1 * num2,
"divide" = num1 / num2
)

paste("Result:", result)
})
}

# Run the application
shinyApp(ui = ui, server = server)
46 changes: 46 additions & 0 deletions Slides/code/exercises/solutions/1_calculator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
library(shiny)

# Define UI
ui <- fluidPage(
titlePanel("Shiny Calculator"),

sidebarLayout(
sidebarPanel(
numericInput("num1", label = "Enter first number:", value = 0),
numericInput("num2", label = "Enter second number:", value = 0),
radioButtons("operation", label = "Choose operation:", choices = list(
"Add" = "add",
"Subtract" = "subtract",
"Multiply" = "multiply",
"Divide" = "divide"
))
),

mainPanel(
textOutput("result")
)
)
)

# Define server logic
server <- function(input, output) {

output$result <- renderText({
if (input$operation == "divide") {
req(input$num2 != 0)
}

result <- switch(input$operation,
"add" = input$num1 + input$num2,
"subtract" = input$num1 - input$num2,
"multiply" = input$num1 * input$num2,
"divide" = input$num1 / input$num2
)

paste("Result:", result)
})

}

# Run the application
shinyApp(ui = ui, server = server)
Loading

0 comments on commit 97ab96b

Please sign in to comment.