3

It seems that Shiny automatically introduces a line-break after hyphen (-) characters, at least when used together with datatables (see example below). I guess this is related to a similar problem in HTML, and probably happens when the document is rendered into HTML, but here replacing the hyphen by a "non-breaking" version (‑) won't work, neither does replacing the hyphen by the long version ().

Here is an example code reproducing this problem, where I create a variable with a minus sign in its name, and want to show its name (including the minus sign) in the table header. As you can see, a line-break after - appears.

---
title: "Example"
runtime: shiny
theme: simplex
vertical_layout: fill
output:
  flexdashboard::flex_dashboard:
  orientation: rows
---

Explore
======================================================================

```{r echo = FALSE}
library("tidyverse")
library("shiny")
library("DT")
```

```{r echo = FALSE}
createDataTable <- function (data) {

  data %>%  

    DT::datatable(
      extensions = c('Buttons','Scroller'),
      rownames=FALSE,
      options = list(
        paging = TRUE,
        pageLength = nrow(data),
        searching = TRUE,
        fixedColumns = TRUE,
        autoWidth = FALSE,
        ordering = TRUE,
        scroller = TRUE,
        scrollX = '400px',
        scrollY = '300px',
        dom = 'Bfrtip',
        buttons = list(
          'copy',
          list(
            extend='collection',
            buttons = list (
              list(extend='csv', filename='catalog'),
              list(extend='excel', filename='catalog'),
              list(extend='pdf', filename='catalog')
            ),
            text='Download'),
          'print'
        )
      )
    )
}
```

```{r echo = FALSE}
DT::renderDataTable(
  server=FALSE, {
  mtcars %>% dplyr::mutate(`-mpg` = -mpg, a_longer_name_for_mpg = mpg) %>% createDataTable()
})

```

Does anybody know how to avoid this behavior without changing the DT functions used, and without refusing to include the hyphen in the column header?

EDIT

It seems that if the page is view in full screen mode, there is no page-break. But it has nothing to do with the length of the variable name (the code is updated also to create a new variable with a longer name). So there should be a way to avoid this behavior, independently of the screen size.

This is my sessionInfo():

R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.8.0
LAPACK: /usr/lib/liblapack.so.3.8.0

locale:
 [1] LC_CTYPE=es_ES.UTF-8       LC_NUMERIC=C               LC_TIME=es_ES.UTF-8        LC_COLLATE=es_ES.UTF-8    
 [5] LC_MONETARY=es_ES.UTF-8    LC_MESSAGES=es_ES.UTF-8    LC_PAPER=es_ES.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.6.0        rsconnect_0.8.13      htmltools_0.3.6       tools_3.6.0           flexdashboard_0.5.1.1
 [6] yaml_2.2.0            Rcpp_1.0.1            rmarkdown_1.13        knitr_1.23            jsonlite_1.6         
[11] xfun_0.7              digest_0.6.19         evaluate_0.13    
elcortegano
  • 2,444
  • 11
  • 40
  • 58
  • could you add the packages and/or the environment (markdown...) required please ? I'm not very familiar with this sort of code – bretauv Jul 04 '19 at 14:41
  • I have edited the post with some libraries that could be missing, as well as with the result of `sessionInfo()` – elcortegano Jul 04 '19 at 14:53
  • well that's strange because when I set the window in full screen, there is no line-break (I'm using windows 10), have you tried to open it in browser ? – bretauv Jul 04 '19 at 15:00
  • Well, you are right, it seems that this *fixes* the problem, even when the size is reduced after using the full screen view. However, still cannot understand why this line-break. Note that if a new variable is added with a longer name, the line-break has nothing to do with it, but with the hyphen. I edit the question to show this. – elcortegano Jul 04 '19 at 15:11
  • 1
    I don't know yet how to fix it, but you should notice that it is not due to the hyphen in particular : if you replace -mpg by new_mpg for example, the problem is the same – bretauv Jul 04 '19 at 15:20

1 Answers1

4

You can disable the wrapping by using the class nowrap:

DT::datatable(
  class = "display nowrap",
  extensions = c('Buttons','Scroller'),
  ......
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225