Closed yogat3ch closed 6 years ago
Would you mind provide a minimal reproducible example?
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()
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()
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: