-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved hass app
- Loading branch information
Showing
24 changed files
with
937 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Creating apps with R-Shiny by Albert Rapp | ||
|
||
# Anatomy of a Shiny App | ||
|
||
# 1) User Interface (UI): What your user sees in the app. | ||
# 2) Server function: The brains behind the operation (more on that later). | ||
# 3) ShinyApp() function call that ties UI & server together. | ||
|
||
# As you’ve seen in the code, every input function needs three things: | ||
|
||
# 1) First an ID, | ||
# 2) A label of that UI element, | ||
# 3) Followed by input-specific arguments | ||
|
||
library(shiny) | ||
|
||
ui <- bslib::page_fluid( | ||
# A slider | ||
sliderInput( | ||
'my_slider', | ||
'Select your number', | ||
|
||
min = 0, | ||
max = 1, | ||
value = 0.5, | ||
step = 0.1 | ||
), | ||
|
||
# A dropdown menu | ||
selectInput( | ||
'my_dropdown_menu', | ||
'Pick your color', | ||
|
||
choices = c('red', 'green', 'blue') | ||
), | ||
|
||
# Add a text output | ||
textOutput('my_generated_text') | ||
) | ||
|
||
server <- function(input, output, session){ | ||
output$my_generated_text <- renderText({ | ||
paste( | ||
input$my_dropdown_menu, | ||
input$my_slider | ||
) | ||
}) | ||
} | ||
|
||
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Creating apps with R-Shiny by Albert Rapp | ||
# Website: https://3mw.albert-rapp.de/p/r-shiny-events | ||
|
||
# Event listeners with R-Shiny | ||
|
||
# Setting up a simple shiny app | ||
|
||
# First, let us set up a little Shiny app. Let’s throw in | ||
|
||
# 1) A slider input using sliderInput(), | ||
# 2) An action button using actionButton(), and | ||
# 3) A text output using textOutput(). | ||
|
||
library(shiny) | ||
|
||
ui <- bslib::page_fluid( | ||
sliderInput('slider', 'Slider', | ||
min = 1, max = 100, value = 50), | ||
actionButton('button', 'Button'), | ||
textOutput('text') | ||
) | ||
|
||
# req() helps one control reactivity | ||
# isolate() helps one to isolate inputs | ||
# bindEvent() is more useful than isolate() and observeEvent() | ||
|
||
server <- function(input, output, session){ | ||
output$text <- renderText({ | ||
req(input$button >= 1) | ||
print(input$button) | ||
input$slider | ||
}) |> bindEvent(input$button) | ||
} | ||
|
||
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Creating apps with R-Shiny by Albert Rapp | ||
# Website: https://3mw.albert-rapp.de/p/r-shiny-events | ||
|
||
# Reactive expressions in R-Shiny | ||
|
||
# Setting up a simple shiny app | ||
|
||
# 1) bslib::layout_column_wrap() to place things next to each other. | ||
# 2) {DT} packages to create a table output. | ||
|
||
library(shiny) | ||
|
||
ui <- bslib::page_fluid( | ||
bslib::layout_column_wrap( | ||
selectInput( | ||
'species', | ||
'Choose your species', | ||
unique(palmerpenguins::penguins$species) | ||
), | ||
selectInput( | ||
'island', | ||
'Choose your island', | ||
unique(palmerpenguins::penguins$island) | ||
) | ||
), | ||
bslib::layout_column_wrap( | ||
plotOutput('plot'), | ||
DT::dataTableOutput('tbl') | ||
) | ||
) | ||
|
||
|
||
library(tidyverse) | ||
server <- function(input, output, session) { | ||
output$plot <- renderPlot({ | ||
palmerpenguins::penguins |> | ||
filter( | ||
!is.na(sex), | ||
species == input$species, | ||
island == input$island | ||
) |> | ||
ggplot(aes(x = flipper_length_mm, y = bill_depth_mm)) + | ||
geom_point(size = 3) | ||
}) | ||
|
||
output$tbl <- DT::renderDT({ | ||
palmerpenguins::penguins |> | ||
filter( | ||
!is.na(sex), | ||
species == input$species, | ||
island == input$island | ||
) | ||
}) | ||
} | ||
|
||
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Creating apps with R-Shiny by Albert Rapp | ||
# Website: https://3mw.albert-rapp.de/p/r-shiny-events | ||
|
||
# Reactive expressions in R-Shiny | ||
|
||
# Setting up a simple shiny app | ||
|
||
# 1) Avoid code repetition. | ||
# 2) The easiest way to do that is the reactive() function. | ||
|
||
library(shiny) | ||
|
||
ui <- bslib::page_fluid( | ||
bslib::layout_column_wrap( | ||
selectInput( | ||
'species', | ||
'Choose your species', | ||
unique(palmerpenguins::penguins$species) | ||
), | ||
selectInput( | ||
'island', | ||
'Choose your island', | ||
unique(palmerpenguins::penguins$island) | ||
) | ||
), | ||
bslib::layout_column_wrap( | ||
plotOutput('plot'), | ||
DT::dataTableOutput('tbl') | ||
) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
filtered_data <- reactive({ | ||
palmerpenguins::penguins |> | ||
filter( | ||
!is.na(sex), | ||
species == input$species, | ||
island == input$island | ||
) | ||
}) | ||
|
||
output$plot <- renderPlot({ | ||
# CALL the reactive | ||
filtered_data() |> | ||
ggplot(aes(x = flipper_length_mm, y = bill_depth_mm)) + | ||
geom_point(size = 3) | ||
}) | ||
|
||
output$tbl <- DT::renderDT({ | ||
# CALL the reactive | ||
filtered_data() | ||
}) | ||
} | ||
|
||
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Creating apps with R-Shiny by Albert Rapp | ||
# Website: https://3mw.albert-rapp.de/p/r-shiny-events | ||
|
||
# Reactive expressions in R-Shiny | ||
|
||
# Setting up a simple shiny app | ||
|
||
# 1) reactiveVal() vs reactive() | ||
# 2) There are other ways to create reactive values. | ||
# 3) For example, there’s also reactiveVal(). | ||
# This function lets you assign values more manually. | ||
# 4) Requires an observer for input values | ||
# - define the reactiveVal() outside the observe() code, and | ||
# - update the reactiveVal() inside of observe(). | ||
|
||
library(shiny) | ||
|
||
ui <- bslib::page_fluid( | ||
bslib::layout_column_wrap( | ||
selectInput( | ||
'species', | ||
'Choose your species', | ||
unique(palmerpenguins::penguins$species) | ||
), | ||
selectInput( | ||
'island', | ||
'Choose your island', | ||
unique(palmerpenguins::penguins$island) | ||
) | ||
), | ||
bslib::layout_column_wrap( | ||
plotOutput('plot'), | ||
DT::dataTableOutput('tbl') | ||
) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
# Intialize reactiveVal() | ||
filtered_data <- reactiveVal() | ||
observe({ | ||
dat <- palmerpenguins::penguins |> | ||
filter( | ||
!is.na(sex), | ||
species == input$species, | ||
island == input$island | ||
) | ||
filtered_data(dat) # update the reactiveVal() | ||
}) | ||
|
||
output$plot <- renderPlot({ | ||
filtered_data() |> | ||
ggplot(aes(x = flipper_length_mm, y = bill_depth_mm)) + | ||
geom_point(size = 3) | ||
}) | ||
|
||
output$tbl <- DT::renderDT({ | ||
filtered_data() | ||
}) | ||
} | ||
|
||
|
||
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Creating apps with R-Shiny by Albert Rapp | ||
# Website: https://3mw.albert-rapp.de/p/r-shiny-events | ||
|
||
# Reactive expressions in R-Shiny | ||
|
||
# Setting up a simple shiny app | ||
|
||
# 1) Use reactiveValues() function if you don't like the syntax of | ||
# reactiveVal() | ||
|
||
library(shiny) | ||
|
||
ui <- bslib::page_fluid( | ||
bslib::layout_column_wrap( | ||
selectInput( | ||
'species', | ||
'Choose your species', | ||
unique(palmerpenguins::penguins$species) | ||
), | ||
selectInput( | ||
'island', | ||
'Choose your island', | ||
unique(palmerpenguins::penguins$island) | ||
) | ||
), | ||
bslib::layout_column_wrap( | ||
plotOutput('plot'), | ||
DT::dataTableOutput('tbl') | ||
) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
# Intialize list | ||
r_list <- reactiveValues() | ||
observe({ | ||
dat <- palmerpenguins::penguins |> | ||
filter( | ||
!is.na(sex), | ||
species == input$species, | ||
island == input$island | ||
) | ||
r_list$filtered_data <- dat # update | ||
}) | ||
|
||
output$plot <- renderPlot({ | ||
# use data from list (without parantheses) | ||
r_list$filtered_data |> | ||
ggplot(aes(x = flipper_length_mm, y = bill_depth_mm)) + | ||
geom_point(size = 3) | ||
}) | ||
|
||
output$tbl <- DT::renderDT({ | ||
# use data from list (without parantheses) | ||
r_list$filtered_data | ||
}) | ||
} | ||
|
||
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Creating apps with R-Shiny by Albert Rapp | ||
# Website: https://3mw.albert-rapp.de/p/shiny-dynamic-ui | ||
|
||
# Dynamic UI with R-Shiny | ||
|
||
# Insert and remove UI (Part 1) | ||
|
||
# The first way to create a dynamic UI experience is pretty easy. | ||
|
||
library(shiny) | ||
|
||
ui <- bslib::page_fluid( | ||
radioButtons( | ||
'yesno', | ||
'Do you like this? ', | ||
c('Yes', 'No'), | ||
selected = character(0) # no selection at first | ||
) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
|
||
} | ||
|
||
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Creating apps with R-Shiny by Albert Rapp | ||
# Website: https://3mw.albert-rapp.de/p/shiny-dynamic-ui | ||
|
||
# Dynamic UI with R-Shiny | ||
|
||
# Insert and remove UI (Part 2) | ||
|
||
# Depending on if “Yes” or “No” is selected, you want to throw in some specific | ||
# part into your user interface. In these cases, you can use the insertUI() function | ||
|
||
library(shiny) | ||
|
||
ui <- bslib::page_fluid( | ||
radioButtons( | ||
'yesno', | ||
'Do you like this? ', | ||
c('Yes', 'No'), | ||
selected = character(0) # no selection at first | ||
) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
observe({ | ||
if (input$yesno == 'Yes') { | ||
insertUI( | ||
# Place new UI after the radio button | ||
# (the element with the id 'yesno') | ||
selector = '#yesno', | ||
where = 'afterEnd', | ||
# What ui to insert | ||
ui = sliderInput( | ||
'slider', | ||
'How much do you like this (10 = very)', | ||
min = 1, | ||
max = 10, | ||
value = 5 | ||
) | ||
) | ||
} else { | ||
insertUI( | ||
selector = '#yesno', | ||
where = 'afterEnd', | ||
ui = textInput('textinput', 'Why not?') | ||
) | ||
} | ||
}) |> bindEvent(input$yesno) | ||
} | ||
|
||
shinyApp(ui, server) |
Oops, something went wrong.