【发布时间】:2020-07-13 01:04:18
【问题描述】:
我想和包装官一起从 Shiny 下载 PowerPoint 幻灯片。我制作了一个包含绘图的 PowerPoint 示例。如果更改绘图的输入,如何更新幻灯片?因为当我更改输入时,它并没有更新绘图,而是添加了一张带有修改后绘图的新幻灯片。那不是我想要的。我想根据输入更新绘图。如何做到这一点?这是一个可重现的示例:
- 导入库并定义有用的函数
# Import packages ---------------------------------------------------------
library(shiny)
library(tidyr)
library(dplyr)
library(ggplot2)
library(officer)
# Useful functions --------------------------------------------------------
IsNumeric <- function(x){return(is.numeric(x) == TRUE)}
IsNotNumeric <- function(x){return(is.numeric(x) == FALSE)}
- 定义用户界面
# Define user interface ---------------------------------------------------
ui <- fluidPage(
titlePanel("Dataset analysis"),
sidebarLayout(
sidebarPanel(
# Select categorical variables:
selectInput(inputId = "CatVar"
, label = "Select categorical variables:"
, choices = diamonds %>% select_if(IsNotNumeric) %>% colnames()
, multiple = TRUE
),
selectInput(inputId = "NumVar"
, label = "Select categorical variables:"
, choices = diamonds %>% select_if(IsNumeric) %>% colnames()
, multiple = FALSE
)
),
mainPanel(
plotOutput(outputId = "plot_id"),
downloadButton(outputId = "pptx_id"
, label = "Download analysis to PowerPoint"
)
)
)
)
- 定义服务器函数
mypptx <- read_pptx()
server <- function(session, input, output){
selectCatVar <- reactive({
validate(
need(is.null(input$CatVar) == FALSE, "Please select at least one categorical variable.")
)
input$CatVar
})
# selectNumVar <- reactive({input$NumVar})
myplot <- reactive({
dat <- diamonds %>% select(selectCatVar(), input$NumVar) %>%
gather(MyVar, MyValue, -input$NumVar)
ggplot(data = dat, mapping = aes(x = MyValue, y = !!sym(input$NumVar), fill = MyValue)) +
geom_boxplot() +
facet_wrap(MyVar ~ ., scales = "free_x") +
labs(y = input$NumVar) +
theme(legend.position = "none"
)
})
output$plot_id <- renderPlot({
myplot()
})
output$pptx_id <- downloadHandler(
filename = function(){"test_pptx.pptx"},
content = function(file){
mypptx %>% add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = myplot(), location = ph_location_type(type = "body")) %>%
print(target = file)
}
)
}
- 运行应用程序
shinyApp(ui = ui, server = server)
【问题讨论】:
-
感谢您的好评。我真的不明白,当我更改输入时,情节会更新,如果我点击下载 pptx,我会在单个幻灯片 powerpoint 文件中得到完全相同的情节。
-
我明白你的意思。我想我没有详细解释我是如何得到这个“错误”的。首先,我运行应用程序。其次,我选择输入。第三,我下载了PPT。第四,我在没有关闭应用程序的情况下更改了输入。最后,我下载了PPT。从那,我得到一张带有修改后的情节的新幻灯片。从解释中,你最终会遇到同样的问题吗?
-
我复制了这个例子,但我没有下载 powerpoint,而是下载了 CSV。它运作良好。所以,我不认为这是一个闪亮的问题。
-
不抱歉。第四,你得到了一张带有修改后的情节的新幻灯片。你期待什么?
-
我希望一张幻灯片带有修改后的情节,而不是 2 张幻灯片。你更明白了吗?也许我可以给你一个我期望的例子。