Joanna
Joanna

Reputation: 673

R Shiny: paste multiple tags in one line

I wonder if there is a way to paste h2("A > B") and h5(" > C > D > E > F > G > H > I > J > K > L > M > N > O > P > Q > R > S > T > U > V > W > X > Y > Z") in one line. That is saying combine the feature of text2 and text3 together in Shiny.

My minimal shiny code:

library(shiny)
selecteds=as.list(as.factor(c(LETTERS)))

shinyApp(
  ui = shinyUI(
    fluidPage(
      uiOutput("text2"),uiOutput("text3")

    )
  ),
  server = function(input, output, session){

    output$text2 <- renderUI({
      paste(sapply(selecteds, paste, collapse=", "), collapse=" > ")
    })

    output$text3 <- renderUI({
      tags <- purrr::map(1:length(selecteds), function(level){
        # choose which tag to use based on level
        if(level < 3){
          purrr::map(selecteds[[level]], h2)
        } else {
          purrr::map(selecteds[[level]], h5)
        }
      })

      divs <- purrr::map(tags, function(x) {
        div(tagList(list = x))
      })
      tagList(list = divs)
    })
  }
)

Thanks!

Upvotes: 2

Views: 1875

Answers (1)

GyD
GyD

Reputation: 4072

You can include multiple HTML header levels in the same line by using the display: inline; CSS rule via the style = "display: inline;" argument.

library(shiny)
selecteds=c(LETTERS)

shinyApp(
  ui = shinyUI(
    fluidPage(
      uiOutput("text")
    )
  ),
  server = function(input, output, session){

    output$text <- renderUI({
      div(
        h2(paste(selecteds[1:2], collapse = " > "), style = "display: inline;"),
        h5(paste(c('', selecteds[3:length(selecteds)]), collapse = " > "), style = "display: inline;")
      )
    })
  }
)

Output:

Output

Upvotes: 4

Related Questions