I am trying to create a shiny user interface with a dynamic reactive UI.
Essentially a user has to input the number of variants for an experiment (1-10).
For each variant the user is required to enter the variant name in a textbox and also a proportion 1 - 100 to allocate to each variant. The sum of proportions across all variants should add to 100.
e.g. User selects 5 variants. Then for each variant 1-5 the user should get dropdowns to select proportions.
- Variant 1 proportion (1-100), user selects 40 say
- Variant 2 proportion (1-60), user selects 10 say
- Variant 3 proportion (1-50), user selects 35 say
- Variant 4 proportion (1-15), user selects 10 say
- Variant 5 proportion (5), user has no say in allocating this - the proportion is backed out as 100 - (v1 + v2 + v3 + v4) = 5
I have borrowed code from here to start this: Producing dynamic/multiple input boxes to collect data depending on user selection in Shiny R
ui.R
library(shiny)
shinyUI(pageWithSidebar (
headerPanel( "Portfolio Returns"),
sidebarPanel(
numericInput("assets", label = "Enter Number of variants in Experiment", value="1"),
uiOutput("variants")
),
mainPanel()
))
Server.R
library(shiny)
shinyServer( function(input, output, session) {
output$variants <- renderUI({
numAssets <- as.integer(input$assets)
lapply(1:numAssets, function(i) {
list(tags$p(tags$u(h4(paste0("Variant ", i, ":")))),
textInput(paste0("variant", i), label = "Variant Name", value = paste0("Variant ", i, " name..."))
, numericInput(paste0("weight", i)
, label = "Proportion allocated (0 - 100)", value=0))
})
})
})
Could anyone please assist with the above?