How to wrap text in datatable

4,578 views
Skip to first unread message

rdabbler

unread,
Aug 21, 2014, 10:18:26 PM8/21/14
to shiny-...@googlegroups.com
Hello,

When I use data datatable output with long text, I am not able to figure out how to wrap the text. Below is an example ui.R and server.R I am using for testing

ui.R:

library(shiny)

shinyUI(fluidPage(
   fluidRow(
        dataTableOutput(outputId="table")
          )
  ))

server.R

library(shiny)

mystr=paste(letters,letters,letters,collapse='',sep="")
df=data.frame(v1=rep(mystr,50),v2=rep(mystr,50),v3=rep(mystr,50),v4=rep(mystr,50))

shinyServer(function(input,output){
  
  output$table = renderDataTable({
    df
  })

})

WIth long text like this, I have to scroll the window to view the columns. Is there a way to word wrap the text. I will appreciate any help.

I am using shiny_0.10.0 and RStudio 0.98.932 in Windows.

Thanks,
Shankar

Yihui Xie

unread,
Sep 2, 2014, 11:32:28 PM9/2/14
to rdabbler, shiny-discuss
You can set the column widths in DataTables options:
http://datatables.net/reference/option/columns.width Note the
documentation is for DataTables 1.10.x, which is currently supported
in the development version of shiny:
https://groups.google.com/forum/#!topic/shiny-discuss/-0u-wTnq_lA

For example, if you want the width of the 5th column to be 30%, you can

renderDataTable(..., options = list(columnDefs=list(list(width =
'30%', targets = 4))))


Regards,
Yihui

rdabbler

unread,
Sep 8, 2014, 9:03:54 PM9/8/14
to shiny-...@googlegroups.com, dabbl...@gmail.com
Yihui,

Thanks very much for the pointers above. For some reason when I try the following code, the column width is not changing.

library(shiny) 
mystr=paste(letters,letters,letters,collapse='',sep="")
df=data.frame(v1=rep(mystr,50),v2=rep(mystr,50),v3=rep(mystr,50),v4=rep(mystr,50))
runApp(list( 
  ui = fluidPage(dataTableOutput("mytable")), 
  server = function(input, output) { 
    output$mytable = renderDataTable(df,options = list(columnDefs=list(list(width = 
                                                                                '10%', targets = 1)))) 
  } 
)) 

I am using the latest dev version of Shiny (shiny_0.10.1.9007) within RStudio version 0.98.994. 
My machine is a Windows 8 machine

Regards,
Shankar

Yihui Xie

unread,
Sep 17, 2014, 5:00:08 PM9/17/14
to rdabbler, shiny-discuss
Sorry, I can reproduce the problem but I do not know what is going on
there. The width was indeed set in the table, but for some reason
ignored.

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages