RichardRogers

ShinyGUI.R

May 28th, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.14 KB | Source Code | 0 0
  1. library(base64enc)
  2. library(bslib)
  3. library(DBI)
  4. library(plotly)
  5. library(shiny)
  6. library(shinythemes)
  7. library(stringr)
  8.  
  9. AddAllOption <- function(DF)
  10. {
  11.   # Position to insert the new row (after the second row)
  12.   InsertPos <- 1
  13.  
  14.   DataFramePart1 <- DF[1:InsertPos - 1,,drop = FALSE]
  15.   DataFramePart2 <- DF[(InsertPos):nrow(DF),,drop = FALSE]
  16.  
  17.   NewRow <- c('All')
  18.  
  19.   # Combining the dataframe parts with the new row
  20.   NewDataframe <- rbind(DataFramePart1, NewRow, DataFramePart2)
  21.  
  22.   return(NewDataframe)
  23. }
  24.  
  25. # On Desktop at home:
  26. # source("F:/Data/Dropbox/#1 - CC/R/RRUtils.R")
  27.  
  28. # On Laptop:
  29. source("C:/Users/Richard/Dropbox/#1 - CC/R/RRUtils.R")
  30.  
  31. # "F:\Data\Dropbox\#1 - CyclingCanada\R\RRUtils.R"
  32.  
  33. WorkingDirectory <- "C:/Users/Richard/Dropbox/#1 - CC/R/CCDashboard"
  34.  
  35. # on Desktop at home:
  36. if (GetPCName() != "RRLAPTOP")
  37.   WorkingDirectory <- "F:/Data/Dropbox/#1 - CC/R/CCDashboard"
  38.  
  39. setwd(WorkingDirectory)
  40.  
  41. Config <- config::get()
  42.  
  43. DBDriver    <- Config$Driver
  44. DBName      <- Config$DBName
  45. DBHost      <- Config$DBHost
  46. DBPort      <- Config$DBPort
  47. DBUser      <- Config$DBUser
  48. DBPassword  <- rawToChar(base64decode(Config$DBPassword))
  49.  
  50. Conn <- dbConnect(odbc::odbc(),
  51.                   Driver = DBDriver, Server = DBHost, Port = DBPort,
  52.                   Database = DBName, UID = DBUser, PWD = DBPassword)
  53.  
  54. RiderYearsSQL <- Config$SQLRiderYears
  55. RiderYears <- dbGetQuery(Conn, RiderYearsSQL)
  56. RiderYears <- AddAllOption(RiderYears)
  57.  
  58. # ChoicesList <- c("Andrew", "Bob", "Chris")
  59. # ChoicesList <- as.character(seq(1, 10, by = 1))
  60. # ChoicesList <- RiderYears$cyclingyear
  61. # ChoicesList <- as.character(RiderYears$cyclingyear)
  62.  
  63. # TestList <- list(as.character(seq(2024, 2000, by = -1)))
  64. # names(TestList) <- as.character(TestList)
  65.  
  66. # names(TestList) <- as.character(TestList)
  67. # TestList <- RiderYears$cyclingyear
  68. # append(TestList, "All", after = 1)
  69. # names(TestList) <- as.character(TestList)
  70.  
  71. Frequencies <- c("Year", "Departure Hour", "Day of Week", "Indoor/Outdoor")
  72.  
  73. BestOf <- c(
  74.     "Highest Average Cadence"
  75.   , "Highest Average HR"
  76.   , "Highest Average Power"
  77.   , "Highest Average Speed"
  78.   , "Distance (KM)"
  79.   , "Duration (Time)"
  80. )
  81.  
  82. Quarters <-
  83.   data.frame("Quarters" = c("Q1 (Jan - Mar)", "Q2 (Apr - Jun)", "Q3 (Jul - Sep)", "Q4 (Oct - Dec)"))
  84. Quarters <- AddAllOption(Quarters)
  85.  
  86. Months <-
  87.   data.frame("Months" = month.name)
  88. Months <- AddAllOption(Months)
  89.  
  90. SQLRiderTotals <- Config$SQLRiderTotalsByYear
  91. RiderTotals <- dbGetQuery(Conn, SQLRiderTotals)
  92.  
  93. # TotalSeconds for 2014 is too high to be correct, not sure why
  94. # RiderTotals[]
  95.  
  96. RiderTotals[RiderTotals$rideryear == 2014,]$totalseconds <-
  97.   RiderTotals[RiderTotals$rideryear == 2014,]$ridecount *
  98.   RiderTotals[RiderTotals$rideryear == 2015,]$totalseconds /
  99.   RiderTotals[RiderTotals$rideryear == 2015,]$ridecount
  100.  
  101. MaxDist <- floor(max(RiderTotals$totalkilometers) / 1000) * 1000
  102.  
  103. # source("OldShinyUIFunction.R")
  104. source("UpdatedShinyUIFunction.R")
  105.  
  106. server <- function(input, output) {
  107.  
  108.   # UIFreq    <- reactive(input$UIFreq)
  109.   # UIYear    <- reactive(input$UIYear)
  110.   # UIQuarter <- reactive(input$UIQuarter)
  111.   # UIMonth   <- reactive(input$UIMonth)
  112.   # req(UIFreq, UIYear, UIQuarter, UIMonth)
  113.  
  114.   # renderPlotly(
  115.  
  116.   # Freq <- UIFreq() # ("Year", "Departure Hour", "Day of Week", "Indoor/Outdoor")
  117.   # FreqIndex <- match(Freq, Frequencies)
  118.   # XAxisLabel <- Frequencies[FreqIndex]
  119.  
  120.   XAxisLabel <- "RRTest"
  121.  
  122.   Plot1 <-
  123.     renderPlot({
  124.       ggplot(RiderTotals, aes(x = rideryear, y = totalkilometers)) +
  125.       scale_x_continuous (
  126.           name = XAxisLabel
  127.         , labels = as.character(RiderTotals$rideryear)
  128.         , breaks = RiderTotals$rideryear
  129.       ) +
  130.       scale_y_continuous (
  131.         "Kilometers",
  132.         breaks = seq(0, MaxDist, by = 250)
  133.       ) +
  134.       geom_col(fill = 'Green')
  135.     })
  136.  
  137.   # sink("C:/Users/Richard/Dropbox/#1 - CC/R/May28_2024.txt")
  138.   # print(summary(Plot1))
  139.   # sink()  
  140.  
  141.   output$Plot1 <- Plot1
  142. }
  143.  
  144. # rsconnect::deployApp(account = 'richardrogers', appDir = "C:/Users/Richard/Dropbox/#1 - CC/R/CCDashboard", appFiles = c("ShinyGUI.R", "config.yml"))
  145.  
  146. shinyApp(ui = ui, server = server)
Add Comment
Please, Sign In to add comment