0

I'm unable to successfully change the class of a column that was uploaded via fileInput(). I've tried specifying the column type to a date in the "read_csv" argument, although that didn't work. I then tried using mutate and as.Date() after importing, but that didn't work either.

Note - the data that is being imported has the date in the format of "mm/dd/yyyy". The name of the column is "day." ultimately, I want to extract the month and year from the date using lubridate.

# setup sidebar
# -------------
sidebar <- dashboardSidebar(disable = FALSE,
                            sidebarMenu(
                              menuItem("Testing Tab", 
                                  tabName="testing_tab", 
                                  icon=icon("line-chart")),
                              menuItem("Excel Input", 
                                  tabName="excel_input", 
                                  icon=icon('filter'),
                              menuSubItem(icon = NULL,  
fileInput("upload", "Upload"))
                          )
                        )
)

# setup dashboard body
# --------------------
body <- dashboardBody(
  tabItems(
    tabItem(tabName = "testing_tab",
        column(width = 12,
               tableOutput('testing_01')
            )
    )
  )
)

# setup dashboard header & tie three components together
# ------------------------------------------------------
header <- dashboardHeader(title = "Testing")
myUI <- dashboardPage(header, sidebar, body)


# setup server
# ------------
mySERVER <- function(input, output, session) {

  output$testing_01 <- renderTable({
    req(input$upload)
    test_01 <- read.csv(input$upload$datapath) %>% 
      clean_names() %>% 
      mutate(day = as.Date(day, format = '%m/%d/%y'))
  })
}

# deploy app
# ----------
shinyApp(
  ui = myUI,
  server = mySERVER
)
  • 2
    Welcome to SO, Rich! In general setting the column names via `read_csv` or converting via `mutate` should work. But without [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a minimal working code and a snippet of your data, one can only guess what might be the issue. I would also be helpful if you could clarify what's wrong with your conversion? Quite often the issue with dates is that they are not in the standard format "%Y-%m-%d" expected by `as.Date()` in which case setting the `format=` may fix the issue. – stefan Feb 15 '23 at 19:02
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 15 '23 at 19:38
  • Hey! I updated the code so that it is reproducible. I'm new here and not so sure how to upload the csv that I'm using. Any csv with a date column in 'mm/dd/yyyy' format should reproduce my error. – Rich Viebrock Feb 15 '23 at 20:16

0 Answers0