Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I explicitly set column width for R DT tables using R Markdown?

I have a table with multiple variables with long variable names and long character strings as values for multiple observations. How can I explicitly set each column width, so the table rendered to HTML via R Markdown won't automatically double the lines to fit the long string into the cell?

It would be OK for me to have some columns that are long enough, even if the table is not able to show all the columns at the same time. But, I can always use the 'FixedColumns' extension and scroll bar to move to the right for more columns.

The codes I am currently using to set the column width are:

options = list(
    autowidth = TRUE,
    columnDefs = list(list(width = '600px', targets = c(1, 2))),

But no matter how I set the width differently, the final rendered table doest look too much different at all.

Here are some re-produceable codes that I used in rmarkdown, including generating the mock data and the DT object in HTML.

```{r}
# generate data
vec_1 <- c("DHDH, DDDDDTV", 
       "DHDH, DDDDDTV, TT&T",
       "DHDH, DDDDDTV, TT&T, Caaater",
       "DHDH, DDDDDTV, TT&T, Caaater, Cxx",
       "DHDH, DDDDDTV, TT&T, Cxx",
       "DHDH, DDDDDTV, Caaater",           
       "DHDH, DDDDDTV, Caaater, Cxx",
       "DHDH, DDDDDTV, Cxx")

vec_2 <- c("Some radomn long string aaa bbb ccc dddd aaa bbb ccccccc", 
       "Some radomn long stringa",
       "Some string aaa bbb",
       "Some radomn long string aaa bbb ccc dddd aaa bb (A)",
       "Some radomn long string aaa bbb ccc dddd aaa b (B)",
       "Some radomn long string aaa bbb ccc dd (D)",
       "Some radomn long string aaa bbb ccc ddd (D)",
       "Some radomn long string aaa bbb ccc d (D)",
       "Some radomn long string aaa bbb ccc ddd aaa bbb dddaa (G)",
       "Some radomn long string aaa bbb ccc ddd aaa bbb dddaa(G)", 
       "Some radomn long string aaa bbb ccc ddd aaa bbb ddd (G)",
       "Some radomn long strin(H)",
       "Some radomn long string  (G)",
       "Some radomn long (Beee)")

col_1 <- sort(rep(vec_1, 14))
col_2 <- rep(vec_2, 8)
col_3 <- c(rep(105, 14), rep(29, 14), rep(22, 14), rep(2, 14),
       rep(10, 14), rep(29, 14), rep(5, 14), rep(8, 14))
col_4 <- rnorm(112)
col_5 <- rnorm(112)
col_6 <- rnorm(112)
col_7 <- rnorm(112)
col_8 <- rnorm(112)
col_9 <- rnorm(112)

df <- data.frame(col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9)
```

```{r}
library(DT)

datatable(
    df,
    colnames = c("Source Type", "Factors",
                 "Market Counts", "Minimum", "1st Qu",
                 "Median", "Mean", "3rd Qu", "Maximum"),
    extensions = 'FixedColumns',
    options = list(
        autowidth = TRUE,
        columnDefs = list(list(width = '600px', targets = c(1, 2))),
        pageLength = 14,
        lengthMenu = c(7, 14, 28, 36),
        scrollX = TRUE,
        fixedColumns = TRUE
        )) %>%
    formatRound(4:9, digits = 2)
```

Here is a screen-shot of the DT produced in HTML using above codes: enter image description here

What I really want is to set the column width for the first two columns long enough, so the values in each row won't be clutter like this. I would also like the width for column 4:9 to be set the same. But it looks like that currently the width of these columns are dependent on the length of variable names, which doesn't look so great. Any workarounds I can do here to improve? Thanks,

like image 255
QY Luo Avatar asked May 02 '16 18:05

QY Luo


1 Answers

You can get a one line column by adding the following css to your html file:

<style>
table.display td { white-space: nowrap; }
</style>

Alternatively make a seperate .ccs file and include it with the yaml header. See the comment by clifton here for further discussions on the issue.

I was not able to set the width of the other columns quickly. I tried something like:

```{r}
library(DT)

datatable(
    df,
    colnames = c("Source Type", "Factors",
                 "Market Counts", "Minimum", "1st Qu",
                 "Median", "Mean", "3rd Qu", "Maximum"),
    extensions = 'FixedColumns',
    options = list(
        autowidth = FALSE,
        columnDefs = list(list(width = '600px', targets = c(1, 2)),
                          list(width = '100px', targets = 3:(ncol(df) - 2))),
        pageLength = 14,
        lengthMenu = c(7, 14, 28, 36),
        scrollX = TRUE,
        fixedColumns = TRUE
        )) %>%
    formatRound(4:9, digits = 2)
```

I think it is related to the points made here regarding setting the width when using the fixedColumns extension.

like image 165
Benjamin Christoffersen Avatar answered Nov 15 '22 21:11

Benjamin Christoffersen