0

I want to use cut.POSIXct funtion to group and sum data by time period in R Shiny, but it got an eorror “‘x’ must be numeric”. Do you know why, and how to solve this problem?

Here is the code:

library(shiny)
library(readxl)


ui <- fluidPage(titlePanel("Calculation of 1h Data"),
   sidebarLayout(
   sidebarPanel(
   fileInput("file", label = h3("File input")) 
   ),
   mainPanel(tableOutput("table")) ) 
)

server <- function(input, output) {

   options(shiny.maxRequestSize=30*1024^2) 

   selectedData <- reactive({   
   inFile <- input$file
   if (is.null(inFile))
   return(NULL)
   data <- read_xlsx(inFile$datapath, col_names = TRUE,na = "")
   data$date<-format(as.POSIXct(data$date,"%Y/%m/%d %H:%M:%S"),"%Y/%m/%d 
   %H:%M:%S")    
   datat<-aggregate(. ~ cut(date, "1 hour"), data, mean)   
   })

   output$table <- renderTable({ 
   selectedData()})

}

shinyApp(ui, server)
TichPi
  • 146
  • 5
  • 2
    Without some code (and data), it's hard to tell. My first guess is that the date values you are passing to `cut` are characters, and not of the `POSIXct` class. – Benjamin Jan 30 '18 at 14:40
  • Thank you very much. I have just add the code. – TichPi Jan 30 '18 at 16:08
  • 1
    Hi Frank, it is still very difficult to help without the contents of the file. Try to remove the `read.csv` function and add data with the output of `dput()` for example. For more info on how to add a good minimal reproducible example, see for example [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/) or [here](https://stackoverflow.com/a/48343110/8037249). – Florian Jan 30 '18 at 16:38

0 Answers0