3

I am trying to put interactive, sortable tables in html summaries produced by using rmarkdown::render from an R script. For producing tables I am using datatables() from DT package. Reports are generated fine and tables look pretty good, until you do a column level filter/search, after which display shows some funny problems. My question will become clearer with the following example.

#' ---
#' title: "Test"
#' author: test
#' output: 
#'   html_document:
#'     toc: true
#' ---

#' <style type="text/css">
#'   .main-container {
#'     max-width: 1200px;
#'     margin-left: auto;
#'     margin-right: auto;
#'   }
#' </style>

#' ### Test data

#+ setup, include=FALSE, echo=TRUE
require(dplyr)
require(DT)
knitr::opts_chunk$set(echo = TRUE)

#+ core_code, include=FALSE, echo=TRUE 
plants <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/cluster/plantTraits.csv")
plants<- plants %>% 
  mutate( ID = paste0("ID_" , sprintf("%04d", 1:136)  )  ) %>%
  select(ID, X:unsp)

#+ test_table, echo = FALSE
datatable( plants ,
           extensions = c("Buttons" , "FixedColumns"),
           filter = 'top',
           options = list( autoWidth = TRUE , 
                           dom = 'Blftip',
                           pageLength = 100,
                           searchHighlight = TRUE,
                           buttons = c('copy', 'csv', 'print'),
                           scrollX = TRUE,
                           fixedColumns = list(leftColumns = 2)),
           class = c('compact cell-border stripe hover') ,
           rownames = FALSE) 

Produces the table (screenshot): enter image description here

If I do a search for 048 in the ID column, it shows the right row, like this... enter image description here

But then, if I cancel the filter, and bring all the rows back, rows have characters missing from ID column. enter image description here

This would happen to any column that I search, or any other data. It does not happen if I use the main search box (at right side top corner). I am running RStudio(Version 1.1.463) on Mac(OS X 10.11.6) but I have tested the produced html file on Chrome, Safari, and RStudio inbuilt browser on Mac; and Chrome and IE on Win7. Any clues about how to address this?

ktyagi
  • 975
  • 10
  • 15
  • check if `ID_0001` is listed in ulterior rows? you may have new row order after filtering. – Ali Sheikhpour Nov 12 '18 at 16:02
  • There is only one row with ID_0001, the one with X == Aceca. The problem is that after filtering is removed, leading zeros in ID are gone, and in row number 4 in the screenshot ID is now showing as ID_0 where it should be ID_0004. – ktyagi Nov 12 '18 at 17:11

1 Answers1

2

This is not really a solution, but more of circumventing the problem. Since there were no suggestions, I starting disabling all the options I was using and it turns out that it was the highlight search results that was causing the issue. So if I do:

#+ test_table, echo = FALSE
datatable( plants ,
           extensions = c("Buttons" , "FixedColumns"),
           filter = 'top',
           options = list( autoWidth = TRUE , 
                           dom = 'Blftip',
                           pageLength = 100,
                           searchHighlight = FALSE,
                           buttons = c('copy', 'csv', 'print'),
                           scrollX = TRUE,
                           fixedColumns = list(leftColumns = 2)),
           class = c('compact cell-border stripe hover') ,
           rownames = FALSE) 

It works fine now.

ktyagi
  • 975
  • 10
  • 15