Error: Problem with `mutate()` input `time_format`. All arguments must be numeric or NA

I have a R script which i am using in R shiny but in R shiny it throws an error when the mutate function is executed as below.I don't understand why its causing an error in r shiny but works fine in R script and how to resolve it.

promax_2020_data_time <- mutate(promax_2020_data,
time_format = hms(promax_2020_data$Time) %>% as.duration())

Error: Problem with mutate() input time_format.
x All arguments must be numeric or NA
i Input time_format is hms(promax_2020_data$Time) %>% as.duration().

promax_2020_data_time is a dataframe.Time column is of char type.I have used the following libraries for the entire code:
library(dplyr)
library(tidyr)
library(hms)
library(lubridate)
library(tidyverse)
library(shiny)

Any update on the same would be highly appreciated.Thanks.

Hi ritm,

Could you prepare a reproducible example? With the info you have given is really difficult to narrow down what your problem is.

1 Like

I have given the code below for both R script as well as R shiny.When the code is executed in R script,it doesn't throw any error but when the same code is executed in R shiny,it throws the error as highlighted earlier.

R script:-

getwd()
setwd("C:/Users/Ritwik/Documents/R")

promax_Jan_Sep_2020_data<-read.csv("Promax_Jan_Sept_2020_Data.csv")

promax_Oct_Dec_2020_data<-read.csv("Promax_Oct_Dec_2020_Data.csv")

promax_2020_data<-rbind(promax_Jan_Sep_2020_data,promax_Oct_Dec_2020_data)

View(promax_2020_data)

class(promax_2020_data)

str(promax_2020_data)

unique(promax_2020_data$Region)


#install.packages("dplyr")
library(dplyr)

#install.packages("tidyr")
library(tidyr)

#install.packages("lubridate")
library(lubridate)

#install.packages("tidyverse")
library(tidyverse)

promax_2020_data <-subset(promax_2020_data,!(Region %in% c("#VALUE!","10.1","10.2","138","145","161")))

unique(promax_2020_data$Region)

promax_2020_data$Region<-gsub("-","",promax_2020_data$Region)

View(promax_2020_data)

promax_2020_data$Time<-format(strptime(promax_2020_data$Time, "%I:%M:%S %p"), "%H:%M:%S")

promax_2020_data$Date<-as.factor(promax_2020_data$Date)

promax_2020_data$Date<-strptime(promax_2020_data$Date,"%m/%d/%Y")

promax_2020_data$Date<-as.Date(promax_2020_data$Date,"%Y-%b-%d")

str(promax_2020_data)

promax_2020_data$month<-format(promax_2020_data$Date,"%b")

unique(promax_2020_data$month)

promax_2020_data$month = factor(promax_2020_data$month, levels = month.abb)


colnames(promax_2020_data$month)<-promax_2020_data$Month

promax_2020_data <- mutate(promax_2020_data, Quarter = case_when(
  month %in% c("Jan", "Feb", "Mar") ~ "Q1",
  month %in% c("Apr", "May", "Jun") ~ "Q2",
  month %in% c("Jul", "Aug", "Sep") ~ "Q3",
  TRUE ~ "Q4"
))


str(promax_2020_data)


unique(promax_2020_data$Quarter)



promax_2020_data_time <- mutate(promax_2020_data,
                                time_format = hms(promax_2020_data$Time) %>% as.duration())

View(promax_2020_data_time)

R shiny:-

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

getwd()
setwd("C:/Users/Ritwik/Documents/R")

promax_Jan_Sep_2020_data<-read.csv("Promax_Jan_Sept_2020_Data.csv")

promax_Oct_Dec_2020_data<-read.csv("Promax_Oct_Dec_2020_Data.csv")

promax_2020_data<-rbind(promax_Jan_Sep_2020_data,promax_Oct_Dec_2020_data)

View(promax_2020_data)

class(promax_2020_data)

str(promax_2020_data)

unique(promax_2020_data$Region)

library(dplyr)
library(tidyr)
library(hms)
library(lubridate)
library(tidyverse)
library(shiny)


promax_2020_data <-subset(promax_2020_data,!(Region %in% c("#VALUE!","10.1","10.2","138","145","161")))

unique(promax_2020_data$Region)

promax_2020_data$Region<-gsub("-","",promax_2020_data$Region)

View(promax_2020_data)

promax_2020_data$Time<-format(strptime(promax_2020_data$Time, "%I:%M:%S %p"), "%H:%M:%S")

promax_2020_data$Date<-as.factor(promax_2020_data$Date)

promax_2020_data$Date<-strptime(promax_2020_data$Date,"%m/%d/%Y")

promax_2020_data$Date<-as.Date(promax_2020_data$Date,"%Y-%b-%d")

str(promax_2020_data)

promax_2020_data$month<-format(promax_2020_data$Date,"%b")

unique(promax_2020_data$month)

promax_2020_data$month = factor(promax_2020_data$month, levels = month.abb)


colnames(promax_2020_data$month)<-promax_2020_data$Month

promax_2020_data <- mutate(promax_2020_data, Quarter = case_when(
    month %in% c("Jan", "Feb", "Mar") ~ "Q1",
    month %in% c("Apr", "May", "Jun") ~ "Q2",
    month %in% c("Jul", "Aug", "Sep") ~ "Q3",
    TRUE ~ "Q4"
))


unique(promax_2020_data$Quarter)


promax_2020_data_time <- mutate(promax_2020_data,
                                time_format = hms(promax_2020_data$Time) %>% as.duration())

View(promax_2020_data_time)