0

我希望有人可以帮助我解决这个问题。我有一个包,它使用 htmltools 和 reactable 为某些操作生成 html 报告,例如:

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)

在这种情况下,我使用 htmlwidget 中的 prependContent 函数,因为 reactable 是一个小部件。当我打印此小部件(在 RStudio Viewer 或浏览器中)时,一切正常,但我还想将此小部件导出到磁盘上指定路径的自包含 html 文件中。所以在我的功能代码中我这样做:

htmlwidgets::saveWidget(widg, export_widget_path)

默认情况下,从文档中,selfcontained 参数设置为 TRUE,并且我已正确安装 pandoc,但会发生这种情况:

在此处输入图像描述

即使我选择自包含选项,也会生成一个文件夹,并且当我打开文件时,它的一部分会被错误地呈现:

在此处输入图像描述

打印小部件时不会发生这种情况(在查看器或浏览器中)

在此处输入图像描述

我也试图改变这个

 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)

有了这个

 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
    )
)

获得一个 tag.shiny 对象,但它当然不适用于 htmlwidgets::saveWidget,我必须使用 htmltools::save_html ,它不会产生一个独立的文件。

我知道 pandoc 有一个选项可以将 html 转换为自包含的,但是当我尝试使用它时它也会产生奇怪的结果(主要是图形未正确呈现)。

有什么办法可以做到这一点,还是我必须屈服于我将拥有非独立的 html 文件的事实?提前致谢

4

2 回答 2

0

您需要使用 htmlwidgets::saveWidget(frameableWidget(mapdt3),'map.html') frameableWidget 将来自 widgetframe 它将 html 文件保存为自包含文件。

于 2021-06-03T03:41:14.203 回答
0

您是否尝试将工作目录设置为要保存自包含文件的位置?这是我能够使用htmlwidgets::saveWidget().

于 2021-05-06T17:20:47.390 回答