在 rpivotTable 中只有 Table
Only Table in rpivotTable
我在 Shiny 应用程序中使用 rpivotTable 包,我只想为用户选择 'Table'(无图表)
RenderName 参数仅用于选择默认显示...
output$pivot <- renderRpivotTable(
rpivotTable(iris,
rendererName = "Table" )
)
非常感谢!
这里有多个问题。
- 您可以通过
rpivotTable()
中的匿名 renderers
参数指定渲染器。我有 JS 代码形式 here.
- 但是,只选择一个选项时会出现错误。在这种情况下,
rpivotTable()
再次将参数包装在列表中(参见原始函数代码中的 Map()
调用)并且转发到 JS 失败。
因此,我考虑到了这个问题,并扩展了一些功能。试用 aggregators/renderers 看看它与原始 rpivotTable()
函数有何不同。
# define own function
my_rpivotTable <- function (data, rows = NULL, cols = NULL, aggregatorName = NULL,
vals = NULL, rendererName = NULL, sorter = NULL, exclusions = NULL,
inclusions = NULL, locale = "en", subtotals = FALSE, ...,
width = 800, height = 600, elementId = NULL)
{
if (length(intersect(class(data), c("data.frame", "data.table",
"table", "structable", "ftable"))) == 0) {
stop("data should be a data.frame, data.table, or table",
call. = F)
}
if (length(intersect(c("table", "structable", "ftable"),
class(data))) > 0)
data <- as.data.frame(data)
params <- list(rows = rows, cols = cols, aggregatorName = aggregatorName,
vals = vals, rendererName = rendererName, sorter = sorter,
...)
params <- Map(function(p) {
# added to the class check -------------------------------------------------
if (length(p) == 1 && class(p[[1]]) != "JS_EVAL") {
p = list(p)
}
return(p)
}, params)
par <- list(exclusions = exclusions, inclusions = inclusions)
params <- c(params, par)
params <- Filter(Negate(is.null), params)
x <- list(data = data, params = params, locale = locale,
subtotals = subtotals)
htmlwidgets::createWidget(name = "rpivotTable", x, width = width,
height = height, elementId = elementId, package = "rpivotTable")
}
# create the pivot table
my_rpivotTable(
expand.grid(LETTERS, 1:3),
aggregatorName = "Count",
aggregators = list(Sum = htmlwidgets::JS('$.pivotUtilities.aggregators["Sum"]'),
Count = htmlwidgets::JS('$.pivotUtilities.aggregators["Count"]')),
rendererName = "fancyTable",
renderers = list(fancyTable = htmlwidgets::JS('$.pivotUtilities.renderers["Table"]'))
)
我在 Shiny 应用程序中使用 rpivotTable 包,我只想为用户选择 'Table'(无图表)
RenderName 参数仅用于选择默认显示...
output$pivot <- renderRpivotTable(
rpivotTable(iris,
rendererName = "Table" )
)
非常感谢!
这里有多个问题。
- 您可以通过
rpivotTable()
中的匿名renderers
参数指定渲染器。我有 JS 代码形式 here. - 但是,只选择一个选项时会出现错误。在这种情况下,
rpivotTable()
再次将参数包装在列表中(参见原始函数代码中的Map()
调用)并且转发到 JS 失败。
因此,我考虑到了这个问题,并扩展了一些功能。试用 aggregators/renderers 看看它与原始 rpivotTable()
函数有何不同。
# define own function
my_rpivotTable <- function (data, rows = NULL, cols = NULL, aggregatorName = NULL,
vals = NULL, rendererName = NULL, sorter = NULL, exclusions = NULL,
inclusions = NULL, locale = "en", subtotals = FALSE, ...,
width = 800, height = 600, elementId = NULL)
{
if (length(intersect(class(data), c("data.frame", "data.table",
"table", "structable", "ftable"))) == 0) {
stop("data should be a data.frame, data.table, or table",
call. = F)
}
if (length(intersect(c("table", "structable", "ftable"),
class(data))) > 0)
data <- as.data.frame(data)
params <- list(rows = rows, cols = cols, aggregatorName = aggregatorName,
vals = vals, rendererName = rendererName, sorter = sorter,
...)
params <- Map(function(p) {
# added to the class check -------------------------------------------------
if (length(p) == 1 && class(p[[1]]) != "JS_EVAL") {
p = list(p)
}
return(p)
}, params)
par <- list(exclusions = exclusions, inclusions = inclusions)
params <- c(params, par)
params <- Filter(Negate(is.null), params)
x <- list(data = data, params = params, locale = locale,
subtotals = subtotals)
htmlwidgets::createWidget(name = "rpivotTable", x, width = width,
height = height, elementId = elementId, package = "rpivotTable")
}
# create the pivot table
my_rpivotTable(
expand.grid(LETTERS, 1:3),
aggregatorName = "Count",
aggregators = list(Sum = htmlwidgets::JS('$.pivotUtilities.aggregators["Sum"]'),
Count = htmlwidgets::JS('$.pivotUtilities.aggregators["Count"]')),
rendererName = "fancyTable",
renderers = list(fancyTable = htmlwidgets::JS('$.pivotUtilities.renderers["Table"]'))
)