Merge 1000 HTML files in R

老子叫甜甜 提交于 2021-01-28 06:08:48

问题


I have more than 1000 html files and I want to merge them together. I have provided the code below how I am doing it. But it's not getting merge and I am getting blank html file(merged).

saveWidget(f1, "f1.html")
saveWidget(f2, "f2.html")
saveWidget(f3, "f3.html")
saveWidget(f4, "f4.html") and so on till saveWidget(f1000, "f1000.html")

Code to merge files:

    library(htmlwidgets)
    htmlFiles<-list.files("/path/")
    library(htmltools)
    widgets <- list(htmlFiles)
    #fns <- replicate(length(widgets), tempfile(pattern = "widget_", fileext = #".html"))
    #Map(htmlwidgets::saveWidget, widgets, fns)

iframes <- lapply(widgets, function(fn) 
  tags$iframe(
    src = paste0("/path/", fn), 
    style="display:block", 
    height="300", width="1200"
  )  
)
tags$html(
  tags$body(
    iframes
  )
) %>% 
  save_html(tf<<-tempfile(fileext = ".html"))
shell.exec(tf)

Thanks in advance!


回答1:


You can do

library(htmltools)
widgets <- list(DT::datatable(mtcars), DT::datatable(USArrests))
fns <- replicate(length(widgets), tempfile(pattern = "widget_", fileext = ".html"))
Map(htmlwidgets::saveWidget, widgets, fns)

iframes <- lapply(fns, function(fn) 
  tags$iframe(
    src = paste0("file:///", fn), 
    style="display:block", 
    height="300", width="1200"
  )  
)
tags$html(
  tags$body(
    iframes
  )
) %>% 
  save_html(tf<<-tempfile(fileext = ".html"))
shell.exec(tf)



来源:https://stackoverflow.com/questions/50762229/merge-1000-html-files-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!