0

so basically I am trying to develop a user interface where a person could select yes or no if they have the disease in the checkbox. Then it will take that yes/no answer for each corresponding disease and create a data frame so that a date table can be rendered and the person can see their responses. I've been struggling with trying to take what is clicked in the checkbox and putting it into a data frame with the diseases that I created another date frame for in the coding. I've tried several things and right now it keeps saying that the object 'data1', 'smoking', and 'diabetes' can't be found when I try to create the data frame from what the user would select. It seems my if/else statement is not working. Below is my code

library(shiny)
library(DT)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("IVD"),

    
    sidebarLayout(
        sidebarPanel(
            checkboxGroupInput("Diabete", "Diabetes:",
                              choices =  c("Yes" = "yes0",
                                 "No" = "no0")),
            checkboxGroupInput("Smoke", "Smoking:",
                               c("Yes" = "yes1",
                                 "No" = "no1"))),

        
        mainPanel(
            fluidRow(actionButton("button", "Click for Risk Prediction")),
            dataTableOutput("summary_table")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

 
observeEvent(input$button, { 
output$summary_table<-renderDataTable({
    a<- eventReactive(input$Diabete, {   
        if (input$Diabete == "yes0") {
            a=1
        } else {
            a=0
        }})
    b<- eventReactive(input$Smoke, {   
        if (input$Diabete == "yes1") {
            b=1
        } else {
            b=0
        }})
    ivd<-c('Diabetes','Smoking')
    #data<- c(11,10,sugar,8,7,6,5,4,3,2,1)
    values <- reactiveValues()
    values$ivd <- data.frame()
    eventReactive(input$Diabete, {
        diabetes <- a
        smoking <- b
        
        da <- data.frame(diabetes, smoking)
        
        data1 <- rbind(values$ivd, da)
    })
    ivd_data<-data.frame(ivd,data1,stringsAsFactors=FALSE )
    print(ivd_data)   
})
})
}

# Run the application 
shinyApp(ui = ui, server = server)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

0

Try this

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("IVD"),
  
  
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("Diabete", "Diabetes:",
                         choices =  c("Yes" = "yes0",
                                      "No" = "no0")),
      checkboxGroupInput("Smoke", "Smoking:",
                         c("Yes" = "yes1",
                           "No" = "no1"))),
    
    mainPanel(
      fluidRow(actionButton("button", "Click for Risk Prediction")),
      DTOutput("summary_table")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  
  observeEvent(input$button, { 
    
    a<- eventReactive(input$Diabete, {   
      a = ifelse(input$Diabete == "yes0",1,0) 
      })
    
    b<- eventReactive(input$Smoke, {   
      b = ifelse(input$Smoke == "yes1",1,0) 
      })
    
    ivd<- data.frame(a='Diabetes',b='Smoking')
    
    data1 <- reactive({
      data <- rbind(ivd,data.frame(a=a(),b=b()))
    })
    
    output$summary_table <- renderDT(data1())
    
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Thanks so much @YBS ! Yes this works! Second Question, I see the ifelse statement is set for two options "1" and "0". How would you set it up if you had more than two options? Such as "1", "2", "4", "5". – Janae Bradley Oct 27 '20 at 18:44
  • There are many ways to do it. Easiest would be to do what you have in OP, that is, `if(){...} else if (){...} else if (){...}` and so on. If you can manage, nested `ifelse` can be done fairly easily. You can also try `case_when(),` `array`, `sqldf`, etc. – YBS Oct 27 '20 at 19:19