I start with data that looks like this:
Date Time1 Time2
01/02/2018 01/02/2018 11:12 01/02/2018 13:14
01/03/2018 01/03/2018 09:09 01/03/2018 15:05
and I want output that looks like this:
Date Time1 Time2 Hour1 Hour2
01/02/2018 01/02/2018 11:12 01/02/2018 13:14 11 13
01/03/2018 01/03/2018 09:09 01/03/2018 15:05 9 15
Assume the times have been successfully coerced into class POSIXct.
In regular R script I use this code to produce my output:
library(lubridate)
a <- ymd_hms(myDF$Time1)
var4 = hour(a)
myDF = cbind(myDF, var4, stringsAsFactors=FALSE)
names(myDF)[4]<-"Hour1"
library(lubridate)
a <- ymd_hms(myDF$Time2)
var5 = hour(a)
myDF = cbind(myDF, var5, stringsAsFactors=FALSE)
names(myDF)[5]<-"Hour2"
But what works in regular R script seems to fail when I try to run the exact same code in a shiny app.
When I try to run this code:
shinyServer(function(input, output))({
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
myDF = read.csv(inFile$datapath, sep=",")
timesData = myDF[,c(2:3)]
timesData$Time1 = as.POSIXct(timesData$Time1, format="%m/%d/%Y %H:%M",
tz="GMT")
timesData$Time2 = as.POSIXct(timesData$Time2, format="%m/%d/%Y %H:%M",
tz="GMT")
library(lubridate)
a <- ymd_hms(timesData$Time1)
var4 = hour(a)
myDF = cbind(myDF, var4, stringsAsFactors=FALSE)
names(myDF)[4]<-"Hour1"
library(lubridate)
a <- ymd_hms(timesData$Time2)
var5 = hour(a)
myDF = cbind(myDF, var5, stringsAsFactors=FALSE)
names(myDF)[5]<-"Hour2"
})
})
shinyui(
mainPanel(
tableOutupt("contents")
)
))
I get this as my output:
Date Time1 Time2 Hour1 Hour2
01/02/2018 01/02/2018 11:12 01/02/2018 13:14 18 18
01/03/2018 01/03/2018 09:09 01/03/2018 15:05 18 18
It fills in every row of both hour columns with "18" and when I tried using "a <- ymd_hm(timesData$Time2)" it filled every row of both hour columns with "NA" instead. What do I have to do to make it fill in the rows of my hour columns with the actual hours?