简体   繁体   中英

R Shiny Create Dynamic Numeric Input Based off Checkboxes

The program I'm writing takes a spreadsheet of grades and then creates tables, plots, and summaries of the data it receives. However, I want multiple users to be able to use this. This means the weight of each type of grade (Homework, Tests, Quizzes, etc) will be different for each user.

What I'm trying to do is create a checkboxGroupInput that generates a list of the different types of grades (read from 'Type' column in the spreadsheet), then when each checkbox is checked a numericInput will pop up allowing the user to select a weight. Here is an example picture.

Example Pic (Sorry, I don't have enough reputation to post an image right now)

However, I don't know how to render the numericInput to a True/False value of each checkbox. I know how to render a table to show columns based on group input, like in this example , but I don't know how to initiate an unknown number of numericInputs since each one will be its own variable. Is this possible? I can't think of another way to achieve what I want.

Here is the code (snippet) of what I have so far.

ui <- fluidPage(
          dashboardBody(
      tabBox(
        id = 'tabset1',
        width = '100%',
        tabPanel('Enter weights', 
                  splitLayout(uiOutput('type'), uiOutput('weight'))),
        tabPanel('Grades Graph', plotOutput('individualGraph')),
        tabPanel('Grades Table', dataTableOutput('summaryDT')),
        tabPanel('Summary by Unit', plotOutput('summaryBarGraph'), br(), dataTableOutput('summaryUnitDT')),
        tabPanel('Class Averages', plotOutput('classAverageGraph'), br(), dataTableOutput('classAverageTable'))
      )
    )

server <- function(input, output, session) {

  output$type <- renderUI({
    df <- data.frame(grades())
    choices <- unique(pull(df, Type))
    checkboxGroupInput(inputId = 'typeVar',
                label = 'Select Type',
                choices = choices,
                selected = choices)
  })

  output$weight <- renderUI({
    numericInput(inputId = 'weightVar',
                 label = 'Select Weight %', value = 50,
                 width = 50)
  })
}

Any help/guidance would be appreciated! If I'm wrong in thinking this is the best way to achieve what I want please let me know. I'm open to more suggestions.

First of all, does the checkboxGroupInput() make sense here? This seems like the radio buttons UI element match what you're trying to do better (because radio buttons imply to the user that only one can be checked at a time). Second, instead of having many numericInputs created dynamically, could you try to use one numeric input and use updateNumericInput() to change it whenever the radio button changes? Then in your handling of the number change your conditional statement would just need to look at the number AND what radio button is checked.

I'm envisioning something like this:

observeEvent(input$RadioButton, {
    updateNumericInput(session, "NumericInputName", value=theVariable)
}

where theVariable is the value previously entered for that category (test, quiz, etc), or 0 if nothing has ever been entered for that category.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM