如果对象不存在,R Shiny 忽略下载
Rshiny Ignore download if object not present
我有一个闪亮的应用程序,用户上传 5 个数据源,生成一些汇总表,然后将这些表下载到 ppt。目前,此下载仅在将所有 5 个源都加载到应用程序中时才有效。我正在尝试通过 "exists" 函数找到解决方法。
下面是只有一个文件的示例代码,演示应该适用于任何 csv 文件。
在下载处理程序部分,代码通过if (exists("file1.df"))
检查数据帧是否存在,然后生成flextable。生成幻灯片的代码块也使用 if (exists("flextable_file1"))
但是,ppt 仅在上传文件时才会生成。如果 "exists" 对象不存在,为什么不忽略代码部分?
library(shiny)
library(tidyverse)
library(flextable)
library(officer)
ui <- fluidPage(
# Side bar layout and inputs ----
sidebarLayout(
sidebarPanel(
h4("Attach file"),
fileInput("file1", "File location", accept = c(".csv")),
downloadButton("download_powerpoint", "Download Tables to Powerpoint")
),
mainPanel(tableOutput("file1.df"))
)
)
server <- function(input, output, session) {
# LMS Pivot ----
file1.df <- reactive({
req(input$file1)
read.csv(input$file1$datapath)
})
output$file1.df <- renderTable({
file1.df()
})
# PPT ----
output$download_powerpoint <- downloadHandler(
filename = function() {
"test.pptx"
},
content = function(file) {
if (exists("file1.df")) {
flextable_file1 <- flextable(file1.df()) %>%
border_remove() %>%
border(border.top = fp_border(color = "black"),
border.bottom = fp_border(color = "black"),
border.left = fp_border(color = "black"),
border.right = fp_border(color = "black"), part = "all") %>%
align(align = "center", part = "all")
}
example_pp <- read_pptx() %>%
add_slide(layout = "Title Slide", master = "Office Theme") %>%
ph_with_text(
type = "ctrTitle",
str = "Weekly P3 Deck"
) %>%
ph_with(
location = ph_location_type(type = "subTitle"),
value = "Copy and paste the generated tables into your report"
)
# LMS slide ----
if (exists("flextable_file1")) {
example_pp <- example_pp %>% add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_text(
type = "title",
str = "words"
) %>%
ph_with_flextable(
value = flextable_file1,
type = "body"
)
}
print(example_pp, target = file)
}
)
}
shinyApp(ui, server)
我找到了解决办法。而不是使用 if exists
使用 if else
到 return null 如果它没有上传 else return df.
if (is.null(input$file1)) {
NULL
} else {
flextable_file1 <- flextableformatting
我有一个闪亮的应用程序,用户上传 5 个数据源,生成一些汇总表,然后将这些表下载到 ppt。目前,此下载仅在将所有 5 个源都加载到应用程序中时才有效。我正在尝试通过 "exists" 函数找到解决方法。
下面是只有一个文件的示例代码,演示应该适用于任何 csv 文件。
在下载处理程序部分,代码通过if (exists("file1.df"))
检查数据帧是否存在,然后生成flextable。生成幻灯片的代码块也使用 if (exists("flextable_file1"))
但是,ppt 仅在上传文件时才会生成。如果 "exists" 对象不存在,为什么不忽略代码部分?
library(shiny)
library(tidyverse)
library(flextable)
library(officer)
ui <- fluidPage(
# Side bar layout and inputs ----
sidebarLayout(
sidebarPanel(
h4("Attach file"),
fileInput("file1", "File location", accept = c(".csv")),
downloadButton("download_powerpoint", "Download Tables to Powerpoint")
),
mainPanel(tableOutput("file1.df"))
)
)
server <- function(input, output, session) {
# LMS Pivot ----
file1.df <- reactive({
req(input$file1)
read.csv(input$file1$datapath)
})
output$file1.df <- renderTable({
file1.df()
})
# PPT ----
output$download_powerpoint <- downloadHandler(
filename = function() {
"test.pptx"
},
content = function(file) {
if (exists("file1.df")) {
flextable_file1 <- flextable(file1.df()) %>%
border_remove() %>%
border(border.top = fp_border(color = "black"),
border.bottom = fp_border(color = "black"),
border.left = fp_border(color = "black"),
border.right = fp_border(color = "black"), part = "all") %>%
align(align = "center", part = "all")
}
example_pp <- read_pptx() %>%
add_slide(layout = "Title Slide", master = "Office Theme") %>%
ph_with_text(
type = "ctrTitle",
str = "Weekly P3 Deck"
) %>%
ph_with(
location = ph_location_type(type = "subTitle"),
value = "Copy and paste the generated tables into your report"
)
# LMS slide ----
if (exists("flextable_file1")) {
example_pp <- example_pp %>% add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_text(
type = "title",
str = "words"
) %>%
ph_with_flextable(
value = flextable_file1,
type = "body"
)
}
print(example_pp, target = file)
}
)
}
shinyApp(ui, server)
我找到了解决办法。而不是使用 if exists
使用 if else
到 return null 如果它没有上传 else return df.
if (is.null(input$file1)) {
NULL
} else {
flextable_file1 <- flextableformatting