Last active
April 3, 2020 07:25
-
-
Save yonicd/2f706dbd0d941602a3fb961112916b34 to your computer and use it in GitHub Desktop.
convert raw html to htmltools::tagList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#'@title html2tagList | |
#'@description convert raw html to htmltools tagList | |
#'@param x character vector of html | |
#'@examples | |
#' | |
#'x<-'<h1>Title</h1> | |
#' <h2>Header text</h2> | |
#' <p>Text here</p> | |
#' <h1>Title</h1> | |
#' <h2>Header text</h2> | |
#' <p>Text here</p>' | |
#' | |
#' html2tagList(x) | |
#' | |
#'require(xtable) | |
#'htmlIn<-print(xtable::xtable(mtcars),type = 'html',print.results = F) | |
#'htmlIn | |
#'tagL<-html2tagList(htmlIn) | |
#'class(tagL) | |
#'tagL | |
#'if(interactive()) htmltools::browsable(tagL) | |
#' | |
html2tagList<-function(x){ | |
x<-strsplit(gsub('>','>_AAA_',x),'_AAA_')[[1]] | |
#remove any \\n | |
x<-gsub('\\n','',x) | |
#remove comments | |
x<-x[!grepl('<!',x)] | |
#replace " with ' | |
x<-gsub('"',"'",x) | |
#replace close tag with ), | |
x<-gsub('</(.*?)>',"),",x) | |
#Attributted tags | |
attrIdx=grep('=',x) | |
attrVal=grep('=',x,value = T) | |
attrVal=gsub('>',",",attrVal) | |
attrVal=gsub('<','tags$',attrVal) | |
attrVal=sub('^ ','',attrVal) | |
attrVal=sub(' ',"(",attrVal) | |
x[attrIdx]=attrVal | |
#Non Attributed tags | |
x=gsub('<','tags$',x) | |
x=gsub('>',"(",x) | |
x=sub('^ ','',x) | |
x=sub(' )',')',x) | |
#add quotes in tags | |
endIdx=grep('),',x) | |
endVal=grep('),',x,value = T) | |
endVal=paste0("'",gsub('),',"'),",endVal)) | |
x[endIdx]=endVal | |
#remove last comma from collpased string | |
xout=paste0(x,collapse = '') | |
xout=gsub(',$','',xout) | |
#eval to tagList object | |
xout<-eval(parse(text=sprintf('tagList(list(%s))',xout))) | |
return(xout) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment