Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Last active April 7, 2017 00:31
Show Gist options
  • Save MattSandy/601926a35dca85360159cafd64987d02 to your computer and use it in GitHub Desktop.
Save MattSandy/601926a35dca85360159cafd64987d02 to your computer and use it in GitHub Desktop.
Create html table from dataframe
html.data.frame <- function(table,id="records") {
df <- data.frame(table)
for(i in 1:ncol(df)) {
df[,i] <- as.character(df[,i])
df[,i] <- gsub("&", "&amp;", df[,i])
df[,i] <- gsub("<", "&lt;", df[,i])
df[,i] <- gsub(">", "&gt", df[,i])
}
html <- paste0('<table id="',id,'">')
html <- paste0(html,"<thead><tr><td>",paste(names(df),collapse = "</td><td>"),"</td></tr></thead><tbody>")
html <- paste0(html,paste(unlist(apply(df,1,function(x) {
return(paste0("<tr><td>",paste(x,collapse = "</td><td>"),"</td></tr>"))
})),collapse = " "))
html <- paste0(html,"</tbody></table>")
return(paste(html,collpase=""))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment