0

I've been struggling for a few hours with such a task: in R Shiny I need to display a table which contains of a single column of integers with the definite (relatively large) spacing between rows.

There is the spacing argument in the renderTable() function, but even setting it to the biggest value 'l' is still not enough for my purpose.

I've tried to do that using xtable and taking into account the example from Adjust row height xtable R , but with no result (I don't know CSS).

The most natural way I've found in the web is to use DT package along with the Scroller extension, but the following code still gives no results

ui.R:

fluidPage(
  sidebarLayout(
    sidebarPanel(
        dataTableOutput('dtable', width = '50%') # argument 'height' does not work here
        ),
    mainPanel()
  )
)

server.R:

library(shiny)
library(DT)

function(input, output) {
output$dtable <- DT::renderDataTable({
  data.frame(SSD = c(2, 17, 19, 35))
},
extensions = 'Scroller',
options = list(
  dom = 't',
  ordering = FALSE,
  scroller = list(rowHeight = 100)
)
)
}

The output of that gives only column name (what is wrong??), but without Scroller extensions it displays the expected table - of course with too small spacing...

Community
  • 1
  • 1
Pawel
  • 401
  • 6
  • 17

1 Answers1

4

You want to use the rowCallback option and attach a style to each row:

server.R

library(shiny)
library(DT)

function(input, output) {
    output$dtable <- DT::renderDataTable({
        data.frame(SSD = c(2, 17, 19, 35))
    },
    options = list(
        dom = 't',
        ordering = FALSE,
        rowCallback = JS("function(r,d) {$(r).attr('height', '100px')}")
    )
    )
}

Note that this may result in increased render time as the number of rows raises

Community
  • 1
  • 1
GGamba
  • 13,140
  • 3
  • 38
  • 47
  • It does exactly what I want. Thank you GGamba! Anyway, I am still curious, whether the `rowHeight` option could yield the effect in accord to its name? Maybe something is wrong with my syntax? Do you know that? – Pawel Feb 23 '17 at 17:11
  • The `Scroller` extensions is useful to improve rendering time by only render the visible part of the datatable. To do this it have to know what the height of the rows, to compute the number of rows to render. It does not affect the actual height of the rows, but rather it's used to know 'How many rows do I render in this viewport?' – GGamba Feb 24 '17 at 09:44