gpt4 book ai didi

R Markdown : datable within collapsible section

转载 作者:行者123 更新时间:2023-12-03 22:59:54 26 4
gpt4 key购买 nike

以下是带有 <details> 的 R-markdown 文档标签来创建可折叠的部分。
你能帮我渲染datatable吗?来自 html 输出中的第 2 节?

最小可重现示例

### Section 1
<details> <summary>Click to expand</summary>

```{r, echo=FALSE}
head(iris)
```

</details>


### Section 2
<details> <summary>Click to expand</summary>

```{r, echo=FALSE}
DT::datatable(iris)
```

</details>
enter image description here

最佳答案

这不是一个真正的答案,但有点太长了,不能成为评论,所以我把它包括在这里。希望有人可以用它来得出一个实际的答案:
好消息是它绝对是“可能的”。坏消息是“这并不容易”。由于我对 web-dev 的了解有限,问题似乎是 DT::datatable (或更准确地说 htmlwidgets:::print.html_widget )在一个临时文件中创建一个完整的 html 网页,这是可视化 DT::datatable 的默认方法.文件本身看起来像

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<style>body{background-color:white;}</style>
<script src="lib/htmlwidgets-1.5.3/htmlwidgets.js"></script>
<script src="lib/jquery-1.12.4/jquery.min.js"></script>
<link href="lib/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="lib/datatables-binding-0.16/datatables.js"></script>
<link href="lib/dt-core-1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="lib/dt-core-1.10.20/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="lib/dt-core-1.10.20/js/jquery.dataTables.min.js"></script>
<link href="lib/crosstalk-1.1.0.1/css/crosstalk.css" rel="stylesheet" />
<script src="lib/crosstalk-1.1.0.1/js/crosstalk.min.js"></script>

</head>
<body>
<div id="htmlwidget_container">
<div id="htmlwidget-cd5f37d21433eb2088ae" style="width:960px;height:500px;" class="datatables html-widget"></div>
</div>
<script type="application/json" data-for="htmlwidget-cd5f37d21433eb2088ae">{"x":{"filter":"none","data":[["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119","120","121","122","123","124","125","126","127","128","129","130","131","132","133","134","135","136","137","138","139","140","141","142","143","144","145","146","147","148","149","150"],[5.1,4.9,4.7,4.6,5,5.4,4.6,5,4.4,4.9,5.4,4.8,4.8,4.3,5.8,5.7,5.4,5.1,5.7,5.1,5.4,5.1,4.6,5.1,4.8,5,5,5.2,5.2,4.7,4.8,5.4,5.2,5.5,4.9,5,5.5,4.9,4.4,5.1,5,4.5,4.4,5,5.1,4.8,5.1,4.6,5.3,5,7,6.4,6.9,5.5,6.5,5.7,6.3,4.9,6.6,5.2,5,5.9,6,6.1,5.6,6.7,5.6,5.8,6.2,5.6,5.9,6.1,6.3,6.1,6.4,6.6,6.8,6.7,6,5.7,5.5,5.5,5.8,6,5.4,6,6.7,6.3,5.6,5.5,5.5,6.1,5.8,5,5.6,5.7,5.7,6.2,5.1,5.7,6.3,5.8,7.1,6.3,6.5,7.6,4.9,7.3,6.7,7.2,6.5,6.4,6.8,5.7,5.8,6.4,6.5,7.7,7.7,6,6.9,5.6,7.7,6.3,6.7,7.2,6.2,6.1,6.4,7.2,7.4,7.9,6.4,6.3,6.1,7.7,6.3,6.4,6,6.9,6.7,6.9,5.8,6.8,6.7,6.7,6.3,6.5,6.2,5.9],[3.5,3,3.2,3.1,3.6,3.9,3.4,3.4,2.9,3.1,3.7,3.4,3,3,4,4.4,3.9,3.5,3.8,3.8,3.4,3.7,3.6,3.3,3.4,3,3.4,3.5,3.4,3.2,3.1,3.4,4.1,4.2,3.1,3.2,3.5,3.6,3,3.4,3.5,2.3,3.2,3.5,3.8,3,3.8,3.2,3.7,3.3,3.2,3.2,3.1,2.3,2.8,2.8,3.3,2.4,2.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0.4,0.1,0.2,0.2,0.2,0.2,0.1,0.2,0.2,0.3,0.3,0.2,0.6,0.4,0.3,0.2,0.2,0.2,0.2,1.4,1.5,1.5,1.3,1.5,1.3,1.6,1,1.3,1.4,1,1.5,1,1.4,1.3,1.4,1.5,1,1.5,1.1,1.8,1.3,1.5,1.2,1.3,1.4,1.4,1.7,1.5,1,1.1,1,1.2,1.6,1.5,1.6,1.5,1.3,1.3,1.3,1.2,1.4,1.2,1,1.3,1.2,1.3,1.3,1.1,1.3,2.5,1.9,2.1,1.8,2.2,2.1,1.7,1.8,1.8,2.5,2,1.9,2.1,2,2.4,2.3,1.8,2.2,2.3,1.5,2.3,2,2,1.8,2.1,1.8,1.8,1.8,2.1,1.6,1.9,2,2.2,1.5,1.4,2.3,2.4,1.8,1.8,2.1,2.4,2.3,1.9,2.3,2.5,2.3,1.9,2,2.3,1.8],["setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","setosa","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","versicolor","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica","virginica"]],"container":"<table class=\"display\">\n <thead>\n <tr>\n <th> <\/th>\n <th>Sepal.Length<\/th>\n <th>Sepal.Width<\/th>\n <th>Petal.Length<\/th>\n <th>Petal.Width<\/th>\n <th>Species<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"columnDefs":[{"className":"dt-right","targets":[1,2,3,4]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false}},"evals":[],"jsHooks":[]}</script>
<script type="application/htmlwidget-sizing" data-for="htmlwidget-cd5f37d21433eb2088ae">{"viewer":{"width":450,"height":350,"padding":15,"fill":true},"browser":{"width":960,"height":500,"padding":40,"fill":false}}</script>
</body>
</html>
数据和标题显然会根据数据而变化。
现在,如果我们在页面处于事件状态时检查和编辑页面
  • 在 Rstudio 中调用 DT::datatable(iris)显示表格
  • 单击“在新窗口中显示”以在您的首选浏览器中打开它
  • 右键单击页面上的任意位置,然后单击“检查元素 (Q)”
  • 右键单击“正文”并单击“编辑为 HTML”
  • 最后添加<details>/</details> <body> 的开头和结尾

  • 然后我们实际上可以看到它像我们预期的那样工作(先关闭):
    datatable-closed
    (现在开门了):
    datatable-open
    所以这是一个明确的结论,“有可能”。问题是提取代码。走进 DT::datatable你最终会发现它调用了 htmlwidgets:::print.html_widget打开实际的 html 页面。这让我们可以重新创建一个脚本并提取小部件中使用的实际 html 代码:
    #' Generate html and make dependencies available in directory for a DT::datatable
    #'
    #' @param x a data.frame or DT::datatable
    #' @param dir the (root) directory for the project/dependencies. See details
    #' @param background background for the html widget
    #' @param libdir directory to export dependencies to
    #'
    #' @details This function generates the html that is usually generated when
    #' printing DT::datatable, and exports dependencies to a given directory, making
    #' it useful for embedding the html into a markdown file or shiny script, either
    #' running and saving this in the pre-amble/header or interactively. "Dir"
    #' can be used to specify the project root, with "libdir" specifying the path
    #' relative from "dir" to place dependencies. Note that this likely enforces
    #' the html file to be placed in the project root, and not a sub-folder of the
    #' project.
    datatable_html <- function(x, dir = getwd(), background = "white", libdir = 'lib'){
    if(is.data.frame(x))
    x <- DT::datatable(x)
    #from htmlwidgets:::print.html_widgets
    x <- htmltools::as.tags(x, standalone = TRUE)
    #from htmltools::save_html (called by print.html_widgets
    x <- htmltools::renderTags(x)
    deps <- lapply(x$dependencies, function(dep) {
    dep <- htmltools::copyDependencyToDir(dep,
    libdir,
    FALSE)
    dep <- htmltools::makeDependencyRelative(dep, dir, FALSE)
    dep
    })
    bodyBegin <- if (!isTRUE(grepl("<body\\b", x$html[1],
    ignore.case = TRUE))) {
    "<body>"
    }
    bodyEnd <- if (!is.null(bodyBegin)) {
    "</body>"
    }
    c("<!DOCTYPE html>", "<html>", "<head>",
    "<meta charset=\"utf-8\"/>", sprintf("<style>body{background-color:%s;}</style>",
    htmltools::htmlEscape(background)),
    htmltools::renderDependencies(deps, c("href", "file")), x$head, "</head>",
    bodyBegin, # <=== body starts here, maybe remove?
    x$html,
    bodyEnd, # <=== Body ends here, maybe remove?
    "</html>")
    }

    dt_html <- datatable_html(DT::datatable(iris))
    # print (very large output):
    cat(dt_html)
    现在 dt_html包含向量中的 html 段,并将依赖项复制到 {dir}/{libdir}它应该是 Markdown 项目根目录下的文件夹。需要注意的几点: html 向量在 dt_html[6] 中具有依赖关系。 (可能必须包含在 Markdown 序言中?)并且 htmlscript 本身位于 dt_html[10]带有 dt_html[9] 中的正文标签和 dt_html[11]分别。
    我对在 Rmarkdown 中嵌入 html 的技术不够熟练,无法确定从这里去哪里,但我确信那里会有一些有能力滥用它并提供答案的最后部分的 efficionados。我假设 document dependencies 的组合对于标题,然后使用 dt_html 中的 html 段应该以某种方式完成这项工作。

    关于R Markdown : datable within collapsible section,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66670299/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com