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.
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
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...
#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
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)