0

How to insert escape characters into dataframe?

It will be useful for the formatting of text data into table packages.

We could be use one universal solution for every table package (my hope).

But now we should employ a lot of approaches for the every case (depends on libraries and output formates: pdf, html).

It is really uncomfortable!

For example, we have next data

df <- data.frame("names" = c("a long long long long long ... pretty long name of the our story","Here is also long, but a little shorter"))

We see a normal output

                                                             names
1 a long long long long long ... pretty long name of the our story
2                          Here is also long, but a little shorter

But how we can achieve this output ("\n")?

                                                             names
1                                       a long long long long long 
                             ... pretty long name of the our story
2                          Here is also long, but a little shorter

I promised to add a simple example:

knit to HTML, in PDF doesn't work

```{r warning = FALSE, echo = FALSE, message=FALSE, include=FALSE,}
library(flextable)
library(gt)

#data
df <- data.frame("names" = c("a long long long \n long long ... <br> pretty long name of the our story","Here is also long, but a little shorter"))
#table one
table3 <- flextable(df)
table3 <- set_table_properties(table3, layout = "autofit", width = .5)
table3

#table two
table4 <- gt(df) %>% fmt_markdown(columns = everything())
table4
```

<center>
<caption>Table in flextable </caption>
</center>
`r table3`

<center>
<caption>Table in gt </caption>
</center>
`r table4`

In the first case - works \n, in the second - /br/ and there are only two packages!

enter image description here

manro
  • 3,529
  • 2
  • 9
  • 22
  • 1
    Sounds like you want something like `stringr::str_wrap()`. – Ritchie Sacramento Nov 27 '21 at 13:50
  • @RitchieSacramento Interesting. I try to realize my task with ```str_wrap``` now. And return after. But question is still opened. It will be cool to embed escaping without any bells ans whistles. – manro Nov 27 '21 at 14:02
  • @RitchieSacramento No, can't make desired output( – manro Nov 27 '21 at 14:37
  • you could include the line break as "\n" on the wanted position of the cell value and then use the [huxtable](https://cran.r-project.org/web/packages/huxtable/vignettes/huxtable.html) package to print it: huxtable::as_huxtable(df) this way the print out will be a table where the "\n" are converted to actual line breaks... not sure if that is what you are looking for – DPH Nov 27 '21 at 14:38
  • @DPH Not with ```huxtable```, but you are right. I want to prepare text data for using into table packages. "\t" is also useful. – manro Nov 27 '21 at 14:43
  • 1
    @manro what are you trying to achive exactly ... possibly you are looking to print a table in markdown where linebreaks are shown as such? If so the kableExtra might be an option like explained [here](https://stackoverflow.com/questions/52944533/wrap-text-in-knitrkable-table-cell-using-n) – DPH Nov 27 '21 at 14:46
  • @DPH Yes, we are near. Not only in the rmarkdown, in the viewer too. But I want to find a more universal solution. Because in one situation we should use ```kableExtra``` in another - ```gt``` etc. – manro Nov 27 '21 at 14:58

1 Answers1

2

Don't add any line breaks to your text!! This is absolutely unnecessary. All you need is the skillful use of the kableExtra package. Note the code below is the content from the RMarkdown. The document created with it can be found here.

Add everything from ## RMarkdown Start to ## RMarkdown End to RMarkdown Of course, don't forget to remove the comment characters before the code blocks (#```)!!

## RMarkdown Start
---
title: "LongText"
author: "Marek Fiołka"
date: "30 11 2021"
output:
  html_document: default
  pdf_document: default
---

#```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
#```

Prepare data frame with long text

#```{r echo=TRUE, message=FALSE, warning=FALSE}
library(stringi)
library(tidyverse)
library(kableExtra)

n=50
df = tibble(
  id = 1:n,
  txt = stri_rand_lipsum(n)
)
#```

See the data frame in a typical display

#```{r}
print(df)
#```

View data frame formatted with Kable Extra for HTML

#```{r}
df %>% kbl() %>%
  kable_styling()
#```

View data frame formatted with Kable Extra for PDF

#```{r}
df %>% kbl() %>%
  kable_paper(full_width = F) %>%
  column_spec(1, bold = T, border_right = T) %>%
  column_spec(2, width = "30em")

## RMarkdown End

This way you can create not only html documents but also corresponding pdf documents. Below is a fragment of a document created in this format. Note that in this case you have to use kable_paper with column_spec instead of kable_styling.

enter image description here

Hope this is what you were looking for.

Update

## RMarkdown Start
---
title: "LongText"
author: "Marek Fiołka"
date: "30 11 2021"
output:
  pdf_document: default
  html_document: default
---

#```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
#```

Prepare data frame with long text

#```{r echo=TRUE, message=FALSE, warning=FALSE}
library(stringi)
library(tidyverse)
library(kableExtra)

n=10
df = tibble(
  id = 1:n,
  txt = stri_rand_lipsum(n)
)
#```

See the data frame in a typical display

#```{r echo=TRUE}
print(df)
#```

View data frame formatted with [kableExtra](http://haozhu233.github.io/kableExtra/) for HTML

#```{r}
df %>% kbl() %>%
  kable_styling()
#```

View data frame formatted with [kableExtra](http://haozhu233.github.io/kableExtra/) for PDF

#```{r}
df %>% kbl() %>%
  kable_paper(full_width = F) %>%
  column_spec(1, bold = T, border_right = T) %>%
  column_spec(2, width = "30em")

#```

View data frame formatted with [gt](https://gt.rstudio.com/index.html) (only for HTML)

#```{r}
library(gt)
df %>% gt %>% 
  fmt_markdown(columns = everything()) %>%
    tab_options(table.width = px(400))
#```

View data frame formatted with [flextable](https://davidgohel.github.io/flextable/articles/overview.html) HTML & PDF

#```{r message=FALSE, warning=FALSE}
library(flextable)

df %>% flextable %>% width(2, width = 6, unit = "in")
#```

View data frame formatted with [huxtable](https://hughjonesd.github.io/huxtable/) HTML & PDF

#```{r message=FALSE, warning=FALSE}
library(huxtable)

df %>% as_hux %>% 
  set_width(1) %>% 
  set_right_border(everywhere, 1, brdr(3, "double", "grey"))
#```
## RMarkdown End
Marek Fiołka
  • 4,825
  • 1
  • 5
  • 20
  • Thanks, Mr. Marek. Can you try to make this trick for some other table packages? (knit to PDF)```gt or flextable?``` As I said in my question, sometimes we need to use different instruments which can give their features for our task. Bounty is prepared for you ;) – manro Nov 30 '21 at 21:30
  • Here you are. Now you have it in four different ways. I also updated the [report](https://rpubs.com/MarekFiolka/841535) on my RPubs. – Marek Fiołka Nov 30 '21 at 23:22
  • Cool, your solution looks like universal one? – manro Dec 01 '21 at 06:33
  • I don't know if my solution can be called universal. It seems to solve your problem in most of the ways possible. I would suggest to get acquainted one library (I recommend kable Extra or [kableExtra](http://haozhu233.github.io/kableExtra/) or [flextable](https://ardata-fr.github.io/flextable-book/) ) and fine-tune the table view that suits you best. – Marek Fiołka Dec 01 '21 at 15:54
  • I haven't any task now, only preparing for the future deeds. Dzięki, panu Marek ;) I'll add a bounty to you in the last day for better impact of the post ;) – manro Dec 01 '21 at 16:10