lundi 26 octobre 2020

How to create a reactive data frame and table from user input checkboxes in shiny

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)



Aucun commentaire:

Enregistrer un commentaire