-
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.
- Loading branch information
mictes
committed
Apr 9, 2024
1 parent
13c294f
commit 97ab96b
Showing
11 changed files
with
390 additions
and
7 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 @@ | ||
.DS_Store |
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,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) |
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 @@ | ||
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() | ||
|
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,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) |
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,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) |
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
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
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,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) |
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,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) |
Oops, something went wrong.