how to convert factor to numeric in shiny

.i am working on R shiny. i want to convert factor to numeric in R shiny. i have used following code for conversion

 numeric_con<-renderTable({
   merge_cli_mi$Gender<-factor(merge_cli_mi$Gender,levels = c("female","male"),labels = c(1,2))
 })
 output$num1<-renderTable({
   numeric_con()
 })

but didn't work. how can i convert "male" "female" into numeric form. In above code merge_cli_mi is data set which contains Gender named column. Output is stored innum1.

Hi, welcome!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one for a shiny app

It's not really a factor problem you are having, its a shiny problem.
Assuming you have a reasonable sort of data.frame in merge_cli_mi such as


merge_cli_mi <- tribble( ~Gender,~SomeOtherVariable,
                         "female",99,
                         "male",-88
)

merge_cli_mi$Gender<-factor(merge_cli_mi$Gender,levels = c("female","male"),labels = c(1,2))

merge_cli_mi

therefore you would have a dataframe with the Gender variable as a factor with labels 1 and 2.
Problem in your code as provided is you only pass the Gender vector rather than the whole merge_cli_mi object to numeric_con, and you have two layers of renderTabler...

Try this

 numeric_con<-reactive({
   merge_cli_mi$Gender<-factor(merge_cli_mi$Gender,levels = c("female","male"),labels = c(1,2))
   merge_cli_mi
 })
 output$num1<-renderTable({
   numeric_con()
 })

if i want to convert entire data set into numeric variable ?

result <- mutate_all(input_df,as.numeric)

of course this will only work (or give sensible info) when the numeric data is present in your df as numbers hiding in character strings.

still showing error object of type 'closure' is not subsettable.
how can i solve this ?

Theres something improper about the object you are passing that should be a dataframe.
Perhaps you are passing a reactive and need to add () after it.

Without a reprex, this is kind of a guessing game.

i have run following code

   #merging two data set
   merge_cli_mi<-reactive({
       cbind(sub_datami(),sub_datacli())    
       
   })
   
   output$merge_cli_mi1<-renderTable({
       #as.numeric(levels())[merge_cli_mi()]
       merge_cli_mi() })
   
   
 
   numeric_con<-reactive({
       
       merge_cli_mi()$Gender<-factor(merge_cli_mi$Gender,levels = c("Female","Male"),labels = c(1,2))
       
       merge_cli_mi()
       
   })
   
   output$num1<-renderTable({
       
       numeric_con()
       
   })```
and got error saying object of type 'closure' is not subsettable

I think this part is problematic.
On the right you are referencing merge_cli_mi without acknowledge its being a reactive object ()
and on the left, you are trying to modify the reactive , which is a bit naughty :stuck_out_tongue:

Here is a demo of the type of approach that both works and is easy to follow.
Hopefully it will be a good example to you of a shiny reprex, for if you need support on a shiny issue in the future.

library(shiny)

ui <- fluidPage(
  tableOutput("merge_cli_mi1"),
  tableOutput("num1")
)

server <- function(input, output) {

  # merging two data set
  merge_cli_mi <- reactive({
    cbind(head(iris), head(iris))
  })

  output$merge_cli_mi1 <- renderTable({
    # as.numeric(levels())[merge_cli_mi()]
    merge_cli_mi()
  })

  numeric_con <- reactive({
    local_cli <- merge_cli_mi()

    local_cli$Gender <- factor(local_cli$Species,
      levels = c("setosa", "versicolor", "virginica"),
      labels = c(1, 2, 3)
    )

    local_cli
  })

  output$num1 <- renderTable({
    numeric_con()
  })
}

# Run the application
shinyApp(ui = ui, server = server)

worked!!!!!!!!!!!!!!
thank YOU ....

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.