如何让闪亮的应用程序将 git 输出保存为用户浏览器上的 .RTF 文件?

How to have shiny app save gt ouput as an .RFT file on the users browser?

我有这个简单闪亮的应用程序,它接受用户的输入并将其传递给 gt table。 我还有两个按钮可以将 table 保存为 pngrtf

png 按预期工作并通过用户的浏览器下载 table。

我的问题是 rtf 按钮。从技术上讲它有效,但它会将它保存到我闪亮的应用程序目录中,我希望通过用户浏览器下载 .rtf 文件,如 png 按钮。

在这里你可以看到它保存了 .rtf 文件到我闪亮的应用程序目录

代码

library(data.table)
library(shiny)
library(gt)
library(shinyscreenshot)

data <- datasets::mtcars 
setDT(data, keep.rownames = TRUE)[]


ui <- navbarPage("Save this to RTF",
                 tabPanel("Table", icon = icon("table"),
                          
                          sidebarLayout(
                            sidebarPanel(
                              selectInput("input",
                                          label = "Choose # of cyl",
                                          choices = c("All", data$cyl)),
                              screenshotButton(selector="#table", label = 'Download Png', filename = 'screenshot'),
                              actionButton('downloadData', 'Download RTF')
                            ),
                            
                            mainPanel(
                              gt_output("table")
                            )
                          )
                 )
)



server <- function(input, output, session) {
  
reactive_tab  <- reactive({
    d <- data
      if(input$input != "All")
        d <- subset(d, cyl == input$input)
      d 
  })

output$table <- render_gt( 
  reactive_tab() %>% gt()
  )


  observeEvent(input$downloadData,{
    gtsave(reactive_tab() %>% gt(), 
           paste0(Sys.Date(),".rtf"))
  })


}
shinyApp(ui, server)

downloadHandler() 是解决方案

library(data.table)
library(shiny)
library(gt)
library(shinyscreenshot)

data <- datasets::mtcars 
setDT(data, keep.rownames = TRUE)[]


ui <- navbarPage("Save this to RTF",
                 tabPanel("Table", icon = icon("table"),
                          
                          sidebarLayout(
                            sidebarPanel(
                              selectInput("input",
                                          label = "Choose # of cyl",
                                          choices = c("All", data$cyl)),
                              screenshotButton(selector="#table", label = 'Download Png', filename = 'screenshot'),
                              downloadButton('downloadData', 'Download RTF')
                            ),
                            
                            mainPanel(
                              gt_output("table")
                            )
                          )
                 )
)



server <- function(input, output, session) {
  
  reactive_tab  <- reactive({
    d <- data
    if(input$input != "All")
      d <- subset(d, cyl == input$input)
    d 
  })
  
  output$table <- render_gt( 
    reactive_tab() %>% gt()
  )
  
  
  output$downloadData <- downloadHandler(
    filename = paste0("gttable-",Sys.Date(),".rtf"),
    content = function(file){
      gtsave(reactive_tab() %>% gt(),
             file)
    }
  )
  
  # observeEvent(input$downloadData,{
  #   gtsave(reactive_tab() %>% gt(), 
  #          paste0(Sys.Date(),".rtf"))
  # })
  # 
  
}
shinyApp(ui, server)