0

I have a dataframe of the following form:

myDf <- data.frame(doc = c(1, 2, 3), text = c("Statement 1: ok<br/> Statement 2: not ok", 
"Statement 1: true\n Statement 2: false", "Statement 1: yes Statement 2: no"))

Which I would like to render as the following in a shiny app:

doc text
1   1   Statement 1: ok
        Statement 2: not ok
2   2   Statement 1: true
        Statement 2: false
3   3   Statement 1: yes Statement 2: no

However, each time I try (either using br or \n) the text is rendered verbatim.

Code for Shiny app:

library(shiny)
server <- function(input, output) {
  myDf <- data.frame(doc = c(1, 2, 3), text = c("Statement 1: ok<br/> Statement 2: not ok", "Statement 1: true\n Statement 2: false", "Statement 1: yes Statement 2: no"))

  output$dfTable <- renderTable({ myDf })
}




ui <- fluidPage(
    mainPanel(tableOutput("dfTable"))
)



shinyApp(ui = ui, server = server)

I have found one webpage that mentions this issue, but discusses it more in the context of rmarkdown and mustache. Another discusses this in context of Latex.

info_seekeR
  • 1,296
  • 1
  • 15
  • 33

1 Answers1

0

They way I got this working was to create the table using shiny tags and then rendering the the HTML. It doesnt come with the standard styling so it will require CSS to make the table how you would like. However, I came across these tags when I required dynamic dropdowns generated from MySQL db. Anyway, the below code may get you on your way.

myDF <- tags$table(tags$tr(tagList(mapply(tags$th,c("doc", "text"), SIMPLIFY = FALSE)
                           )
                    ),
           tags$tr(tags$td(1), tags$td(tags$ul(tagList(mapply(tags$li,c("Statement 1: ok", "Statement 2: not ok"), SIMPLIFY = FALSE))))),
           tags$tr(tags$td(2), tags$td(tags$ul(tagList(mapply(tags$li,c("Statement 1: true", "Statement 2: false"), SIMPLIFY = FALSE))))),
           tags$tr(tags$td(3), tags$td(tags$ul(tagList(mapply(tags$li,c("Statement 1: yes", "Statement 2: no"), SIMPLIFY = FALSE)))))
                   )

creates the required HTML. Below is the shiny part. Note you have to change from renderTable to renderUI.

server <- function(input, output) {

  output$dfTable <- renderUI({ myDf })
}




ui <- fluidPage(
    mainPanel(tableOutput("dfTable"))
)



shinyApp(ui = ui, server = server)

Would love to know if this could be achieved without resorting to building the HTML.

amwill04
  • 1,330
  • 1
  • 11
  • 18