wattss
wattss

Reputation: 71

Sum values from previous week based on current week

I am working in shiny app to produce a training report.

My simplified data and code:

library(tidyverse)
library(shiny)
library(shinythemes)
library(dplyr)
library(readr)
library(data.table)
library(formattable)
library(rsconnect)



Data<- structure(list(WeekEnding = c("2020-03-08", "2020-03-15", "2020-03-22", 
                                     "2020-03-29", "2020-04-05", "2020-04-12", "2020-04-19", "2020-04-26"
), TotalTraining.hrs = c(14.119574637, 15.560762437, 14.160377084, 
                         16.968056203, 14.617250934, 10.865982397, 14.619121779, 13.135129677
)), row.names = c(NA, 8L), class = c("grouped_df", "tbl_df", 
                                     "tbl", "data.frame"))
Data$WeekEnding <- as.Date(Data$WeekEnding)

ui <- (fluidPage(
    titlePanel("Training Report"), 
    sidebarPanel(
        selectInput("dateInput", "Week Ending", choices = max(Data$WeekEnding))), 
        mainPanel(
            h3("Results"), 
            formattableOutput("results"), 
            h3("Test"), 
            formattableOutput("test"))))


    
    server <- (function(input, output){
        
        output$results <- renderFormattable({
            Table <- data.table(
                "Variable" = c("Total Training (hrs)"), 
                "Last Week" = c(sum(Data$TotalTraining.hrs[Data$WeekEnding == input$dateInput])),
                "Change" = c(
                    (sum(Data$TotalTraining.hrs[Data$WeekEnding == input$dateInput]) - 
                         sum(Data$TotalTraining.hrs[Data$WeekEnding == (input$dateInput)-7]))))
                       
            
            formattable(Table, align= c("l", "c", "c"), 
                        list(Change = formatter("span", 
                                                   style = x ~ style(font.weight = "bold", 
                                                                     color = ifelse(x > 0, "green", ifelse(x < 0, "red", "black"))), 
                                                   x ~ icontext(ifelse(x>0, "arrow-up", ifelse(x<0, "arrow-down", "")), x))))
            
            
        })
        
        
    output$test <- renderFormattable({
        Test<- data.table(
            "Variable" = c("Total Training (hrs)"), 
            "Last Week" = c(sum(Data$TotalTraining.hrs[Data$WeekEnding == max(Data$WeekEnding)])),
            "Change" = c(
                (sum(Data$TotalTraining.hrs[Data$WeekEnding == max(Data$WeekEnding)]) - 
                     sum(Data$TotalTraining.hrs[Data$WeekEnding == max(Data$WeekEnding)-7]))))
                     
        
        formattable(Test, align= c("l", "c", "c"), 
                    list(Change = formatter("span", 
                                            style = x ~ style(font.weight = "bold", 
                                                              color = ifelse(x > 0, "green", ifelse(x < 0, "red", "black"))), 
                                            x ~ icontext(ifelse(x>0, "arrow-up", ifelse(x<0, "arrow-down", "")), x))))
        
    })
})
    
    
    shinyApp(ui = ui, server = server)
    

The code for Table is the code I would like to use, where the table values are reactive to the input date. I would like the table output to show the value of TotalTraining.hrs for the current date (i.e. dateInput) and then the change from the previous week (i.e. (dateInput) - 7). I know the issue is this this part of the code Data$WeekEnding == (input$dateInput)-7, because when I use Data$WeekEnding == max(Data$WeekEnding)-7 as you will see in the Test output, it works fine, When I run the code, the output for Table produces the error:

Warning: Error in -: non-numeric argument to binary operator
  97: data.table
  95: func
  82: origRenderFunc
  81: output$results
   1: runApp

Can someone tell me where I am going wrong? Is it an issue with using a reactive date?

Upvotes: 0

Views: 64

Answers (0)

Related Questions