download rpivotTable output in shiny

后端 未结 3 1618
萌比男神i
萌比男神i 2021-01-02 21:21

I\'ve found an interesting package rpivotTable. I\'d like to create shiny app which includes rpivotTable with the possibility to downl

相关标签:
3条回答
  • 2021-01-02 21:52

    A the github repository rpivotTabletocsv I try to implement the export of rpivotTable to csv from a download button from Rshiny App.

    0 讨论(0)
  • 2021-01-02 22:01

    I've just pushed on the master branch of rpivotTable on github a change that addresses the issue of getting the parameters the user is / has looked at on the server side.

    Download the rpivotTable code with devtools:

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

    This is an example of how to get the selected data on the server side. The example is not complete for your needs: you need to subset the original data frame with what you get back from rpivotTable. But this should be enough to give you an head start.

    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) 
    
    0 讨论(0)
  • 2021-01-02 22:14

    To extend Enzo's excellent answer (Thank you for the awesome package), I mocked up the following as a way to get the summarized data and use it inside shiny.

    This uses the onRefresh to watch for changes in the config, then uses the DOM to get the innerHTML of the relevant element. In this case, then uses rvest to clean that html and extract the table, and finally, for demo purposes, shows it inside a DT::datatable.

    This might be too hacky, but it can be straightforwardly downloaded as a CSV then, or passed to other shiny elements for further processing.

    ui.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 讨论(0)
提交回复
热议问题