簡體   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