User247365 User247365 -4 years ago 71
R Question

for loop shiny variable assignment 2

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)

Answer Source

You could do it without the conditional panel like this. The output can you define via output[["name"]] in case you also want that dynamic,..

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download