I am creating a shiny app such that when I click on add or remove button, multiple reactive elements are affected. I have simplified significantly what I am trying to do below. Basically, we get selectInput() and textInput() boxes side-by-side, such that the textInput() box is populated with the user-chosen result of selectInput() box. I then have an add button and remove button, such that by clicking the add button, on the next line down, we get new selectInput() and textInput() boxes side-by-side. As above, the new row's textInput() box displays the user-chosen result of the new row's selectInput() box.
The issue I am having is being able to reference the new value of the new seletInput() box. Using a get() reference does not work, and I need an iterative way to be able to reference these values as new boxes are added and removed. How can I successfully call references to the result of successive selectInput() boxes?
suppressWarnings(library(shiny))
suppressWarnings(library(shinyFiles))
ui <- function(request) {
fluidPage(
fluidRow(
column(2,
uiOutput("ui1")
),
column(2,
uiOutput("ui2")
),
column(1,
actionButton(inputId = 'insertParamBtn', label = "Add Param")
),
column(1,
actionButton(inputId = 'removeParamBtn', label = "Remove Param")
)
),
tags$div(id = 'placeholder'),
hr(),
fluidRow(column(12, verbatimTextOutput("view", placeholder = T)))
)
}
server <- function(input, output, session) {
params <- reactiveValues(btn = 0)
output$ui1 <- renderUI({
selectInput("UI1", "First UI",
choices = thisList, selected = 1)
})
output$ui2 <- renderUI({
textInput("UI2", "Second UI", value = input$UI1, width = '150px')
})
observeEvent(input$insertParamBtn, {
params$btn <- params$btn + 1
insertUI(
selector = '#placeholder',
## wrap element in a div with id for ease of removal
ui = tags$div(
id = paste0('param', params$btn + 1),
tags$p(fluidRow(
column(2,
selectInput(paste0("UI1", params$btn + 1),
paste0("First UI ", params$btn + 1),
choices = thisList, selected = 1)
),
column(2,
textInput(paste0("UI2", params$btn + 1), #*#
paste0("Second UI ", params$btn + 1), #*#
value = get(paste0("input$UI1", params$btn + 1)), #*#
width = '150px') #*#
)
)
)
)
)
output$view <- renderPrint({ get(paste0("UI1", params$btn + 1)) })
})
observeEvent(input$removeParamBtn, {
removeUI(
## pass in appropriate div id
selector = paste0('#param', params$btn + 1)
)
params$btn <- params$btn - 1
})
}
shinyApp(ui = ui, server = server)