End*_*dre 5 javascript r shiny rhandsontable
我需要将一些参数传递给自定义rhandsontable渲染器中。它在 RStudio 中执行时工作正常,但在 Shiny 应用程序中使用时不会渲染任何内容。
以下是设置粗体字体的自定义渲染器的代码:
renderSheet <- function(df, bold) {
rhandsontable(
df,
col_bold = bold$col,
row_bold = bold$row) %>%
hot_cols(renderer = "
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
tbl = this.HTMLWidgets.widgets[0]
col_bold = tbl.params.col_bold
col_bold = col_bold instanceof Array ? col_bold : [col_bold]
row_bold = tbl.params.row_bold
row_bold = row_bold instanceof Array ? row_bold : [row_bold]
if (col_bold.includes(col) && row_bold.includes(row)) {
td.style.fontWeight = 'bold';
}
return td;
}"
) }
Run Code Online (Sandbox Code Playgroud)
以下是在 RStudio 中运行它的方法:
df = data.frame(a = c("a1", "a2"), b = c("b1", "b2"))
bold <- data.frame(row = c(1, 1), col = c(0, 1))
renderSheet(df, bold)
Run Code Online (Sandbox Code Playgroud)
这是一个最小的 Shiny 应用程序,用于演示它不会渲染:
library(rhandsontable)
library(shiny)
df = data.frame(a = c("a1", "a2"), b = c("b1", "b2"))
bold <- data.frame(row = c(1, 1), col = c(0, 1))
ui <- shinyUI(fluidPage(
rHandsontableOutput("hot")
))
server <- shinyServer(function(input, output, session) {
output$hot = renderRHandsontable({
renderSheet(df, bold)
})
})
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
在闪亮的应用程序中引起问题的行是tbl = this.HTMLWidgets.widgets[0],我不知道如何修复它。
是否有其他方法可以将参数传递到自定义渲染器中?
注意:rhandsontable 的帮助页面指出,在 Shiny 应用程序中我们必须小心,但我无法使用帮助页面中提供的代码片段 (jrowen.github.io/rhandsontable/#custom_renderer)
HTMLWidgets.widgets.filter(function(widget) {
// this should match the table id specified in the shiny app
return widget.name === "hot"
})[0];
Run Code Online (Sandbox Code Playgroud)
我们如何将上面的代码片段应用于我的问题?
| 归档时间: |
|
| 查看次数: |
639 次 |
| 最近记录: |