download rpivotTable output in shiny

后端 未结 3 1650
萌比男神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 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); 
                            }")
        )
      })
    
    }
    

提交回复
热议问题