我发现了一个有趣的包rpivotTable。我想创建包含rpivotTable的shiny app,可以使用downloadHandler下载生成的数据。
但是,我找不到解决方案,也找不到如何创建data.frame或其他我可以传递给downloadHandler函数的东西。
rpivotTable创建一个类的对象:
class(pivot)
[1] "rpivotTable" "htmlwidget" 是否有可能下载此函数的输出?
此外,我还附上了示例,如何在shiny中创建pivot,以及我想使用的下载函数的示例。
也许还有其他的想法或建议?
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)
})发布于 2015-10-27 01:48:44
我刚刚在github上推送了rpivotTable的主分支,这一变化解决了获取用户在服务器端查看的参数的问题。
使用devtools下载rpivotTable代码
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) 发布于 2016-09-06 10:45:07
为了扩展Enzo的出色回答(感谢您提供的出色的包),我模拟了以下内容,以此作为获取汇总数据并在shiny中使用的一种方法。
它使用onRefresh监视配置中的更改,然后使用DOM获取相关元素的innerHTML。在本例中,然后使用rvest清理html并提取表,最后出于演示目的,在DT::datatable中显示它。
这可能太老生常谈了,但它可以直接作为CSV下载,或者传递给其他闪亮的元素进行进一步处理。
ui.R
library(shiny)
library(DT)
library(rpivotTable)
FullPage <- fluidPage(
DT::dataTableOutput('aSummaryTable'),
rpivotTableOutput('RESULTS')
)
FullPageserver.R:
library(shiny)
library(rpivotTable)
library(DT)
library(rvest)
function(input, output, session) {
# Make some sample data
qbdata <- reactive({
expand.grid(LETTERS,1:3)
})
# Clean the html and store as reactive
summarydf <- eventReactive(input$myData,{
input$myData %>%
read_html %>%
html_table(fill = TRUE) %>%
# Turns out there are two tables in an rpivotTable, we want the second
.[[2]]
})
# show df as DT::datatable
output$aSummaryTable <- DT::renderDataTable({
datatable(summarydf(), rownames = FALSE)
})
# Whenever the config is refreshed, call back with the content of the table
output$RESULTS <- renderRpivotTable({
rpivotTable(
qbdata(),
onRefresh =
htmlwidgets::JS("function(config) {
Shiny.onInputChange('myData', document.getElementById('RESULTS').innerHTML);
}")
)
})
}发布于 2019-04-19 22:38:19
在github存储库rpivotTabletocsv中,我尝试从Rshiny App的下载按钮实现rpivotTable到csv的导出。
https://stackoverflow.com/questions/33214397
复制相似问题