Steve Powell
Steve Powell

Reputation: 1698

shiny renderTable omits unicode special characters

I have a table in a shiny app which includes Unicode special characters. But they are omitted, not shown at all, in the tableOutput.

library(shiny)


ui <- fluidPage(
      tableOutput("table")
)

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

    output$table = renderTable({

      mtcars[2,2]="◨"
      mtcars[1:3,1:3]
    }

    ,sanitize.text.function = identity)

}

shinyApp(ui = ui, server = server)

enter image description here

santitze.text.function doesn't seem to make any difference. I believe this is an option for print.xtable - and anyway print.xtable reproduces the characters fine in the console. If I use renderDataTable and dataTableOutput, the characters are shown fine (as actual Unicode characters). So why don't they appear with renderTable and tableOutput?

Upvotes: 4

Views: 1061

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

This works with the html entity:

  output$table = renderTable({

    mtcars[2,2]="&#9704;"
    mtcars[1:3,1:3]
  }

  , sanitize.text.function = identity)

If you don't want to search the html code on the web, you can get it like this:

sprintf("&#%d;", utf8ToInt("◨"))

Finally you can automate as follows. Define a "sanitizer" function:

f <- function(x){
  xs <- strsplit(as.character(x), "")[[1]]
  paste0(sprintf("&#%d;", sapply(xs, utf8ToInt)), collapse="")
}

Then use it in renderTable:

  output$table = renderTable({

    mtcars[2,2]="◨"
    mtcars[1:3,1:3]
  }

  , sanitize.text.function = function(x) sapply(x, f) )

Upvotes: 3

Related Questions