繁体   English   中英

R 获取标签列表的自包含 html 文件

[英]R Obtaining a self-contained html file for tag list

我希望有人能帮我解决这个问题。 我有一个使用 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 查看器或浏览器中)时,一切正常,但我还想将此小部件导出到磁盘上指定路径的自包含 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 文件的事实? 提前致谢

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM