So, you solve one problem and run into the next.
I've now succesfully build a part of code that dynamically creates uiOutput, i.e. a number of sliders, buttons and/or textfields and the amount of them depends on a value that comes rolling out of my model in an ealier step.
However, I'm pretty clueless how to observe whether they are "clicked" / "changed" by the user. Lets say, the model gives out a nr 12 then the server tells my ui to make 12 buttons. I want to know when the user pushes ANY of the buttons and WHICH button it is
To give a clear example in words: if user clicks button 8, i want to have R tell me "User clicked button 8". The objective is to have not only dynamic buttons, but also dynamic reactions to the use of them.
One of the final aims I have is to collect a list of yes / no answers and entered names for each of the elements. So I'm looking for a universal way to hang conditions to observeevent for button " i" or text field "j" and so on
Here is a fully functional minimal example of how I create the dynamic UI.
SERVER:
shinyServer = function(input, output, session) {
################# start functionality HOME TAB #############################
values <- reactiveValues()
observeEvent(input$myNr, {
values$nrofelements <- input$myNr })
observeEvent(values$nrofelements, {
if (values$nrofelements > 0 & values$nrofelements < 25) {
output$sliders <- renderUI({
lapply(1:values$nrofelements, function(j) {
sliderInput(inputId = paste0("ind", j), label = paste("Individual", j),
min = 0, max = 20000, value = c(0, 500), step = 100)
})
})
output$buttons <- renderUI({
lapply(1:values$nrofelements, function(i) {
div(br(),bsButton(inputId = paste0("indr", i), label = paste("Yes", i), block = FALSE, style = "succes"), br(), br() )
})
})
}
})
observe({
if(values$nrofelements != ""){
for(nr in 1:values$nrofelements){
if(!is.null(input[[paste0("indr", nr)]])) print(paste0("Inputname 'indr", nr, "': ", "value is ", isolate(input[[paste0("indr", nr)]])))
}
}
})
}
and the UI.r
library(shiny)
library(shinydashboard)
library(shinybs)
ui <- dashboardPage(
dashboardHeader(title = "FLOW C.A.R.S."),
dashboardSidebar(
sidebarMenu(id = "tabs", menuItem("Home", tabName = "Home", icon = icon("book"))
)
),
dashboardBody(
tabItems(
### HOME ###_________
tabItem(tabName = "Home", class = 'rightAlign',
h5("Enter desired nr of elements here"),
textInput(inputId ="myNr", label = NULL , placeholder = NULL),
fluidRow(
column(3,
uiOutput("sliders")),
column(1,
uiOutput("buttons")
)
),
textOutput('clickedwhat')
)
)
)
)