Untitled

sara almoqbali_133624

2024-12-17

Introduction

The Shiny library in R is one of the most powerful tools for building interactive web applications with ease and efficiency. If you’re passionate about statistics and data analysis and want to share your results in a dynamic and engaging way, Shiny is your gateway to a world of interactivity and clarity.

Shiny allows you to transform traditional R scripts into interactive web applications without requiring advanced web development skills. It seamlessly combines the analytical power of R with user-friendly web interfaces, making your data more vibrant and accessible.

With Shiny, you can:

Upload and analyze data in real-time. Create dynamic visualizations and interactive tables. Perform statistical tests and display results instantly. What makes Shiny even more impressive is the ability to easily publish these applications online, allowing others to explore and interact with your data

Step 1: Package Discovery and Installation

Installing and Loading the Package

# Install shiny (if not already installed)
if (!requireNamespace("shiny", quietly = TRUE)) {
  install.packages("shiny")
}
library(shiny)
## Warning: package 'shiny' was built under R version 4.3.3

Exploring the Package Documentation

# Display package documentation
?shiny
## starting httpd help server ... done
# Browse package vignettes
browseVignettes("shiny")
## No vignettes found by browseVignettes("shiny")

Step 2: Analyzing the Package Structure

Listing Functions and Datasets

# List all exported functions
ls("package:shiny")
##   [1] "a"                        "absolutePanel"           
##   [3] "actionButton"             "actionLink"              
##   [5] "addResourcePath"          "animationOptions"        
##   [7] "appendTab"                "as.shiny.appobj"         
##   [9] "basicPage"                "bindCache"               
##  [11] "bindEvent"                "bookmarkButton"          
##  [13] "bootstrapLib"             "bootstrapPage"           
##  [15] "br"                       "browserViewer"           
##  [17] "brushedPoints"            "brushOpts"               
##  [19] "busyIndicatorOptions"     "callModule"              
##  [21] "captureStackTraces"       "checkboxGroupInput"      
##  [23] "checkboxInput"            "clickOpts"               
##  [25] "code"                     "column"                  
##  [27] "conditionalPanel"         "conditionStackTrace"     
##  [29] "conditionStackTrace<-"    "createRenderFunction"    
##  [31] "createWebDependency"      "dataTableOutput"         
##  [33] "dateInput"                "dateRangeInput"          
##  [35] "dblclickOpts"             "debounce"                
##  [37] "devmode"                  "dialogViewer"            
##  [39] "diskCache"                "div"                     
##  [41] "downloadButton"           "downloadHandler"         
##  [43] "downloadLink"             "em"                      
##  [45] "enableBookmarking"        "eventReactive"           
##  [47] "exportTestValues"         "exprToFunction"          
##  [49] "ExtendedTask"             "fileInput"               
##  [51] "fillCol"                  "fillPage"                
##  [53] "fillRow"                  "fixedPage"               
##  [55] "fixedPanel"               "fixedRow"                
##  [57] "flowLayout"               "fluidPage"               
##  [59] "fluidRow"                 "freezeReactiveVal"       
##  [61] "freezeReactiveValue"      "get_devmode_option"      
##  [63] "getCurrentOutputInfo"     "getCurrentTheme"         
##  [65] "getDefaultReactiveDomain" "getQueryString"          
##  [67] "getShinyOption"           "getUrlHash"              
##  [69] "h1"                       "h2"                      
##  [71] "h3"                       "h4"                      
##  [73] "h5"                       "h6"                      
##  [75] "headerPanel"              "helpText"                
##  [77] "hideTab"                  "hoverOpts"               
##  [79] "hr"                       "HTML"                    
##  [81] "htmlOutput"               "htmlTemplate"            
##  [83] "httpResponse"             "icon"                    
##  [85] "imageOutput"              "img"                     
##  [87] "in_devmode"               "includeCSS"              
##  [89] "includeHTML"              "includeMarkdown"         
##  [91] "includeScript"            "includeText"             
##  [93] "incProgress"              "inputPanel"              
##  [95] "insertTab"                "insertUI"                
##  [97] "installExprFunction"      "invalidateLater"         
##  [99] "is.key_missing"           "is.reactive"             
## [101] "is.reactivevalues"        "is.shiny.appobj"         
## [103] "is.singleton"             "isolate"                 
## [105] "isRunning"                "isTruthy"                
## [107] "key_missing"              "loadSupport"             
## [109] "mainPanel"                "makeReactiveBinding"     
## [111] "markdown"                 "markRenderFunction"      
## [113] "maskReactiveContext"      "memoryCache"             
## [115] "MockShinySession"         "modalButton"             
## [117] "modalDialog"              "moduleServer"            
## [119] "navbarMenu"               "navbarPage"              
## [121] "navlistPanel"             "nearPoints"              
## [123] "need"                     "NS"                      
## [125] "ns.sep"                   "numericInput"            
## [127] "observe"                  "observeEvent"            
## [129] "onBookmark"               "onBookmarked"            
## [131] "onFlush"                  "onFlushed"               
## [133] "onReactiveDomainEnded"    "onRestore"               
## [135] "onRestored"               "onSessionEnded"          
## [137] "onStop"                   "onUnhandledError"        
## [139] "outputOptions"            "p"                       
## [141] "pageWithSidebar"          "paneViewer"              
## [143] "parseQueryString"         "passwordInput"           
## [145] "plotOutput"               "plotPNG"                 
## [147] "pre"                      "prependTab"              
## [149] "printError"               "printStackTrace"         
## [151] "Progress"                 "quoToFunction"           
## [153] "radioButtons"             "reactive"                
## [155] "reactiveConsole"          "reactiveFileReader"      
## [157] "reactivePoll"             "reactiveTimer"           
## [159] "reactiveVal"              "reactiveValues"          
## [161] "reactiveValuesToList"     "reactlog"                
## [163] "reactlogAddMark"          "reactlogReset"           
## [165] "reactlogShow"             "register_devmode_option" 
## [167] "registerInputHandler"     "registerThemeDependency" 
## [169] "removeInputHandler"       "removeModal"             
## [171] "removeNotification"       "removeResourcePath"      
## [173] "removeTab"                "removeUI"                
## [175] "renderCachedPlot"         "renderDataTable"         
## [177] "renderImage"              "renderPlot"              
## [179] "renderPrint"              "renderTable"             
## [181] "renderText"               "renderUI"                
## [183] "repeatable"               "req"                     
## [185] "resourcePaths"            "restoreInput"            
## [187] "runApp"                   "runExample"              
## [189] "runGadget"                "runGist"                 
## [191] "runGitHub"                "runTests"                
## [193] "runUrl"                   "safeError"               
## [195] "selectInput"              "selectizeInput"          
## [197] "serverInfo"               "setBookmarkExclude"      
## [199] "setProgress"              "setSerializer"           
## [201] "shinyApp"                 "shinyAppDir"             
## [203] "shinyAppFile"             "shinyAppTemplate"        
## [205] "shinyOptions"             "shinyServer"             
## [207] "shinyUI"                  "showBookmarkUrlModal"    
## [209] "showModal"                "showNotification"        
## [211] "showTab"                  "sidebarLayout"           
## [213] "sidebarPanel"             "singleton"               
## [215] "sizeGrowthRatio"          "sliderInput"             
## [217] "snapshotExclude"          "snapshotPreprocessInput" 
## [219] "snapshotPreprocessOutput" "span"                    
## [221] "splitLayout"              "stopApp"                 
## [223] "strong"                   "submitButton"            
## [225] "suppressDependencies"     "tableOutput"             
## [227] "tabPanel"                 "tabPanelBody"            
## [229] "tabsetPanel"              "tag"                     
## [231] "tagAppendAttributes"      "tagAppendChild"          
## [233] "tagAppendChildren"        "tagGetAttribute"         
## [235] "tagHasAttribute"          "tagList"                 
## [237] "tags"                     "tagSetChildren"          
## [239] "testServer"               "textAreaInput"           
## [241] "textInput"                "textOutput"              
## [243] "throttle"                 "titlePanel"              
## [245] "uiOutput"                 "updateActionButton"      
## [247] "updateActionLink"         "updateCheckboxGroupInput"
## [249] "updateCheckboxInput"      "updateDateInput"         
## [251] "updateDateRangeInput"     "updateNavbarPage"        
## [253] "updateNavlistPanel"       "updateNumericInput"      
## [255] "updateQueryString"        "updateRadioButtons"      
## [257] "updateSelectInput"        "updateSelectizeInput"    
## [259] "updateSliderInput"        "updateTabsetPanel"       
## [261] "updateTextAreaInput"      "updateTextInput"         
## [263] "updateVarSelectInput"     "updateVarSelectizeInput" 
## [265] "urlModal"                 "useBusyIndicators"       
## [267] "validate"                 "validateCssUnit"         
## [269] "varSelectInput"           "varSelectizeInput"       
## [271] "verbatimTextOutput"       "verticalLayout"          
## [273] "wellPanel"                "with_devmode"            
## [275] "withLogErrors"            "withMathJax"             
## [277] "withProgress"             "withReactiveDomain"      
## [279] "withTags"

Step 3: Solving a Practical Problem

We will create a simple Shiny app that lets users interactively plot a subset of the built-in mtcars dataset.

library(shiny)

# Define UI
ui <- fluidPage(
  titlePanel("Simple Shiny App"),
  sidebarLayout(
    sidebarPanel(
      selectInput("dataset", "Choose a dataset:", 
                  choices = c("mtcars", "iris"))
    ),
    mainPanel(
      tableOutput("table")
    )
  )
)

# Define server logic
server <- function(input, output) {
  output$table <- renderTable({
    dataset <- get(input$dataset)
    head(dataset)  # Show the first few rows of the selected dataset
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

Step 4: Extending the Package

Creating a Custom Function

Adding Custom Features We will enhance the app by allowing the user to filter rows based on mtcars variables.

# Load required packages
library(shiny)
library(ggplot2)  # Ensure ggplot2 is loaded

# Updated UI with filtering options
ui <- fluidPage(
  titlePanel("Enhanced MTCARS App"),
  sidebarLayout(
    sidebarPanel(
      selectInput("xvar", "X-axis variable:", choices = names(mtcars)),
      selectInput("yvar", "Y-axis variable:", choices = names(mtcars)),
      sliderInput("pointSize", "Point Size:", min = 1, max = 10, value = 3),
      sliderInput("cylFilter", "Number of Cylinders:", 
                  min = min(mtcars$cyl), max = max(mtcars$cyl), 
                  value = c(min(mtcars$cyl), max(mtcars$cyl)))
    ),
    mainPanel(
      plotOutput("scatterPlot")
    )
  )
)

# Updated Server with filtering logic
server <- function(input, output) {
  filteredData <- reactive({
    mtcars[mtcars$cyl >= input$cylFilter[1] & mtcars$cyl <= input$cylFilter[2], ]
  })

  output$scatterPlot <- renderPlot({
    ggplot(filteredData(), aes_string(x = input$xvar, y = input$yvar)) +
      geom_point(size = input$pointSize, color = "blue") +
      theme_minimal() +
      labs(
        title = paste("Scatter Plot of", input$yvar, "vs", input$xvar),
        x = input$xvar,
        y = input$yvar
      )
  })
}

# Run the enhanced app
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

Conclusion

In this exercise, we:

Explored the shiny package and its documentation. Created a basic interactive app using shiny. Extended the app to include filtering and custom interactivity. This exercise demonstrates the ability of the shiny package to build interactive and responsive applications for data analysis and visualization.