2

I have a dataframe that I created by reading in a csv file that includes line breaks. The line breaks were manually created in Excel with Alt + Enter. When previewing the dataframe, the line break is visible on the hover preview this image from a practice data set.

The same image shows that the line breaks are not clearly evident without the hover. When rendering to html, the line breaks appear as the "\n" character.

I am looking for a way to render the line breaks as actually line breaks (as it looks in the hover preview) when the dataframe is knitted to html. Because of other formatting limitations for the real data table, I am hoping to achieve this with either just printing the dataframe directly or producing a reactable object.

To replicate my dataframe in R:

multiline_df <- data.frame(User = c("DesWreck97", "T7tUrNoUt", "DstrctrOfDoom", "YAML_Hamilton"),
                           QualityPoints = c(7.80, 9.56, 8.87, 5.49),
                           Notes = c("1. Release code has been validated.\n2. Do not provided access without new confirmed key.", "1. Release code has not been validated.", "N/A", "1. Non-conforming profile structure; DSA.\n2. Three functions applied in last 60-day period.\n3. No user contact. Account primed for deletion at n < 180 days."),
                           ReturnValue = c("A", "A", "T", "A"))
BCovington
  • 21
  • 5
  • 1
    I think many table output packages would do this. huxtable does, flextable I think has the option, probably gt too. huxtable has an option to override `knit_print.data.frame`. But I'm not sure if that is the kind of solution you want. – dash2 Jun 29 '22 at 20:11

1 Answers1

0

This could work

library(reactable)
library(dplyr)
multiline_df <- data.frame(User = c("DesWreck97", "T7tUrNoUt", "DstrctrOfDoom", "YAML_Hamilton"),
                           QualityPoints = c(7.80, 9.56, 8.87, 5.49),
                           Notes = c("1. Release code has been validated.\n2. Do not provided access without new confirmed key.", "1. Release code has not been validated.", "N/A", "1. Non-conforming profile structure; DSA.\n2. Three functions applied in last 60-day period.\n3. No user contact. Account primed for deletion at n < 180 days."),
                           ReturnValue = c("A", "A", "T", "A"))


multiline_df %>% 
  mutate(Notes = stringr::str_replace(Notes, "\\n", "<br>")) %>% 
  reactable(columns = list(Notes = colDef(html = TRUE)))
Julian
  • 6,586
  • 2
  • 9
  • 33