问题
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