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)
答案 0 :(得分:1)
你可以在没有像这样的条件面板的情况下做到这一点。如果您还想要动态的话,可以通过output[["name"]]
定义输出..
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(
sidebarPanel(
selectInput("num_selected", label = "Select Letters", choices = letter_choices, selected = "", multiple = TRUE, selectize = TRUE)
),
mainPanel(
uiOutput("txt")
)
)
server<-function(input,output,session){
output$txt <- renderUI({
amt <- length(input$num_selected)
if(!amt) return(NULL)
tagList(lapply(1:amt, function(nr){
tagList(
textOutput(paste0("Name", nr)),
textOutput(paste0("Number", nr))
)
})
)
})
# if selected value = 0 dont create a condPanel,...
observe({
amt <- length(input$num_selected)
if(!amt) return(NULL)
lapply(1:amt, function(nr){
local({
idx <- which(input$num_selected[nr] == my_df$my_letters)
output[[paste0("Name", nr)]] <- renderText({ as.character(my_df$my_names[idx]) })
output[[paste0("Number", nr)]] <- renderText({ as.character(my_df$my_number[idx]) })
})
})
})
}
shinyApp(ui=ui, server=server)