0

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.

Bakaburg
  • 3,165
  • 4
  • 32
  • 64

1 Answers1

0

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)
Raoul Duke
  • 435
  • 3
  • 13
  • Yep, I hoped for a cleaner solution, creating only 1 output object instead of many different ones. But reading around I understood that either I do as you say or I produce the entire string in the server.r and then pass it onto ui.R, no other shortcuts. – Bakaburg Apr 09 '19 at 16:54
  • Here are some additional examples that might be helpful if you wish to pursue a more advanced solution: creating UI objects using a loop, https://shiny.rstudio.com/gallery/creating-a-ui-from-a-loop.html list of reactive values, https://stackoverflow.com/questions/43107978/accessing-reactivevalues-in-a-reactivevaluestolist – Raoul Duke Apr 09 '19 at 17:24