如何在下载的包含闪亮虹膜数据的 word 文档文件中显示边框?

How to display borders in a downloaded word doc file that contains iris data in shiny?

我想创建一个闪亮的应用程序来下载 word 文件中的一些数据。它应该有一个适当的 table 格式,带有像 this 这样的边框。这与我使用的 table 不同,但我的意思是它应该有 borders 。我创建了以下闪亮的应用程序,它使用 iris 数据集。它显示数据集并下载它。但是下载的文件没有边框。它看起来像这样:

以下是我使用的代码:-

library(shiny)

ui<-shinyUI(fluidPage(
  (sidebarLayout( sidebarPanel( downloadButton('downloaddata','download table')),#this is the download button
                                                                              
mainPanel(

tableOutput ('table')#this outputs the table
)
  )
  
))


server<- shinyServer(
  
  function(input, output) {
    #1 Dataset l
    l<- reactive({
      iris})#store iris dataset in a reactive variable


    output$table<-renderTable({
      l()})#used to display the iris dataset
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    output$downloaddata<-
      downloadHandler(filename=function()
      {
        paste('Item statistics','doc',sep='.') #uses doc as extension
      }
      ,content=function(file)
      {
        
        (write.table(data.frame(l()),file))
      })                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     })
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    } )
    
    shinyApp(ui,server)

我们可以按照@stefan 的建议使用officer

library(shiny)
library(officer)
library(flextable)
library(magrittr)

ui <- fluidPage(
        sidebarLayout(sidebarPanel( downloadButton('downloaddata','download table')),
        mainPanel(tableOutput('table')))
)

server <- function(input, output) {
  
  l <- reactive({iris})
  
  output$table <- renderTable({l()})
  
  output$downloaddata <- downloadHandler(filename=function() {
      paste('Item statistics','docx',sep='.') #uses doc as extension
    },
    content=function(file) {
      
      ft <- flextable(l()) %>%
        border_outer() %>% 
        border_inner()
      
      doc <- read_docx() %>%
        body_add_flextable(ft)
      
      print(doc, target = file)
    })                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
} 

shinyApp(ui,server)