Home > Software engineering >  The fileInput() cause error in process with mutate() in a shiny app
The fileInput() cause error in process with mutate() in a shiny app

Time:09-15

I have a dataset in a csv file.I also display it here. When I run it without uploading it via fileInput() it is processed normally. If I load it via fileInput() I get: Warning: Error in mutate: object 'prοcess' not found

library(shiny)
library(DT)
library(tidyverse)
library(lubridate)
pr5915<-structure(list(case_id = c("WC4120721", "WC4120667", "WC4120689"
), lifecycle = c(110, 110, 110), action = c("WC4120721-CN354877", 
                                            "WC4120667-CN354878", "WC4120689-CN356752"), activity = c("Forged Wire, Medium (Sport)", 
                                                                                                      "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)"), 
resource = c("3419", "3216", "3409"), timestamp = structure(c(1606964400, 
                                                              1607115480, 1607435760), tzone = "", class = c("POSIXct", 
                                                                                                             "POSIXt"))), row.names = c(NA, -3L), class = c("tbl_df", 
                                                                                                                                                            "tbl", "data.frame"))
ui <- fluidPage(
  
  fileInput("file1", "Upload eventlog in csv format",
            accept = c(
              "text/csv",
              "text/comma-separated-values,text/plain",
              ".csv")),
  radioButtons("separator","Separator: ",choices = c(";",","), selected=";",inline=TRUE),
  dataTableOutput("table"),
  dataTableOutput("table2")
  
)

server <- function(input, output, session) {
  
  dataset<-reactive({
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
    process<-read.csv(inFile$datapath, header = T,sep = input$separator)
    
    process$timestamp <- as.POSIXct(process$timestamp, format="%d.%m.%y %H:%M")
    
    process<-prοcess %>% 
      mutate(
        timestamp = ymd_hms(
          as_datetime(
            as.character(timestamp), format = "%Y-%m-%d %H:%M:%S")
        ),
        Date = paste0(
          month(timestamp, label = T, abbr = F), 
          "-", 
          substr(year(timestamp), 3 , 4)
        )
      )
  })
  output$table<-renderDataTable({
    dataset()
  })
  output$table2<-renderDataTable({
    pr5915$timestamp <- as.POSIXct(pr5915$timestamp, format="%d.%m.%y %H:%M")
    
    pr5915<-pr5915 %>% 
      mutate(
        timestamp = ymd_hms(
          as_datetime(
            as.character(timestamp), format = "%Y-%m-%d %H:%M:%S")
        ),
        Date = paste0(
          month(timestamp, label = T, abbr = F), 
          "-", 
          substr(year(timestamp), 3 , 4)
        )
      )
  })
}
shinyApp(ui = ui, server = server)

CodePudding user response:

This works fine, I used your pr5915 data with %d.%m.%y %H:%M format to create my test .csv file and uploaded it using the dashboard below and the table showed up fine. I noticed I had to use stringsAsFactors = F here, although that might be my older version. Anyhow I cleaned up some code and this works.

library(shiny)
library(DT)
library(tidyverse)
library(lubridate)

ui <- fluidPage(
  fileInput("file1", "Upload eventlog in csv format", accept = c(
    "text/csv",
    "text/comma-separated-values,text/plain",
    ".csv")
  ),
  radioButtons("separator", "Separator: ", choices = c(";",","), selected = ";", inline = TRUE),
  dataTableOutput("table")
)

server <- function(input, output, session) {

  dataset <- reactive({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
    read.csv(inFile$datapath, header = T, sep = input$separator, stringsAsFactors = F) %>%
      mutate(
        timestamp = mdy_hm(timestamp),
        Date = paste(lubridate::month(timestamp, label = T, abbr = F), year(timestamp), sep = "-"),
        timestamp = as.character(timestamp) # not sure if DT can format it better with options
    )
  })

  output$table <- renderDataTable({
    dataset()
  })

}

shinyApp(ui = ui, server = server)

enter image description here

  • Related