shiny is a popular web application framework for R, which allows you to create interactive web applications using R code. With shiny, you can create custom dashboards, data visualizations, and other web-based tools that allow users to interact with data in real-time.

Shiny is built on top of R, and allows you to use your existing R code to create web applications. In this chapter, we will provide an overview of Shiny, its key features, and how to get started with building Shiny applications.

12.1 What is Shiny?

shiny is a web application framework for R that allows you to build interactive web applications using R code. It provides a set of tools and components that allow you to create custom user interfaces, data visualizations, and other web-based tools. Shiny applications can be used for a wide range of purposes, from data analysis and visualization to predictive modeling and machine learning.

One of the key features of shiny is its reactive programming model, which allows you to create web applications that update in real-time based on user input. This means that your web application can respond to changes in user input without needing to reload the entire page.

12.2 Key Features of Shiny

  1. Reactive Programming Model: Shiny uses a reactive programming model that allows you to create web applications that update in real-time based on user input. This means that your application can respond to changes in user input without needing to reload the entire page.

  2. Custom User Interfaces: Shiny provides a set of tools and components that allow you to create custom user interfaces for your web applications. You can use a variety of input widgets, such as sliders, drop-down menus, and text boxes, to allow users to interact with your data.

  3. Data Visualization: Shiny allows you to create interactive data visualizations using R packages like ggplot2 and plotly. You can use these packages to create custom charts, maps, and other visualizations that update in real-time based on user input.

  4. Data Input and Output: Shiny provides a set of tools for reading and writing data from a variety of sources, including files, databases, and web APIs. This allows you to build web applications that can import and export data in a variety of formats.

12.3 How to Build Shiny Applications

Building a Shiny application typically involves the following steps:

Defining UI: Define the user interface of your application using Shiny’s UI components. This can include input widgets, data visualizations, and other components.

Defining Server Logic: Define the server logic for your application using Shiny’s reactive programming model. This involves defining reactive expressions and event handlers that update the application in real-time based on user input.

Running the Application: Run the Shiny application using the shinyApp() function, which combines the UI and server logic into a single application object.

Getting Started with Shiny To get started with Shiny, you will need to install the shiny package, which is available on CRAN. Once you have installed the package, you can create a new Shiny application using the shinyApp() function.

Here is an example of a simple Shiny application point and click any point:

Code
knitr::include_app("https://jkylearmstrong.shinyapps.io/Shiny/", height = "600px")

After the yaml which looks like this:

There are four main components this app: setup , ui, server, & runapp

12.4 setup code chuck

Code
knitr::opts_chunk$set(echo = TRUE)
library('tidyverse')

A_DATA_TBL_2.t_ks_result.furrr <- readRDS(here::here('/DATA/A_DATA_week_4_cont_EDA.RDS')) %>%
  select(Feature, mean_diff_est , ttest.pvalue, kstest.pvalue, N_Target, mean_Target, sd_Target, N_Control, mean_Control, sd_Control)

A_DATA_2 <- readRDS(here::here('/DATA/A_DATA.RDS'))
features <- colnames(A_DATA_2)[!colnames(A_DATA_2) %in% c('SEQN', 'DIABETES','AGE_AT_DIAG_DM2')]

numeric_cols <- A_DATA_2 %>%
  select_if(is.numeric) %>%
  colnames()

numeric_features <- intersect(features, numeric_cols)

source(here::here("/FUNCTIONS/wrapper_t_ks_test.R"))

We then have a pair a ui chunk and server chunk:

12.5 ui code chunk:

Code
ui <- fluidPage(
  
  sidebarLayout(
    
    #  input
    sidebarPanel(
      plotOutput("plot1", click = "plot_click")
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      tableOutput("info"), 
      plotOutput("plot2")
    )
  )
  
)

12.6 sever code chunk:

Code
server <- function(input, output) {
  output$plot1 <- renderPlot({
    A_DATA_TBL_2.t_ks_result.furrr %>%
      ggplot(aes(x=round(ttest.pvalue,4) , y= round(kstest.pvalue,4), color = Feature)) +
      geom_point() + 
      theme(legend.position='none')
  })
  
  output$info <- renderTable({
    nearPoints(A_DATA_TBL_2.t_ks_result.furrr, 
               input$plot_click,
               xvar = "ttest.pvalue", 
               yvar = "kstest.pvalue") %>%
      as.data.frame()
  })
  
  output$plot2 <- renderPlot({
    my_feature <- (nearPoints(A_DATA_TBL_2.t_ks_result.furrr, 
                              input$plot_click,
                              xvar = "ttest.pvalue", 
                              yvar = "kstest.pvalue"))$Feature
    
    A_DATA_2 %>%
      filter(!is.na(DIABETES))%>%
      mutate(DIABETES_factor = as.factor(DIABETES)) %>%
      ggplot(aes_string(x = my_feature , fill='DIABETES_factor')) + 
      geom_density()
  })
}

12.7 runapp code chunk

Code
shinyApp(ui, server)