0
votes

I'm trying to create an app creates a simple list (actives$name) of strings, and shows me back those strings. For some reason, it doesn't work. Here's the code:

library(shiny)

ui <- fluidPage(

  sidebarLayout(
    mainPanel(
      textInput(inputId = "name", label = "Name", value = "foo"),
      actionButton(inputId = "add", label = "Add")
    ),
    sidebarPanel(
      uiOutput("activeB")
    )
  ),

)

server <- function(input, output, session) {

  actives <- reactiveValues()

  observeEvent(input$add,{
    actives$name <- c(actives$name, input$name)
  })

  output$activeB <- renderUI({
    for (i in 1:length(actives$name)) {
      p("Name:")
      verbatimTextOutput(actives$name[i])
      print(actives$name[i])
    }
  })

}

shinyApp(ui = ui, server = server)

The print() I'm using in the for loop shows the correct result, so it "knows" what I'm trying to do. It just doesn't output it. What am I doing wrong?

1

1 Answers

1
votes

Interesting one. Took me a couple of goes to get it. You need to wrap the renderUI output in a tagList:

  output$activeB <- renderUI({
    tagList(HTML(paste(actives$name, collapse="<br>")))
  })

Two observations:

  • Your demo code would never have worked because your debug print loop is the last statement in renderUI, so renderUI never returns anything. :(
  • You know you can get the same effect without resorting to uiOutput and renderUI, right? I'm assuming you've simplified your actual use case to focus on the issue - in which case, thank you! :)

Full code:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    mainPanel(
      textInput(inputId = "name", label = "Name", value = "foo"),
      actionButton(inputId = "add", label = "Add")
    ),
    sidebarPanel(
      uiOutput("activeB")
    )
  )
)

server <- function(input, output, session) {
  actives <- reactiveValues(
    name=c()
  )
  observeEvent(input$add,{
    actives$name <- c(actives$name, input$name)
  })
  output$activeB <- renderUI({
    tagList(HTML(paste(actives$name, collapse="<br>")))
  })
}

shinyApp(ui = ui, server = server)