R for
Authoring!!

PSP Session #27…
…Shiny on the Down Low(d)

Problem-Solving Panel
(May 28, 2024)

  • Recap session #26       ↔︎️
  • Focal issue:
    • Shiny downloads
      • Figures & Graphs 📈
  • Shared problem-solving

Recap of Session #26:

Data from Shiny

library(shiny)
library(DT)

# Sample dataset
data <- data.frame(
  ID = 1:10,
  Name = c("John", "Paul", "George", "Ringo",
           "Mick", "Keith", "Charlie", "Ronnie", "Roger",
           "Pete"),
  Age = c(40, 42, 38, 41, 43, 45, 39, 40, 44, 42)
)

# Define UI
ui <- fluidPage(                                            
  titlePanel("Download CSV Example"),
  fluidRow(
    sidebarPanel(
      sliderInput('ageRange', 'Age Range', 
                  min = 30,
                  max = 45,
                  value = c(30, 40))
    ),
    mainPanel(
      DTOutput("table"),
      downloadButton("downloadData", "Download CSV")
    )
  )
)

# Define server logic
server <- function(input, output) {
  # Reactive expression to filter data based on age range
  filtered_data <- reactive({
    data[data$Age >= input$ageRange[1] & data$Age <= input$ageRange[2], ]
  })
  
  # Display the table
  output$table <- renderDT({
    filtered_data()
  })
  
  # Download handler
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep = "")
    },
    content = function(file) {
      write.csv(filtered_data(), file, row.names = FALSE)  
    }
  )
}

# Run the app
shinyApp(ui = ui, server = server)
1
Creating a small dataframe
2
Title of app (see next slide)
3
Max and minimum scale input values
4
“Starting” points for slider scale
5
Table to be displayed (from DT package)
6
Download button with “Download CSV” label
7
Filter applied creating new data object, filtered_data. $ageRange is defined above in ui object.
8
downloadHandler in combination with write.csv most instructive here

Data from Shiny

Today:

Reactive ’s from Shiny

  • Data
  • Figure
  • Report

  • Quarto 👀

Session Info (May 28, 2024) Rendering:

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] DT_0.27           shiny_1.7.4       fontawesome_0.5.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10       jquerylib_0.1.4   compiler_4.2.2    bslib_0.6.1      
 [5] later_1.3.1       tools_4.2.2       digest_0.6.31     memoise_2.0.1    
 [9] lubridate_1.9.3   jsonlite_1.8.8    evaluate_0.23     lifecycle_1.0.4  
[13] timechange_0.3.0  rlang_1.1.3       cli_3.6.0         rstudioapi_0.15.0
[17] crosstalk_1.2.0   yaml_2.3.8        xfun_0.42         emo_0.0.0.9000   
[21] fastmap_1.1.1     webshot2_0.1.1    stringr_1.5.1     knitr_1.45       
[25] sass_0.4.9        generics_0.1.3    vctrs_0.6.5       htmlwidgets_1.6.4
[29] webshot_0.5.5     websocket_1.4.1   chromote_0.2.0    glue_1.6.2       
[33] R6_2.5.1          processx_3.8.1    rmarkdown_2.26    callr_3.7.3      
[37] purrr_1.0.1       magrittr_2.0.3    ps_1.7.5          promises_1.2.0.1 
[41] htmltools_0.5.7   ellipsis_0.3.2    assertthat_0.2.1  mime_0.12        
[45] xtable_1.8-4      httpuv_1.6.9      stringi_1.7.12    cachem_1.0.8     
[49] crayon_1.5.2