下载rpivotTable输出有光泽

问题描述 投票:5回答:3

我找到了一个有趣的包rpivotTable。我想创建包含shiny apprpivotTable,可以使用downloadHandler下载生成的数据。

但是,我无法找到解决方案,如何创建data.frame或其他我能够传递给downloadHandler函数的东西。

rpivotTable创建了一个类对象:

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

是否有任何可能下载此功能的输出?

另外,我附上了这个例子,如何在光泽中创建数据透视图以及我想要使用的下载功能示例。

也许是其他任何想法或建议?

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)
})
r download shiny pivot-table rpivottable
3个回答
4
投票

我刚刚在github上推动了rpivotTable的主分支,这个更改解决了获取用户在服务器端看到的参数的问题。

使用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) 

2
投票

为了扩展Enzo的优秀答案(感谢您提供了很棒的解决方案),我将以下内容模拟为获取汇总数据并在闪亮内部使用它的方法。

这使用onRefresh来监视配置中的更改,然后使用DOM来获取相关元素的innerHTML。在这种情况下,然后使用rvest清理该html并提取表格,最后,为了演示目的,在DT::datatable中显示它。

这可能太过于hacky,但它可以直接下载为CSV,然后传递给其他有光泽的元素进行进一步处理。

长子。 [R

library(shiny)
library(DT)
library(rpivotTable)

FullPage <- fluidPage(
    DT::dataTableOutput('aSummaryTable'),
    rpivotTableOutput('RESULTS')
)

FullPage

server.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); 
                        }")
    )
  })

}

0
投票

一个github存储库rpivotTabletocsv我尝试从Rshiny App的下载按钮实现将rpivotTable导出到csv。

© www.soinside.com 2019 - 2024. All rights reserved.