df[,1] <- as.factor(df[,1]) works in regular environment, but not in shiny environment.

The title practically states most of the issue.

I have a data.frame named df. In regular non-shiny non-reactive environment, the following code works -
df[,1] <- as.factor(df[,1])
However, in shiny environment (written below) I get this [ERROR: cannot xtfrm data frames]

library(shiny)
library(shinydashboard)
library(tidyverse)
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
  fileInput("upload", NULL, accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")),
  plotOutput("plot")
)
ui <- dashboardPage(header, sidebar, body)

server <- function(input, output, session){
  datable <- reactiveVal()
  observeEvent(input$upload,{
    
    datable(
      vroom::vroom(input$upload$datapath, delim=",")
    )
    
  }
  )
  plotable <- reactive({
    req(datable())
    df <- datable()
    df[,1] <- as.factor(df[,1])

    df2 <- gather(df, key = "group", value = "values", -names(df)[1])
    df2$proportions <- df2$values / ave(df2$values, df2$group, FUN = sum)
    ggbarplot(data=df2,
              x = "group",
              y = "proportions",
              fill = names(df2)[1],
              facet.by = names(df2)[1],
              color = "white"
    )+
      xlab("")+
      ylab("")+
      theme(legend.position="none")
  })
  output$plot <- renderPlot(plotable())
}
  

shinyApp(ui, server)

Is there a way to solve this? Thank you in advance.

this is an issue regarding the differences between data.frames and tibbles and is not shiny related.

Here is some background reference: Tibbles.

Here is a brief example script I made for you .

library(tidyverse)

 t1 <- tibble(a=1:2,
        l=letters[1:2])
 
 d1 <- as.data.frame(t1)
 

 d1[,1]
 t1[,1]
 
d1[,1] <- 3:4 #fine 
t1[,1] <- 3:4 #fine

d1[,1]
t1[,1]

d1[,1] <- d1[,1] #fine 
t1[,1] <- t1[,1] #fine

d1[,1] <- as.factor(d1[,1])
t1[,1] <- as.factor(t1[,1]) # your problem ; and 'breaks t1'
t1

(t1 <-  t1 <- tibble(a=3:4,
                     l=letters[1:2])) # remaking 

t1[[1]] <- as.factor(t1[[1]]) # achieves the required and works 

# another option is mutating a tibble in the dplyr way
t1 <- mutate(t1,
             across(1,as.factor))
t1

That makes so much sense. I was dealing with data.frame most of the time and forgot that tibble existed.
I simply used df <- as.data.frame(df) and that fixed the issue.

Thanks.