From 33248acb1033be5d8771395bd53940232b3209f5 Mon Sep 17 00:00:00 2001 From: Gammerdinger Date: Tue, 16 Jul 2024 16:51:09 -0400 Subject: [PATCH] Initiated, datatable and original plot --- RShiny/lessons/03_visuals.md | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 RShiny/lessons/03_visuals.md diff --git a/RShiny/lessons/03_visuals.md b/RShiny/lessons/03_visuals.md new file mode 100644 index 0000000..1a02bd4 --- /dev/null +++ b/RShiny/lessons/03_visuals.md @@ -0,0 +1,76 @@ +--- +title: "Visualization and Interactive Visuals in RShiny" +author: "Will Gammerdinger" +--- + +# Learning Objectives + +In this lesson, you will: +- Create a data table output +- Create a plot output +- Create an interactive plot + +# Creating a data table output + +Within your Shiny app you might like to have a datatable. Shiny has some native datatable functions (`dataTableOutput()` for the UI side and `renderDataTable()` for the server side). However, these function are depreciated and in their documentation, they recommend using the `DT` package for handling datatables. When you load `DT` it will mask the base Shiny functions `dataTableOutput()` with `DTOutput()` and `renderDataTable()` with `renderDT()`. To render a table: + +On the UI side: + +``` +DTOutput("inputID") +``` + +On the server side: +``` +output$ <- renderDT({ + + }) +``` + +We can visualize an example of a data table within a Shiny app using the code below and the built-in dataset `mtcars`: + +``` +library(shiny) +library(DT) + +ui <- fluidPage( + checkboxGroupInput("column_input", "Select columns", choices = colnames(mtcars), inline = TRUE), + DTOutput("table") +) + +server <- function(input, output) { + output$table <- renderDT({ + mtcars[,input$column_input, drop = FALSE] + }) +} + +shinyApp(ui = ui, server = server) +``` + +This will visualize in the app as: + +

+ + + +# Creating a plot + +``` +library(shiny) +library(ggplot2) + +ui <- fluidPage( + selectInput("x_axis_input", "Select x-axis", choices = colnames(mtcars)), + selectInput("y_axis_input", "Select y-axis", choices = colnames(mtcars), selected = "disp"), + plotOutput("plot") +) + +server <- function(input, output) { + output$plot <- renderPlot({ + ggplot(mtcars) + + geom_point(aes_string(x = input$x_axis_input, y = input$y_axis_input)) + }) +} + +shinyApp(ui = ui, server = server) +```