我正在尝试为我们闪亮的应用程序开发一项功能,用户可以在其中下载包含所有表格和图表的 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] 删除。
我来说两句