Change column type from factor to integer in r shiny

shiny

#1

I have a reactive df for which I want to convert all the columns to numeric from factor. I have added a reproducible code below.

How do can I do it for all the columns in a shiny app.As of now I am getting all colnames and trying to change class in the loop but that doesnt seem to be working.

library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(DT)

sidebar <- dashboardSidebar(
  sidebarMenu(id = "tab",
              menuItem("1", tabName = "1")
  )
)
body <-   ## Body content
  dashboardBody(box(width = 12,fluidRow(
    fluidRow(  column(
      width = 3,  textInput("text1", label = h5("Min"), value = "1")),
      DT::dataTableOutput("op"))

  )))

ui <-   dashboardPage(dashboardHeader(title = "Scorecard"),sidebar,body)

# Define the server code
server <- function(input, output,session) {
  df <- data.frame(month = c("mazda 3", "mazda cx5", "mazda 6","mazda miata","honda civic","honda accord"),
                   april = c(.1,.2,.3,.3,.4,.5),
                   may = c(".3",".4",".5",".2",".1",".5")) 

  cols_to_change <- reactive({colnames(df)})

    dj_class  <- reactive({ for(i in cols_to_change()){
      class(df[, i]) = "integer"

    }
      })


  output$op <-renderDataTable({
    print( sapply(dj_class(),class))
    print( sapply(df,class))
    
    df
  })
  }
   shinyApp(ui = ui, server = server)