问题
htmlwidgets.org 页面上自定义小部件 HTML的最后一部分说,您可以通过创建一个名为的函数将自己的 html 添加到小部件<widget_name>_html
你如何将变量传递给这个函数?
例子
我创建了一个名为myWidgetHTML的新包,然后运行htmlwidgets::scaffoldWidget(name = "myHTML”)
. 该软件包托管在我的github 页面上
在生成的R/myHTML.R
文件中,我创建了函数
#' @import htmltools
myHTML_html <- function(id, class, style, ...){
myString <- "hello world"
tags$p(myString)
}
hello world
构建和运行小部件按预期生成字符串
library(myWidgetHTML)
myHTML(message = "")
我想不通的是如何编写函数以便我可以将变量传递给myString
它:
#' @import htmltools
#' @export
myHTML_html <- function(id, class, style, myString, ...){
tags$p(myString)
}
浏览小部件的源代码并没有发现任何问题,因为相关部分(复制如下)表明它通过 接受进一步的参数...
,但我看不到从哪里发送它们。
widget_html <- function(name, package, id, style, class, inline = FALSE, ...){
# attempt to lookup custom html function for widget
fn <- tryCatch(get(paste0(name, "_html"),
asNamespace(package),
inherits = FALSE),
error = function(e) NULL)
# call the custom function if we have one, otherwise create a div
if (is.function(fn)) {
fn(id = id, style = style, class = class, ...)
} else if (inline) {
tags$span(id = id, style = style, class = class)
} else {
tags$div(id = id, style = style, class = class)
}
}