Search code examples
htmlrshinyserver-side

Populate html text in shiny with a list of variables created on server.R


I would like to use a list() generated on server.r to populate a paragraph in ui.r

server.r

shinyServer(function(input, output) {
    output$out <- reactive({
        list(
            a = 'brown',
            b = 'quick',
            c = 'lazy'
        )
    })
})

ui.r

library(shiny)
shinyUI(fluidPage(
    p('The ', output$out$a, output$out$b, 'fox jumps over the ', output$out$c, 'dog')
))

I know that the code is not correct and you have to use helper functions to access data in ui.r, but I just wanted to illustrate my problem.


Solution

  • Perhaps I'm not understanding your intent, but have a look at this:

    library(shiny)
    
    server <- function(input, output) {
      out <- reactive({
    
        tmp <- list()
        tmp <- list(
          a = 'brown',
          b = 'quick',
          c = 'lazy'
        )
    
        return(tmp)
      })
    
      output$a <- function() {
        out()[[1]]
      }
    
      output$b <- function() {
        out()[[2]]
      }
    
      output$c <- function() {
        out()[[3]]
        }
    }
    
    ui <- shinyUI(fluidPage(
      p('The ', textOutput("a"), textOutput("b"),
        'fox jumps over the ', textOutput("c"), 'dog')
    ))
    
    shinyApp(ui, server)