Search code examples
htmlrdocumenttablehtml

Combine / merge multiple HTML documents in r


I have a folder with a multiple html files that I would like to combine into one file in R. can be just combined as one long file or (ideally) a more fancy file with table of contents.

This is to create very simple files to reproduce the problem.

library(tableHTML)
   
   x=data.frame(x=c(1,2,3))
   y=data.frame(y=c(4,5,6))
   
   
   
   tableHTML::write_tableHTML(tableHTML(x), "x.html") 
   
   tableHTML::write_tableHTML(tableHTML(y), "y.html")

I found this package http://richardhooijmaijers.github.io/R3port/ which has html_combine function. However, when I try the code below, it does not work saying that this is not a raw html file.

  library(R3port) 
   
   
   html_combine(
     combine = getwd(), #or whichever folder the html files are stored at
     out = "to.html",
     toctheme = TRUE,
     css = paste0(system.file(package = "R3port"), "/style.css"),
     clean = 0
   )

I get this error: Error in html_combine(combine = getwd(), out = "to.html", toctheme = TRUE, : no raw html files to combine

I have very little experience with HTML and as looking for some guidance on using this package or any other tool to achieve the task. Combining to pdf should work as well (I was able to do this using adobe software but need to find a way through R to be able to automate the script).

thank you


Solution

  • It seems like the function html_combine of the package R3port expects the files to have a rawhtml extension. You can save rawhtml instead of html files like this:

    
    library(tableHTML)
    library(R3port) 
    
    x=data.frame(x=c(1,2,3))
    y=data.frame(y=c(4,5,6))
    
    
    
    tableHTML::write_tableHTML(tableHTML(x), "x.rawhtml") 
    
    tableHTML::write_tableHTML(tableHTML(y), "y.rawhtml")
    
    

    And then use html_combine to get the output:

    html_combine(
      out = "to.html",
      toctheme = TRUE,
      css = paste0(system.file(package = "R3port"), "/style.css"),
      clean = 0
    )
    

    The result is this:

    screenshot of html report