Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carriage returns in DT package R Shiny

Tags:

r

dt

shiny

Is there a way to display carriage returns using the DT package in an R shiny app?

I have tried the code here:

library(DT)

# create data frame with an example column
test_df <- data.frame(SAME =  "Same Line", NEW = "New\nLine")

# print data frame
datatable(test_df)

The \n notation does not work, and it appears the datatable function replaces the \n with a space.

I want the second cell "New Line" to have the words "New" and "Line" on separate lines.

like image 555
easports611 Avatar asked Feb 09 '23 18:02

easports611


1 Answers

This solves the issue:

library(DT)

# create data frame with an example column
test_df <- data.frame(SAME =  "Same Line", NEW = "New\nLine")

# replace \n with <br/>
test_df$NEW <- gsub(pattern = "\n", replacement = "<br/>", x = test_df$NEW)

# print data frame 
# with escape set to FALSE
datatable(test_df, escape = FALSE)
like image 125
easports611 Avatar answered Feb 16 '23 02:02

easports611