rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
599 stars 180 forks source link

DT::datatable repeats a previous table #621

Closed yogat3ch closed 6 years ago

yogat3ch commented 6 years ago

Hi Rstudio Dev, There appears to be a strange bug in DT::datatable when knitting to HTML. A call to DT::datatable will print a previous table rather than the intended table. The document that this is happening in can be found here. right after the line that reads "And with students in the title:". As you can see, datatable repeats the document term frequency table from earlier in the document. I've tried changing the way the data for the table is passed to datatable, using the following formats: lapply(summary_records,FUN = extract_from_esummary,c("title")) %T>% assign(pub_tchar,.,envir = .GlobalEnv) %>% .[str_detect(.,"cognitive")] %>% do.call("rbind",.) %>% as.data.frame %>% DT::datatable()

pub_tchar <- lapply(summary_records,FUN = extract_from_esummary,c("title")) %>% .[str_detect(.,"cognitive")] %>% do.call("rbind",.) %>% as.data.frame
DT::datatable(pub_tchar)

yet it still prints the previous term frequency table. I've taken code from the same chunk with another datatable into it's own chunk (as in the doc) and this didn't remedy the issue. Any ideas?

Session Info:

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] bindrcpp_0.2.2  rentrez_1.2.1   magrittr_1.5    htmltools_0.3.6
 [5] forcats_0.3.0   stringr_1.3.1   dplyr_0.7.7     purrr_0.2.5    
 [9] readr_1.1.1     tidyr_0.8.1     tibble_1.4.2    ggplot2_2.2.1  
[13] tidyverse_1.2.1 knitr_1.20      arules_1.6-1    Matrix_1.2-14  

loaded via a namespace (and not attached):
 [1] nlme_3.1-137         fs_1.2.6             usethis_1.4.0       
 [4] lubridate_1.7.4      devtools_2.0.0       httr_1.3.1          
 [7] rprojroot_1.3-2      tools_3.5.1          backports_1.1.2     
[10] R6_2.3.0             DT_0.5.1             lazyeval_0.2.1      
[13] colorspace_1.3-2     withr_2.1.2          prettyunits_1.0.2   
[16] tidyselect_0.2.5     mnormt_1.5-5         processx_3.1.0      
[19] curl_3.2             compiler_3.5.1       cli_1.0.1           
[22] rvest_0.3.2          formatR_1.5          xml2_1.2.0          
[25] desc_1.2.0           scales_0.5.0         psych_1.8.4         
[28] callr_2.0.4          digest_0.6.18        foreign_0.8-70      
[31] rmarkdown_1.9        pkgconfig_2.0.2      sessioninfo_1.1.0   
[34] htmlwidgets_1.3      rlang_0.3.0.1        readxl_1.1.0        
[37] rstudioapi_0.7       textmineR_3.0.1      shiny_1.1.0         
[40] bindr_0.1.1          jsonlite_1.5         crosstalk_1.0.0     
[43] text2vec_0.5.1       futile.logger_1.4.3  Rcpp_0.12.19        
[46] munsell_0.4.3        RcppProgress_0.4.1   stringi_1.2.4       
[49] yaml_2.1.19          snakecase_0.9.1      pkgbuild_1.0.2      
[52] plyr_1.8.4           grid_3.5.1           parallel_3.5.1      
[55] promises_1.0.1       mlapi_0.1.0          crayon_1.3.4        
[58] lattice_0.20-35      haven_1.1.1          hms_0.4.2           
[61] pillar_1.3.0         pkgload_1.0.1        reshape2_1.4.3      
[64] codetools_0.2-15     stopwords_0.9.0      futile.options_1.0.1
[67] XML_3.98-1.16        glue_1.3.0           evaluate_0.10.1     
[70] remotes_2.0.1        lambda.r_1.2.3       data.table_1.11.4   
[73] RcppParallel_4.4.1   modelr_0.1.2         httpuv_1.4.3        
[76] foreach_1.4.4        testthat_2.0.0       cellranger_1.1.0    
[79] gtable_0.2.0         assertthat_0.2.0     mime_0.6            
[82] xtable_1.8-3         broom_0.4.4          later_0.7.2         
[85] rsconnect_0.8.8      iterators_1.0.9      memoise_1.1.0
shrektan commented 6 years ago

Would you mind provide a minimal reproducible example?

yogat3ch commented 6 years ago

Ok, so while putting together the reproducible example below and testing it - the situation seemed to resolve itself when the repro was put into a new Rmd doc and knitted. This suggested that the issue might have had to do with the knitr cache. I went back to the original document, cleared the Knitr cache and it does seem to have resolved the issue. http://rpubs.com/yogat3ch/440283

Thus, if anyone finds that their tables are being duplicated by DT::datatable() in their knitr document, it likely has to do with the cache and clearing it resolves it.

Case closed, thanks for the prompting to do the repro!

req.packages <- c("tidyverse","htmltools","magrittr","rentrez","rvest","xml2","DT","textmineR")
startPkgs <- Vectorize(FUN=function(pkg){suppressPackageStartupMessages(library(pkg,character.only = T))})
invisible(startPkgs(req.packages))
htm <- xml2::read_html("https://www.nlm.nih.gov/mesh/pubtypes.html")
ptyps <- data.frame(Name = htm %>% rvest::html_nodes("dt") %>% rvest::html_text(),Desc = htm %>% rvest::html_nodes("dd") %>% rvest::html_text(),stringsAsFactors = F)
tags$p("Types used in Query")
ptyps[c(3,5,33:38,43,47,66,100,106,124,134,136:141,145,160),]
entrez_db_searchable("pubmed")
p_types <- paste(ptyps[c(3,5,33:38,43,47,66,100,106,124,134,136:141,145,160),1],"[PTYP]",sep="") %>% paste(collapse = " OR ")
educ <- paste(c("education","elementary","student","grade","grader","middle School","high school","undergraduate","university","graduate","pedagogy"),"[TIAB]",sep="") %>% paste(collapse = " OR ")
tags$p("Query:")
(query <- paste0("mindfulness[TIAB] OR meditation[TIAB] OR meditation[MESH] AND ","(",educ,")"," AND ","2008:2018[EDAT]"," AND ","(",p_types,")"))
(ids <-  entrez_search(db="pubmed", term=query, sort="relevance",retmax = 1000, use_history = T))
summary_records <- list()
for(seq_start in seq(1,round(length(ids$ids),-1),50)){
recs <-  entrez_summary(db="pubmed", web_history=ids$web_history, retmax=50,retstart=seq_start)
summary_records <- append(recs,summary_records,after = length(summary_records))
}
tags$h3("All Titles")
lapply(summary_records,FUN = extract_from_esummary,c("title")) %>% do.call("rbind",.) %>% as.data.frame %>% DT::datatable()
pub_titles <- lapply(summary_records,FUN = extract_from_esummary,c("title")) %>% lapply(function(.)strsplit(.,"\\s{1,}"))
pub_tm <- textmineR::CreateDtm(pub_titles)
tags$h3("Term Frequency:")
textmineR::TermDocFreq(pub_tm) %>% arrange(desc(term_freq)) %>% DT::datatable()
pub_dtm <- pub_tm %>% textmineR::Dtm2Docs()
tags$p("It looks like cognitive and students appear frequently, these are the studies with cognitive in the title:")
(pub_tchar <- lapply(summary_records,FUN = extract_from_esummary,c("title"))) %>% .[str_detect(.,"cognitive")] %>% do.call("rbind",.) %>% as.data.frame %>% DT::datatable()
tags$p("And with students in the title:")
pub_tchar %>% .[str_detect(.,"students")] %>% do.call("rbind",.) %>% as.data.frame %>% DT::datatable()