diff --git a/.gitignore b/.gitignore index e0e05d1060cf6ff02929407ef833e0f9dd9ed1b4..02209e54fbd6c64578ed652696837b6868c9360a 100644 --- a/.gitignore +++ b/.gitignore @@ -328,5 +328,6 @@ $RECYCLE.BIN/ _output/ .quarto/ +/*.html /.quarto/ diff --git a/.vscode/settings.json b/.vscode/settings.json index dd1c032963c5882592b12ef84e5c961bf15cb634..984705f498929e20d30d2624f2fe8c3b7aa362b3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -50,6 +50,14 @@ "editor.insertSpaces": true, "editor.tabSize": 4 }, + "[r]": { + "editor.defaultFormatter": "REditorSupport.r", + "editor.formatOnSave": false, + "editor.indentSize": 2, + "editor.insertSpaces": true, + "editor.tabSize": 2, + "files.eol": "\n" + }, "[snippets]": { "editor.defaultFormatter": "vscode.json-language-features", "editor.formatOnSave": true, diff --git a/README.md b/README.md index 3fed9ec320a7ee4e8973add70636ee4f67e060f7..c036647b8959f634e9ec1630546b43d03c867740 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ 1. `install.packages("shiny")` 1. Any other packages you need for the code you're using. This example repo needs the following. - `install.packages("ggplot2")` +1. Use `quarto serve shiny.rmd` to run the Shiny example. + - Theming currently does not work ### VSCode Integration diff --git a/shiny.qmd b/shiny.qmd new file mode 100644 index 0000000000000000000000000000000000000000..5389f0adb12e5bf7e24f09f35bcee7167cd569d2 --- /dev/null +++ b/shiny.qmd @@ -0,0 +1,75 @@ +--- +title: Shiny Example +server: shiny # necessary for shiny interactive elements +format: + revealjs: + # CREATION METADATA + author: + name: Research Computing + affilitation: UAB + copyright: "© University of Alabama at Birmingham" + date: today # default date + date-format: YYYY-MM-DD + # OPTIONS - CONTROLS + controls: true + controls-layout: bottom-right + controls-back-arrows: faded + # OPTIONS - FEATURES + code-annotation: select + title-slide-attributes: + data-align: left + # + # DEVELOPER ONLY + width: 1280 + height: 720 + auto-stretch: false + history: true + menu: false + progress: true + revealjs-plugins: + - rStackFix + # DEVELOPER ONLY - STYLE AND THEME + highlight-style: runtime/theme/uab-rc.theme # code highlighting + css: + - runtime/theme/uab-rc.css # uab theming + - runtime/theme/fix-r-stack-hack.css # fixes r-stack div + include-in-header: + - runtime/includes/head.html + include-after-body: + - runtime/includes/footer.html +--- + +## Direct Code Example - R + +```{r} +print("Hello World!") +``` + +## Included Code Example - R + +```{r} +{{< include src/code-example.R >}} +``` + +## Interactive Iris Dataset - Shiny + +<!-- UI Sidebar --> + +```{r} +#| panel: sidebar +{{< include src/shiny-ui-sidebar.R >}} +``` + +<!-- UI Plot --> + +```{r} +#| panel: fill +{{< include src/shiny-ui-plot.R >}} +``` + +<!-- Local server backend code--> + +```{r} +#| context: server +{{< include src/shiny-ui-server.R >}} +``` diff --git a/src/code-example.R b/src/code-example.R new file mode 100644 index 0000000000000000000000000000000000000000..9242a670d1770f6404969f639754d2d7cc06e3de --- /dev/null +++ b/src/code-example.R @@ -0,0 +1,18 @@ +library(ggplot2) + +x <- 10 +y <- 5 +z <- x + y +mean_value <- mean(c(x, y, z)) + +person <- setRefClass("Person", + fields = list(name = "character"), + methods = list( + greet = function() paste("Hello, my name is", name) + ) +) +john <- person$new(name = "John") +greeting <- john$greet() + +add_numbers <- function(a, b) a + b +sum_result <- add_numbers(x, y) diff --git a/src/shiny-ui-plot.R b/src/shiny-ui-plot.R new file mode 100644 index 0000000000000000000000000000000000000000..8cf4ec5102a4249a936474f71f5dd4f34d94ca34 --- /dev/null +++ b/src/shiny-ui-plot.R @@ -0,0 +1 @@ +plotOutput("plot1") diff --git a/src/shiny-ui-server.R b/src/shiny-ui-server.R new file mode 100644 index 0000000000000000000000000000000000000000..089ff8a2d94173e15fbac00fa54bd8880b78788a --- /dev/null +++ b/src/shiny-ui-server.R @@ -0,0 +1,21 @@ +selected_data <- reactive({ + iris[, c(input$xcol, input$ycol)] +}) + +clusters <- reactive({ + kmeans(selected_data(), 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(selected_data(), + col = clusters()$cluster, + pch = 20, cex = 3 + ) + points(clusters()$centers, pch = 4, cex = 4, lwd = 4) +}) diff --git a/src/shiny-ui-sidebar.R b/src/shiny-ui-sidebar.R new file mode 100644 index 0000000000000000000000000000000000000000..e71316bea65ea716b910d54bdb15d55d4e3ac263 --- /dev/null +++ b/src/shiny-ui-sidebar.R @@ -0,0 +1,4 @@ +vars <- setdiff(names(iris), "Species") +selectInput("xcol", "X Variable", vars) +selectInput("ycol", "Y Variable", vars, selected = vars[[2]]) +numericInput("clusters", "Cluster count", 3, min = 1, max = 9)