Mickey_NC Mickey_NC - 1 month ago 7
R Question

R shiny : if with checkboxGroupInput

I have this code in my Server but the value NB_ind does not display. It displays only the message if boxes are checked, and if I uncheck boxes, the message "Number of persons who did not answer this question"" does not appear at all.

Do you see why ? Thanks a lot.

This my Ui.R :

checkboxGroupInput("skin", label = "Skin color", choices = list
("01. White" = "1", "02. Black" = "2", "03. Yellow" = "3",
"04. Mixed" = "4")),

textOutput("ETH")


And my Server.R :

observeEvent(input$skin,{

if (is.null(input$skin)){NB_ind <- " Number of persons who did not
answer this question"}
else {NB_ind <- " Number of persons who have checked this/these
box(es)"}

# Process for counting number of persons who answered or not the question
#selectionETH <- reactive({filter(filter(BI14, PC %in% input$dynamic &
# ETH %in% paste(input$skin,collapse=""), AGER >=
# input$age[1] & AGER <= input$age[2]))
#})

output$ETH <- renderText({NB_ind})
})


Thank you very much for helping.

Answer

This is a possible solution:

server.R (ui.R stays the same)

library(shiny)


    shinyServer(function(input, output) {

            NB_indRv <- reactive({
                    if (is.null(input$skin)){NB_ind <- "  Number of persons who did not 
                                          answer this question"}
                    else {NB_ind <- "  Number of persons who have checked this/these 
                    box(es)"}    
                    NB_ind
            })
                    output$ETH <- renderText(NB_indRv())
    })