在模块中使用 R Shiny 应用程序下载 powerpoint

SNT

我正在尝试为我们闪亮的应用程序开发一项功能,用户可以在其中下载包含所有表格和图表的 Powerpoint。我确实看到了一个独立的应用程序,如果所有的表和图都在服务器组件中,我知道如何使用它。由于我们的代码库不断增加,并且我们正在尝试使用模块来破坏应用程序,因此我无法确定我应该在哪里拥有下载处理程序。如果我在服务器组件中有它,我如何将我的表格和绘图从模块传递到服务器中的这个函数?以下是独立下载到 powerpoint 代码的代码。

library(shiny)
library(officer)
library(flextable)
library(dplyr)

my_table <- data.frame(
  Name = letters[1:4],
  Age = seq(20, 26, 2),
  Occupation = LETTERS[15:18],
  Income = c(50000, 20000, 30000, 45000)
)

ui <- fluidRow(
  column(
    width = 12,
    align = "center",
    tableOutput("data"),
    br(),
    downloadButton("download_powerpoint", "Download Data to PowerPoint")
  )
)

server <- function(input, output) {
  output$data <- renderTable({
    my_table
  })

  output$download_powerpoint <- downloadHandler(
    filename = function() {  
      "employee_data.pptx"
    },
    content = function(file) {
      flextable_prep <- flextable(my_table) %>% 
        colformat_num(col_keys = c("Age", "Income"), digits = 0) %>% 
        width(width = 1.25) %>% 
        height_all(height = 0.35) %>% 
        theme_zebra() %>% 
        align(align = "center", part = "all")

      example_pp <- read_pptx() %>% 
        add_slide(layout = "Title Slide", master = "Office Theme") %>% 
        ph_with_text(
          type = "ctrTitle",
          str = "Employee Data"
        ) %>% 
        ph_with(
          location = ph_location_type(type = "subTitle"),
          value = "Company 2019 Report"
        ) %>% 
        add_slide(layout = "Title and Content", master = "Office Theme") %>% 
        ph_with_text(
          type = "title",
          str = "2019 Data"
        ) %>% 
        ph_with_flextable_at(
          value = flextable_prep,
          left = 2.5,
          top = 2
        )

      print(example_pp, target = file)
    }
  )
}

shinyApp(ui, server)
科林·费

有多种方法可以将数据从一个模块传递到另一个模块。

例如,您可以从一个模块返回一个反应式,然后在另一个模块中使用它。

请参阅(我在这里删除了 powerpoint 生成以专注于反应性的实现):

library(shiny)
library(officer)
library(flextable)
library(dplyr)

showui <- function(id){
  ns <- NS(id)
  tagList(
    selectInput(ns("table"), "table", choices = c("iris", "mtcars")),
    tableOutput(ns("data"))
  )
}

show <- function(input, output, session){
  ns <- session$ns

  my_table <- reactive({
    get(input$table)
  })

  output$data <- renderTable({
    head(my_table())
  })

  my_table
}


dlui <- function(id){
  ns <- NS(id)
  tagList(
    downloadButton(
      ns("download_powerpoint"), 
      "Download Data"
    )
  )
}

dl <- function(input, output, session, my_table){
  ns <- session$ns

  output$download_powerpoint <- downloadHandler(
    filename = function() {  
      "employee_data.csv"
    },
    content = function(file) {
      write.csv(my_table(), file)
    }
  )
}

ui <- fluidRow(
  column(
    width = 12,
    align = "center",
    showui("showui"),
    br(),
    dlui("dlui")
  )
)

server <- function(input, output) {

  my_table <- callModule(show, "showui")

  callModule(dl, "dlui", my_table)
}

shinyApp(ui, server)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在从Shiny应用程序下载的R Markdown报告中使用图像

如何使用 xlsx 包从 R Shiny 应用程序动态合并 XLSX 下载的列?

如何使用 source() 函数启动 R Shiny 应用程序

在Shiny应用程序中使用gettext

如何在R Shiny应用程序中使用工作区对象

如何在R Shiny应用程序中使用{gtsummary}软件包

在R Shiny应用程序中下载ggplot2和绘图对象

在R Shiny应用程序中从HTML提取下载链接

在模块中使用shiny.i18n翻译闪亮的应用程序?

使用传递给闪亮应用程序的参数将R Shiny应用程序另存为函数

神秘的JavaScript错误,具体取决于R Shiny应用程序中使用的绘图对象名称

在R Shiny中的多个模块中使用reactValues

如何使用Shiny服务器在本地计算机上对R Shiny应用程序进行Docker化?

R Shiny 中的本地持久数据存储,供部署的应用程序中的多个用户使用

R Shiny 应用程序,使用 rhandsontable,具有类似 excel 的功能

R Shiny应用程序可保存输入以供以后使用

如何使用htmlOutput在R Shiny应用程序中启用语法突出显示

R Shiny:使用模式/弹出窗口创建应用程序内教程

如何在我的R Shiny应用程序中显示使用rcharts创建的图表?

我可以使用Shiny R扩展完整的Web应用程序吗?

使用ReporteRs包使用R Shiny生成PowerPoint幻灯片

如何纠正在Shiny应用程序中使用MutationObserve

在 Shiny 应用程序中使用 Font Awesome 5.7 图标

R Shiny-在Shiny模块中使用'session $ sendCustomMessage'设置输入值

R Shiny不能在大型应用中使用Shinytest

如果在R Shiny应用中使用else

R Shiny应用程序用于数据可视化

在R Shiny应用程序中返回HTTP状态代码

R Shiny 应用程序中的外部过滤器