gpt4 book ai didi

r - 以 Shiny 的形式下载 rpivotTable 输出

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

我发现了一个有趣的包 rpivotTable .
我想创建 shiny app其中包括 rpivotTable可以使用 downloadHandler 下载生成的数据.

但是,我找不到解决方案,如何创建data.frame或其他我可以传递给 downloadHandler 的东西功能。
rpivotTable创建一个类的对象:

class(pivot)
[1] "rpivotTable" "htmlwidget"

有没有可能下载这个函数的输出?

此外,我附上了示例,如何在 Shiny 中创建枢轴以及我想使用的下载功能示例。

也许还有其他想法或建议?
set.seed(1992)
n=99
Year <- sample(2013:2015, n, replace = TRUE, prob = NULL)
Month <- sample(1:12, n, replace = TRUE, prob = NULL)
Category <- sample(c("Car", "Bus", "Bike"), n, replace = TRUE, prob = NULL)
Brand <- sample("Brand", n, replace = TRUE, prob = NULL)
Brand <- paste0(Brand, sample(1:14, n, replace = TRUE, prob = NULL))
USD <- abs(rnorm(n))*100

df <- data.frame(Year, Month, Category, Brand, USD)



output$Pivot <- rpivotTable::renderRpivotTable({
rpivotTable(data = df, rows = "Brand", col = "Category", vals = "USD", aggregatorName = "Sum", rendererName = "Table")
})



output$downloadData <- downloadHandler(
filename = function() { paste(filename, '.csv', sep='') },
content = function(file) {
write.csv(PivotOutput, file)
})

最佳答案

我刚刚在 github 上的 rpivotTable 的 master 分支上推送了一项更改,该更改解决了获取用户在服务器端查看/查看的参数的问题。

下载 rpivotTable代码与 devtools :

devtools::install_github("smartinsightsfromdata/rpivotTable",ref="master")

这是如何在服务器端获取所选数据的示例。该示例不完全满足您的需求:您需要使用从 rpivotTable 返回的内容对原始数据框进行子集化。但这应该足以让您领先一步。
library(rpivotTable)
library(shiny)

list_to_string <- function(obj, listname) {
if (is.null(names(obj))) {
paste(listname, "[[", seq_along(obj), "]] = ", obj,
sep = "", collapse = "\n")
} else {
paste(listname, "$", names(obj), " = ", obj,
sep = "", collapse = "\n")
}
}

server <- function(input, output) {

output$pivotRefresh <- renderText({

cnames <- list("cols","rows","vals", "exclusions","aggregatorName", "rendererName")
# Apply a function to all keys, to get corresponding values
allvalues <- lapply(cnames, function(name) {
item <- input$myPivotData[[name]]
if (is.list(item)) {
list_to_string(item, name)
} else {
paste(name, item, sep=" = ")
}
})
paste(allvalues, collapse = "\n")
})

output$mypivot = renderRpivotTable({
rpivotTable(data=cars, onRefresh=htmlwidgets::JS("function(config) { Shiny.onInputChange('myPivotData', config); }"))
})
}

ui <- shinyUI(fluidPage(
fluidRow(column(6, verbatimTextOutput("pivotRefresh")),
column(6, rpivotTableOutput("mypivot") ))
)
)

shinyApp(ui = ui, server = server)

关于r - 以 Shiny 的形式下载 rpivotTable 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33214397/

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