{"id":64,"date":"2019-08-06T13:50:53","date_gmt":"2019-08-06T13:50:53","guid":{"rendered":"http:\/\/blogs.oregonstate.edu\/cgrb\/?p=64"},"modified":"2024-09-18T13:36:15","modified_gmt":"2024-09-18T20:36:15","slug":"r-tips-a-table-makeover-with-dt","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/cqls\/2019\/08\/06\/r-tips-a-table-makeover-with-dt\/","title":{"rendered":"R-tips: A Table Makeover with DT"},"content":{"rendered":"\n<p>R\u2019s default print function for data frames and matrices is not an effective way to display the contents, especially in a html report. RStudio created a R package, <a href=\"https:\/\/rstudio.github.io\/DT\/\">DT<\/a>, that is an interface to the JavaScript library DataTable. DT allows users to have interactive tables that includes searching, sorting, filtering and exporting! I routinely use these tables in my analysis reports. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> Install the DT package from cran <\/h2>\n\n\n\n<p>First, one must install and load the DT package. Open up RStudio and run the following commands to install and load the DT package: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\n# Install the DT package\ninstall.packages(&quot;DT&quot;)\n# Load the DT package\nlibrary(DT)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"> Example Table <\/h2>\n\n\n\n<p>The print function is not the most effective was to display a table in an HTML R Markdown report. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\nprint(head(mtcars))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-10-1024x252.png\" alt=\"This image has an empty alt attribute; its file name is image-10-1024x252.png\" \/><\/figure>\n\n\n\n<p>Now let&#8217;s look at the <code>datatable <\/code>function for comparison. The input to the <code>datatable <\/code>function is a data frame or matrix. Let\u2019s make a table with the preloaded iris data that\u2019s in a data.frame. The basic call is <code>DT::datatable(iris)<\/code> but in our example I\u2019ve added the filter option to the top of the table, and limited the number of entries to 5 per table. See code and table features below:   <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\ndatatable(iris, filter = &quot;top&quot;, \n          options = list(pageLength = 5))\n<\/pre><\/div>\n\n\n<p> A screen shot of the output looks like: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-1024x490.png\" alt=\"This image has an empty alt attribute; its file name is image-1024x490.png\" \/><\/figure>\n\n\n\n<p>Already, the readability is much better than the base r function print. This is a JavaScript based table, stored in a HTML widget, so a flat image doesn&#8217;t convey all of the interactive features.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Features<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">NUMBER OF ENTRIES TO DISPLAY<\/h3>\n\n\n\n<p>You&#8217;ll notice that there is a drop down menu that says: &#8220;Show 5 entries&#8221;.  The default is 10, but I specified 5 as default with the code <code>pageLength=5<\/code>. One may select the number of entries to show by using the drop down menu like so: <\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-1.png\" alt=\"This image has an empty alt attribute; its file name is image-1.png\" width=\"232\" height=\"156\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">SEARCH BAR<\/h3>\n\n\n\n<p>The widget also includes a search bar on the top right corner which can be very useful when interactively exploring data. Note at the bottom of the table it shows you how many entries (rows) were found and are being displayed.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-2.png\" alt=\"This image has an empty alt attribute; its file name is image-2.png\" width=\"204\" height=\"60\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-3-1024x58.png\" alt=\"This image has an empty alt attribute; its file name is image-3-1024x58.png\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">SORT COLUMNS<\/h3>\n\n\n\n<p>Notice that to the right of each column name are two arrows: <img loading=\"lazy\" decoding=\"async\" width=\"169\" height=\"46\" class=\"wp-image-25\" style=\"width: 100px\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-4.png\" alt=\"\">  One may sort by ascending or descending order and the direction of the blue arrow indicates by which direction you sorted the column.  <\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-6.png\" alt=\"This image has an empty alt attribute; its file name is image-6.png\" width=\"102\" height=\"210\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">FILTER COLUMNS<\/h3>\n\n\n\n<p>The <code>datatable<\/code> function also allows users to filter each column depending on the datatype:  filter <strong>numeric columns<\/strong> with a slider &amp;  filter columns of <strong>class factor <\/strong>with a drop down menu. One must add the <code>filter = \"top\"<\/code> (or bottom, etc.) to the code to enable this feature. <\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-7.png\" alt=\"This image has an empty alt attribute; its file name is image-7.png\" width=\"242\" height=\"171\" \/><figcaption>Numeric columns have sliders<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-8.png\" alt=\"This image has an empty alt attribute; its file name is image-8.png\" \/><figcaption>Columns of class factor have a drop down menu<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Export Data<\/h2>\n\n\n\n<p>Another useful aspect of the datatable function is the \u201cButtons\u201d extension. This enables users to copy the table, save as a csv, excel or PDF file, or print the table. The table \u201cremembers\u201d what you\u2019ve changed so far\u2014so if you sort by Sepal Length, filter pedal width to &gt; 1 and select species \u201cversicolor\u201d the copied\/saved table will have these same restrictions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\ndatatable(iris, \n          extensions = &#039;Buttons&#039;,\n          options = list(dom = &#039;Bfrtip&#039;, \n                         buttons = c(&#039;copy&#039;, &#039;csv&#039;, &#039;excel&#039;, &#039;pdf&#039;, &#039;print&#039;))\n<\/pre><\/div>\n\n\n<p>The above code adds &#8220;buttons&#8221; to the top of the table like so: <\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-9.png\" alt=\"This image has an empty alt attribute; its file name is image-9.png\" width=\"261\" height=\"68\" \/><\/figure>\n\n\n\n<p>If one clicks &#8220;copy&#8221;, the table will be copied to your clipboard, &#8220;CSV&#8221; or &#8220;PDF&#8221; will save the table to the give file type, and &#8220;print&#8221; will bring put the table into a print friendly format and will bring up the print dialog box.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Links and Color<\/h2>\n\n\n\n<p>One may also have links in their table.  Say you made a data frame with links you want to work in your html report. For example: a data frame of variants w\/ links to their position in a genome browser. This is done through not escaping content in the table, specifically the column with the links. The links are made with html and must <strong>not<\/strong> be escaped to show up. This applies to other html as well; including color. For me, it was confusing that I had to <strong>not<\/strong> escape the html columns. Got it completely backwards the first time I tried it. <strong>NOTE<\/strong>: <code>&gt;<\/code> got replaced with  &#8220;&amp; gt;&#8221;  (with no spaces) when it is rendered on the blog&#8230; Need to find a fix! <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; auto-links: false; title: ; notranslate\" title=\"\">\n# Make dataframe\ndf.link &lt;- data.frame(school=c(&quot;OSU&quot;, &quot;UO&quot;, &quot;Linfield&quot;, &quot;Willamette&quot;), \n                      mascot=c(&quot;beavers&quot;, &quot;ducks&quot;, &quot;wildcats&quot;, &quot;bearcats&quot;),\n                      website=c(&#039;&lt;a href=&quot;http:\/\/oregonstate.edu\/&quot;&gt;oregonstate.edu&lt;\/a&gt;&#039;,\n                                &#039;&lt;a href=&quot;https:\/\/www.uoregon.edu\/&quot;&gt;uoregon.edu&lt;\/a&gt;&#039;,\n                                &#039;&lt;a href=&quot;https:\/\/www.linfield.edu\/&quot;&gt;linfield.edu&lt;\/a&gt;&#039;,\n                                &#039;&lt;a href=&quot;https:\/\/www.willamette.edu\/&quot;&gt;willamette.edu&lt;\/a&gt;&#039;),\n                      School_colors=c(&#039;&lt;span style=&quot;color:orange&quot;&gt;orange &amp; black&lt;\/span&gt;&#039;, \n                                        &#039;&lt;span style=&quot;color:green&quot;&gt;green &amp; yellow&lt;\/span&gt;&#039;, \n                                        &#039;&lt;span style=&quot;color:purple&quot;&gt;purple and red&lt;\/span&gt;&#039;, \n                                        &#039;&lt;span style=&quot;color:red&quot;&gt;red and yellow&lt;\/span&gt;&#039;))\n\n# When the html columns, 3 &amp; 4, are not escaped, it works!\ndatatable(df.link, escape = c(1,2,3))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-11-1024x333.png\" alt=\"This image has an empty alt attribute; its file name is image-11-1024x333.png\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Column Visibility<\/h2>\n\n\n\n<p>One may also hide columns from visibility and add a button to add the column back interactively.  For example, say we have a data frame called <code>sv.all.i.in<\/code>. We can hide columns 3 and 4, which are long sequences and disrupt the readability of the table, with the following code: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\ndatatable(sv.all.i.in, extensions = &#039;Buttons&#039;,\n          options = list(dom = &#039;Bfrtip&#039;, columnDefs = list(list(visible=FALSE, targets=c(3,4))), \n                         buttons = list(I(&#039;colvis&#039;),c(&#039;copy&#039;, &#039;csv&#039;, &#039;excel&#039;, &#039;pdf&#039;, &#039;print&#039;))))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3381\/files\/2019\/08\/image-12.png\" alt=\"This image has an empty alt attribute; its file name is image-12.png\" width=\"528\" height=\"365\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Learn More<\/h2>\n\n\n\n<p>There are many more useful features that you can add to your <code>datatable<\/code>! Learn more here:  <a href=\"https:\/\/rstudio.github.io\/DT\/\">https:\/\/rstudio.github.io\/DT\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>R\u2019s default print function for data frames and matrices is not an effective way to display the contents, especially in a html report. RStudio created a R package, DT, that is an interface to the JavaScript library DataTable. DT allows users to have interactive tables that includes searching, sorting, filtering and exporting! I routinely use&hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/cqls\/2019\/08\/06\/r-tips-a-table-makeover-with-dt\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":9584,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1370809,2991,1370810,1370811,1370806],"tags":[712761,1414,1365448,913],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-bioinformatics","category-documentation","category-r","category-rmarkdown","category-tutorial","tag-r","tag-report","tag-rmarkdown","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/users\/9584"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":13,"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/posts\/64\/revisions\/77"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/cqls\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}