I am writing a Shiny application to visualize insurance plans in my company. Here is what I would like to do:
- I will have
selectInput or sliderInput , where the user will select the number of persons according to their medical plan. - The corresponding number of double-sided sliders is displayed (one for each item).
- They can then enter their estimates for the best / worst medical expenses for each participant according to their plan.
- I have a code that will take these estimates and create side-by-side graphs illustrating the projected cost of the three proposals of the plan so that they can decide which one is the least expensive based on their estimates.
Here is my current ui.R file with hard-coded inputs simulating a family of four:
shinyUI(pageWithSidebar( headerPanel("Side by side comparison"), sidebarPanel( selectInput(inputId = "class", label = "Choose plan type:", list("Employee only" = "emp", "Employee and spouse" = "emp_spouse", "Employee and child" = "emp_child", "Employee and family" = "emp_fam")), sliderInput(inputId = "ind1", label = "Individual 1", min = 0, max = 20000, value = c(0, 2500), step = 250), sliderInput(inputId = "ind2", label = "Individual 2", min = 0, max = 20000, value = c(0, 2500), step = 250), sliderInput(inputId = "ind3", label = "Individual 3", min = 0, max = 20000, value = c(0, 2500), step = 250), sliderInput(inputId = "ind4", label = "Individual 4", min = 0, max = 20000, value = c(0, 2500), step = 250) ), mainPanel( tabsetPanel( tabPanel("Side by Side", plotOutput(outputId = "main_plot", width = "100%")), tabPanel("Summary", tableOutput(outputId = "summary")) ) )))
Here's what it looks like (the transparent end sections are the result of two HSA contributions. I thought it was a good way to show both premium and medical expenses by showing the impact of HSA's contribution. So you simply compare the length of the solid colors )

I saw examples like this where the user interface itself is fixed (in this case, one checkboxGroupInput exists, but its contents are configured based on a choice from another user interface input), but I have not seen examples of fitting the number (or, say, type) of input elements, generated as a result of other user interface input content.
Any suggestions on this (is this possible)?
My last resort would be to create, say, 15 input sliders and initialize them to zero. My code will work fine, but I would like to clear the interface without creating as many sliders just for a random user who has a very large family.
Update based on Kevin Wushai's answer
I tried going through the server.R route and got the following:
shinyServer(function(input, output) { output$sliders <- renderUI({ members <- as.integer(input$members)
Immediately after that, I try to extract the values ββfrom input for each individual flow:
expenses <- reactive({ members <- as.numeric(input$members) mins <- sapply(1:members, function(i) { as.numeric(input[[paste0("ind", i)]])[1] }) maxs <- sapply(1:members, function(i) { as.numeric(input[[paste0("ind", i)]])[2] }) expenses <- as.data.frame(cbind(mins, maxs)) })
Finally, I have two functions that create objects to store a data frame for building on the basis of low and high medical costs. They are called best_case and worst_case , and both require the expenses object to work, so I call it my first line, as I learned from this question
best_case <- reactive({ expenses <- expenses() ... )}
I got some errors, so I used browser() to go through the expenses bit and noticed that functions such as input$ind1 do not exist inside the expenses function.
I also played with various print() instructions to see what happens. The brightest when I execute print(names(input)) as the very first line in the function:
[1] "class" "max_pred" "members" [1] "class" "ind1" "ind2" "max_pred" "members"
I get two outputs, which, in my opinion, are determined by the definition of expenses and its subsequent call. Strange ... I don't get the third when worst_case uses the same expenses <- expense() .
If I do something like print(expenses) inside my expenses function, I also get duplicates:
Any advice on why my input elements for ind1 and ind2 will not be displayed until expenses is called a second time and thus the data frame is correctly created?