0

Here is my code - creating a dashboard that will filter by date. One tab will show our wellness survey data, the other will show post-practice loading data. I am pulling in the first 3 columns from "post.csv" which are Date, Name, Daily. Then I am looking to create and add the next 3 columns with the math.

Where I am first stuck is that I need my Daily_Load to aggregate data for a specific athlete on the given Date. Then I need to create a rolling 7-day sum for each athlete using the Daily load data from the last 7 days (including Date selected). A 28-Day Rolling Sum/4 and 7-Day/28-Rolling is the last piece.

Thanks again for all of the help!

library(shiny)
library(dplyr)
library(lubridate)
library(ggplot2)
library(DT)
library(zoo)
library(tidyr)
library(tidyverse)
library(data.table)
library(RcppRoll)

AM_Wellness <- read.csv("amwell.csv", stringsAsFactors = FALSE)
Post_Practice <- read.csv("post.csv", stringsAsFactors = FALSE)
Post_Data <- Post_Practice[, 1:3]
Daily_Load <- aggregate(Daily~ ., Post_Data, sum)
Acute_Load <- rollsum(Post_Data$Daily, 7, fill = NA, align = "right")
Chronic_Load <- rollsum(Post_Data$Daily, 28, fill = NA, align = "right")/4
Post_Data['Day Load'] <- aggregate(Daily~ ., Post_Data, sum)
Post_Data['7-Day Sum'] <- Acute_Load
Post_Data['28-Day Rolling'] <- Chronic_Load
Post_Data['Ratio'] <- Acute_Load/Chronic_Load

ui <- fluidPage(

  titlePanel("Dashboard"),

  sidebarLayout(
    sidebarPanel(
      dateInput('date',
            label = "Date",
            value = Sys.Date()
  ),
  selectInput("athleteInput", "Athlete", 
              choices = c("All"))

),

mainPanel(tabsetPanel(type = "tabs",
                      tabPanel("AM Wellness", tableOutput("amwell")),
                      tabPanel("Post Practice", tableOutput("post"))

)
)

) )

server <- function(input, output) {
  output$amwell <- renderTable({
datefilter <- subset(AM_Wellness, AM_Wellness$Date == input$date)
  }, hover = TRUE, bordered = TRUE, spacing = "xs", align = "c")

  output$post <- renderTable({
datefilter <- subset(Post_Data, Post_Data$Date == input$date)
  }, hover = TRUE, bordered = TRUE, spacing = "xs", align = "c")

}

shinyApp(ui = ui, server = server)
  • Just do `aggregate('Daily Load' ~ ., df1, sum)` Daily Load should be within backquotes – akrun Feb 06 '19 at 16:56
  • This works well. However, I am having my data displayed in Shiny using tableOutput and renderTable. I am also using datefilter to specify the date to view. I am getting this error: Warning: Error in [<-.data.frame: replacement element 1 has 1551 rows, need 11335. Suggestions? – Tyler Friedrich Feb 06 '19 at 19:28
  • Can you post as a new question as it is not clear about your shiny code – akrun Feb 06 '19 at 21:07
  • Edited above. I am new to R and Stack Overflow so thank you for the patience. – Tyler Friedrich Feb 06 '19 at 21:21
  • I reopened the question as it is a bit different problem now – akrun Feb 06 '19 at 21:27
  • I understand the issue. You are assigning the output from `aggregate` I.e. summarised output to a single column in the original data. i.e. `Post_Data['Day Load'] <- aggregate(Daily~ ., Post_Data, sum)`. Here `Post_Data` was created with the first 3 columns of "Post_Practice". If you need to create a column in the original data, use `ave` from `base R` or do this in `dplyr` i.e. `Post_Data %>% group_by(colnames_that_you_need_add) %>% mutate(Day_Load = sum(Daily))` – akrun Feb 06 '19 at 21:36
  • So do I need to create the subset `Post_Data[, 1:3]` from my original "post.csv"? Or is my new subset `Post_Data %>% group_by (columns) %>% mutate(Day_Load = sum(Daily))`? – Tyler Friedrich Feb 06 '19 at 22:57
  • You need to create all the other columns as well i.e. Acute_Load, etc. – akrun Feb 07 '19 at 11:20

0 Answers0