闪亮:将 rpivotTable 输出保存为 Excel

shiny: Saving rpivotTableOutput as Excel

我不知道如何保存 in 的输出。下面是我的最小工作示例以供参考。

library(tidyverse)
library(shiny)
library(shinydashboard)

ui <-
  dashboardPage(
    skin = "green",
    dashboardHeader(
      title      = "Test",
      titleWidth = 280
      ),
    dashboardSidebar(
      width = 280,
      sidebarMenu(
        menuItem(text = "Output", tabName = "Out1",     icon = icon("file-upload"))
      )
    ),
    dashboardBody(
      tabItems(
        tabItem(
                 tabName = "Out1",
                 fluidRow(column(width = 10, strong("Data")), align = "center"),
                 br(),
                 fluidRow(rpivotTableOutput("Data1"))
                 ) 
      )
    )
  )

  
server <- 
  function(input, output){
    library(rpivotTable)
      
    output$Data1 <- 
      renderRpivotTable(
        rpivotTable(
            data = mtcars
          , rows = "cyl"
          , cols = "gear"
          , height = "780px"
          )
        )
          }

runApp(
    list(ui = ui, server = server)
  , launch.browser = TRUE
  ) 

Edit/Update:在rvestxlsx包的帮助下,我们可以将提取的html pivot table保存为xls 文件。更新了下面的代码

我同意@danlooo 的评论,即尝试从 rpivotTable 的渲染中提取数据可能不是最好的前进方式。但是,我可以提供一种方法来提取生成的枢轴 table.

的 HTML 代码

下面的代码提取生成的枢轴 table 的 html 代码,使用 rvest 从这个 html 中提取一个 dataframe,最后使用 xlsx出口。 可以找到更多关于 shiny 和 js 交互的信息 in this article

代码生成一个 xls 文件,其中包含由 rpivotTable

生成的数据透视表 table
library(tidyverse)
library(shiny)
library(shinydashboard)
library(rpivotTable)

library(shinyjs)
library(dplyr)
library(rvest)
library(xlsx)

ui <-
  dashboardPage(
    skin = "green",
    
    dashboardHeader(
      title      = "Test",
      titleWidth = 280
    ),
    
    dashboardSidebar(
      width = 280,
      sidebarMenu(
        menuItem(text = "Output", tabName = "Out1")
      ),
      
      hr(),
      useShinyjs(),
      actionButton(inputId = "btnExport", "Export Table")
      
      
    ),
    dashboardBody(
      tabItems(
        tabItem(
          tabName = "Out1",
          fluidRow(column(width = 10, strong("Data")), align = "center"),
          br(),
          fluidRow(rpivotTableOutput("Data1"))
        ) 
      )
    )
  )


server <- 
  function(input, output){
    #library(rpivotTable)
    
    output$Data1 <- 
      renderRpivotTable(
        rpivotTable(
          data = mtcars
          , rows = "cyl"
          , cols = "gear"
          , height = "780px"
        )
      )
    
    
    observeEvent(input$btnExport,{
      
      runjs(
        "
        var tblhtml=document.getElementsByClassName('pvtRendererArea')[0].innerHTML;
        console.log(tblhtml)
        //set shiny Input value to read reactively from R 
        Shiny.setInputValue('tblvar_shiny', tblhtml);
        "
      )
      
    } )
    
    #save pivot table to html file
    # observeEvent(input$tblvar_shiny, 
    #              { write_file(sprintf("<html><body> %s </body></html>", 
    #                                   input$tblvar_shiny), 
    #                           file = "000pivothtml.html" )})
    
    #save pivot table to xls file 
    observeEvent(input$tblvar_shiny,
                 { 
                     minimal_html(input$tblvar_shiny) %>% 
                     html_element("table")   %>% 
                     html_table() %>% 
                     as.data.frame() %>% 
                     write.xlsx2(file="000pivot_final.xls")
                   })
  } 

runApp(
  list(ui = ui, server = server)  , launch.browser = TRUE
)