This post is an extension of a previous post where I accidentally oversimplified my sample code to replicate the issue. I am trying to repetitively create variables for use in shiny, depending on how many fields a user selects in an input field. The answer that was provided was more than suitable and I am looking to expand on it. The oversimplification of the last post was the use of a numericInput to determine the number of fields to generate, when I actually need to count how many entries are selected of a selectInput to determine how many fields to populate. I then need to reiteratively call upon those generated fields in the server to generate the outputs. My sample code for attempting to expand on this issue is below.
my_letters <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
my_names <- c("Mary", "Joe", "John", "Steve", "Bob", "Linda", "Emily", "Kevin", "Michael", "Tom")
my_number <- as.character(1:10)
my_df <- data.frame(my_letters, my_names, my_number)
letter_choices <- as.character(unique(my_df$my_letters))
ui <- fluidPage(
selectInput("num_selected", label = "Select Letters", choices = letter_choices, selected = "", multiple = TRUE, selectize = TRUE),
uiOutput("condPanels")
)
server<-function(input,output,session){
output$condPanels <- renderUI({
# if selected value = 0 dont create a condPanel,...
if(!nrow(input$num_selected)) return(NULL)
tagList(
lapply(1:nrow(input$num_selected), function(nr){
conditionalPanel(
condition = paste0("input.num_selected >= ", nr),
textOutput(paste0("Name", nr), "Name"),
textOutput(paste0("Number", nr), "Number")
)
})
)
})
output$Name1 <- renderText({ as.character(my_df$my_names[1]) })
output$Name2 <- renderText({ as.character(my_df$my_names[2]) })
output$Number1 <- renderText({ as.character(my_df$my_number[1]) })
output$Number2 <- renderText({ as.character(my_df$my_number[2]) })
}
shinyApp(ui=ui, server=server)
textOutput()
instead oftextInput()
? That would indeed be a bit different ;) So no conditional inputs as well? – Tonio Liebrand