Search code examples
htmlrshinydtflexdashboard

Avoid line-breaks after hyphen in R Shiny


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    

Solution

  • You can disable the wrapping by using the class nowrap:

    DT::datatable(
      class = "display nowrap",
      extensions = c('Buttons','Scroller'),
      ......