Shiny beginner here! I'm trying to create an app, where the user can input a dynamic number of 8-digit codes. First they input the number of codes they like, and using renderUI, that many slots are generated.
I would like to use ShinyFeedback to check the nchar for each inserted code, and show a warning if it is not 8 digits. This is what I have so far for:
code_check <- reactive({
lapply(1:input$numInputs, function(i) {
shinyFeedback::feedbackWarning(input$inputName,
nchar(input$inputName) != 8,
"Your code needs to be 8 digits!")
}
)
})
I am having trouble accessing the individual inputs by their id. The input$inputName is generated in the renderUI section:
observeEvent(input$numInputs, {
output$inputGroup = renderUI({
input_list <- lapply(1:input$numInputs, function(i) {
# for each dynamically generated input, give a different name
inputName <- paste("Code ", i, sep = " ")
# generate the input field, the inputID = label, initial value is 12345678
textInput(inputName, inputName, "12345678")
})
})
})
Running this code give the following error: Warning: Error in feedback: is.character(inputId) is not TRUE
Complete current code below:
library(shiny)
ui <- shinyUI(fluidPage(
shinyFeedback::useShinyFeedback(),
titlePanel("As many 8 digit codes as you like"),
sidebarLayout(
sidebarPanel(
numericInput("numInputs", "How many codes would you like to insert?", 4),
# place to hold dynamic inputs
uiOutput("inputGroup")
),
mainPanel(textOutput("inputValues"),
textOutput("final"),
textOutput("code_check")
)
)
))
server <- shinyServer(function(input, output) {
# observe changes in "numInputs", and create corresponding number of inputs
observeEvent(input$numInputs, {
output$inputGroup = renderUI({
input_list <- lapply(1:input$numInputs, function(i) {
# for each dynamically generated input, give a different name
inputName <- paste("Code ", i, sep = " ")
# generate the input field, the inputID = label, initial value is 12345678
textInput(inputName, inputName, "12345678")
})
})
})
code_check <- reactive({
lapply(1:input$numInputs, function(i) {
shinyFeedback::feedbackWarning(input$inputName,
nchar(input$inputName) != 8,
"Your code needs to be 8 digits!")
}
)
})
output$code_check <- renderPrint({code_check()})
test <- reactive({
paste(lapply(1:input$numInputs, function(i) {
inputName <- paste("Code ", i, sep = " ")
input[[inputName]]
}))
})
output$final <- renderPrint({test()})
observe({
assign(
x = "inserted_codes",
value = test(),
envir = .GlobalEnv
)
})
})
shinyApp(ui, server)
Would be grateful for any ideas :)