edit: I realized only changing the mutate
to be mutate(y = lubridate::ymd_hms(timestamp))
resolves the issue in both cases. My new question would be why does this resolve the issue in dplyer mutate
?
I am having trouble generating a plot after parsing my datetime column, timestamp
, into POSIXct format. The plot I want to create is timestamp
vs. SOEC_FC_0200_PIDPI_VAL0
. The problem is occurring when I perform mutate
and when I define aes()
in ggplot
. If I mutate timestamp
and store in a new column i.e. y
and use aes(y, SOEC_FC_0200_PIDPI_VAL0)
, the plot will render as intended. I am confused why parsing into POSIXct format in the same timestamp
column does not generate a plot.
I don’t see why mutate
to the same column would cause issue, so my best guess is that aes()
is referring to a different timestamp
than the one I want.
library(shiny)
library(ggplot2)
library(dplyr)
library(DateTimeRangePicker)
ui <- fluidPage(
DateTimeRangePickerInput('date_range', from = '2024-06-11 14:00:00', to = '2024-06-11 14:00:05'),
actionButton('run', 'Run'),
plotOutput('plot')
)
server <- function(input,output){
data_input <- structure(list(timestamp = c("2024-06-11 14:00:00.0076393", "2024-06-11 14:00:01.0134811",
"2024-06-11 14:00:02.0112244", "2024-06-11 14:00:03.0090309",
"2024-06-11 14:00:04.0058750"), timestamp_utc = c("2024-06-11 18:00:00.0076393",
"2024-06-11 18:00:01.0134811", "2024-06-11 18:00:02.0112244",
"2024-06-11 18:00:03.0090309", "2024-06-11 18:00:04.0058750"),
sequence_number = 56500303:56500307, SOEC_EC_0510_PIDPI_VAL0 = c(151.346252441406,
151.346252441406, 151.323455810547, 151.351745605469, 151.355331420898
), SOEC_FC_0200_PIDPI_VAL0 = c(3.9853515625, 3.99609375,
4.017578125, 4.0068359375, 4.0068359375)), row.names = c(NA,
5L), class = "data.frame")
data_input_0 <- data_input |> mutate(timestamp = lubridate::ymd_hms(timestamp))
data_filtered <- eventReactive(input$run,
filter(data_input_0, as.POSIXct(timestamp) >= as.POSIXct(input$date_range[1]) & as.POSIXct(timestamp) <= as.POSIXct(input$date_range[2]))
)
output$plot <- renderPlot(
ggplot(data_filtered(), aes(x = timestamp, y = SOEC_FC_0200_PIDPI_VAL0))+geom_point()
)
}
shinyApp(ui = ui, server = server)
This problem is related to another issue I am having. When I incorporate fileInput()
in the user interface and change data_input
to take in the imported .csv file, the same issue occurs. Except now, even when I mutate to a new y
column, the plot will still not render even though the .csv file contains the same data as before.
library(shiny)
library(ggplot2)
library(dplyr)
library(DateTimeRangePicker)
ui <- fluidPage(
fileInput("csv_input", "Select file", accept = ".csv"),
DateTimeRangePickerInput('date_range', from = '2024-06-11 14:00:00', to = '2024-06-11 14:00:05'),
actionButton('run', 'Run'),
plotOutput('plot')
)
server <- function(input,output){
data_input <- reactive({
req(input$csv_input)
df <- read.csv(input$csv_input$datapath)
return(df)
})
data_input_0 <- reactive({data_input() |> mutate(y = lubridate::ymd_hms(timestamp))
})
data_filtered <- eventReactive(input$run,{
filter(data_input_0(), as.POSIXct(timestamp) >= as.POSIXct(input$date_range[1]) & as.POSIXct(timestamp) <= as.POSIXct(input$date_range[2]))
})
output$plot <- renderPlot(
ggplot(data_filtered(), aes(x = timestamp, y = SOEC_TE_0304_IOPI_VAL0))+geom_point()
)
}
shinyApp(ui = ui, server = server)