Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shiny renderTable omits unicode special characters

Tags:

r

shiny

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?

like image 425
Steve Powell Avatar asked Oct 15 '25 14:10

Steve Powell


1 Answers

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) )
like image 116
Stéphane Laurent Avatar answered Oct 17 '25 05:10

Stéphane Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!