R Obtaining a self-contained html file for tag list

771 Views Asked by At

I hope somebody can help me with this. I have a package that uses htmltools and reactable to produce html reports for some operations, for example:

columns_def <- list(
    ProjectID = reactable::colDef(
        align = "right",
        style = list(
            color = "#9e9e9e",
            fontWeight = "800",
            borderRight = "2px solid #E6E6E6"
        ),
        minWidth = 60
    ),
    concatenatePoolIDSeqRun = reactable::colDef(
        minWidth = 100
    ),
    Found = reactable::colDef(
        maxWidth = 100,
        align = "center",
        style = function(value) {
            color <- if (value == TRUE) {
                "#6afc21"
            } else {
                "#d61e1e"
            }
            list(
                color = color, paddingLeft = "15px",
                fontWeight = "bold"
            )
        },
        cell = function(value) {
            if (value == TRUE) "\u2713" else "\u2718"
        }
    ),
    Path = reactable::colDef(
        minWidth = 200
    )
)

styled_df <- .generate_react_table(checker_df,
    defaultSorted = list(Found = "asc"),
    columns = columns_def
)
 widget_text <- htmltools::tags$html(
    htmltools::tags$head(
        htmltools::tags$style(.widget_css())
    ),
    htmltools::tags$body(
        htmltools::h1("IMPORT ASSOCIATION FILE REPORT"),
        htmltools::h2("ALIGNMENT RESULTS"),
        htmltools::div(
            id = "section-content",
            htmltools::div("Results of alignment between file system and",
                "association file. If some folders are not found",
                "they will be ignored until the problem is fixed",
                "and the association file re-imported.",
                id = "subtitle"
            )
        )
    )
)
widget <- htmlwidgets::prependContent(styled_df, widget_text)

In this case i'm using the prependContent function from htmlwidget since reactable is a widget. When I print this widget (either in RStudio Viewer or the browser) everything works fine, but I'd also like to export this widget in a self contained html file on disk at a specified path. So in my function code I do:

htmlwidgets::saveWidget(widg, export_widget_path)

From documentation by default the selfcontained parameter is set to TRUE and I have pandoc installed correctly but this happens:

enter image description here

Even if I opt for self contained options a files folder is produced and when I open the file a portion of it is wrongly rendered:

enter image description here

This doesn't happen when the widget is printed (either in viewer or browser)

enter image description here

I also tried to change this

 widget_text <- htmltools::tags$html(
    htmltools::tags$head(
        htmltools::tags$style(.widget_css())
    ),
    htmltools::tags$body(
        htmltools::h1("IMPORT ASSOCIATION FILE REPORT"),
        htmltools::h2("ALIGNMENT RESULTS"),
        htmltools::div(
            id = "section-content",
            htmltools::div("Results of alignment between file system and",
                "association file. If some folders are not found",
                "they will be ignored until the problem is fixed",
                "and the association file re-imported.",
                id = "subtitle"
            )
        )
    )
)
widget <- htmlwidgets::prependContent(styled_df, widget_text)

with this

 widget <- htmltools::tags$html(
    htmltools::tags$head(
        htmltools::tags$style(.widget_css())
    ),
    htmltools::tags$body(
        htmltools::h1("IMPORT ASSOCIATION FILE REPORT"),
        htmltools::h2("ALIGNMENT RESULTS"),
        htmltools::div(
            id = "section-content",
            htmltools::div("Results of alignment between file system and",
                "association file. If some folders are not found",
                "they will be ignored until the problem is fixed",
                "and the association file re-imported.",
                id = "subtitle"
            )
        ), styled_df
    )
)

Obtaining a tag.shiny object but of course it doesn't work with htmlwidgets::saveWidget, I have to use htmltools::save_html which doesn't produce a self-contained file.

I know there is an option with pandoc to convert html to self contained but it also produces weird results when I tried to use it (mainly graphics not rendered correctly).

Is there any way this could be done or do I have to surrender to the fact that I'll have non self-contained html files? Thanks in advance

2

There are 2 best solutions below

0
On

Have you tried setting your working directory to the location that you want to save the self-contained file? That is the only way that I am able to make self-contained files with htmlwidgets::saveWidget().

0
On

You need to use htmlwidgets::saveWidget(frameableWidget(mapdt3),'map.html') frameableWidget will be from widgetframe It will save the html file as self contained file.