r - 从闪亮下载
问题描述
我在闪亮上使用以下代码。我希望能够下载表格和情节
代码本身运行良好,但无法弄清楚如何下载表格和绘图。
library(shiny)
library(ggplot2)
ui <- pageWithSidebar(
headerPanel("Example"),
sidebarPanel(
radioButtons("color", "Pick Color", c("Pink", "Green", "Blue")),
selectInput("shape", "Select Shape:", c("Circle", "Triangle"))
),
mainPanel(
fluidRow(column(width = 6,
h4("Click plot to add points"),
actionButton("rem_point", "Remove Last Point"),
plotOutput("plot1", click = "plot_click")),
column(width = 6,
h4("Table of points on plot"),
tableOutput("table")))
)
)
server = function(input, output){
## 1. set up reactive dataframe ##
values <- reactiveValues()
values$DT <- data.frame(x = numeric(),
y = numeric(),
color = factor(),
shape = factor())
## 2. Create a plot ##
output$plot1 = renderPlot({
ggplot(values$DT, aes(x = x, y = y)) +
geom_point(aes(color = color,
shape = shape), size = 5) +
lims(x = c(0, 100), y = c(0, 100)) +
theme(legend.position = "bottom") +
# include so that colors don't change as more color/shape chosen
scale_color_discrete(drop = FALSE) +
scale_shape_discrete(drop = FALSE)
})
## 3. add new row to reactive dataframe upon clicking plot ##
observeEvent(input$plot_click, {
# each input is a factor so levels are consistent for plotting characteristics
add_row <- data.frame(x = input$plot_click$x,
y = input$plot_click$y,
color = factor(input$color, levels = c("Pink", "Green", "Blue")),
shape = factor(input$shape, levels = c("Circle", "Triangle")))
# add row to the data.frame
values$DT <- rbind(values$DT, add_row)
})
## 4. remove row on actionButton click ##
observeEvent(input$rem_point, {
rem_row <- values$DT[-nrow(values$DT), ]
values$DT <- rem_row
})
## 5. render a table of the growing dataframe ##
output$table <- renderTable({
values$DT
})
}
shinyApp(ui, server)
渲染表等中的values$DT
问题是症结所在,因为我datasetInput()
在闪亮的例子中没有。问题是表格和绘图是动态的,需要类似于此进行更改
我不知道如何改变它。
解决方案
下载用户生成的材料(图表和表格)需要结合使用downloadButton
和downloadHandler
。有关了解下载的帮助,请参见此处。
对于您的情况,第一步是包含downloadButtons
在 UI 中。
ui <- pageWithSidebar(
headerPanel("Example"),
sidebarPanel(
radioButtons("color", "Pick Color", c("Pink", "Green", "Blue")),
selectInput("shape", "Select Shape:", c("Circle", "Triangle")),
downloadButton("dlPlot", "Download Current Plot"),
downloadButton("dlTab", "Download Current Table")
),
mainPanel(
fluidRow(column(width = 6,
h4("Click plot to add points"),
actionButton("rem_point", "Remove Last Point"),
plotOutput("plot1", click = "plot_click")),
column(width = 6,
h4("Table of points on plot"),
tableOutput("table")))
)
)
接下来,就是downloadHandler
在服务器中包含 s。我将此块放在您的服务器调用的末尾,没有其他更改:
output$dlPlot <- downloadHandler(
filename="Plot_Download.jpg",
content=function(file){
ggsave(file, device = "jpeg")
}
)
output$dlTab <- downloadHandler(
filename="Table_Download.csv",
content=function(file){
write.csv(x=values$DT, file=file)
}
)
我发现ggsave()
并且write.csv()
是最容易使用的操作downloadHandler
,但还有其他方法,您可以在其他 SO 问题中找到。
推荐阅读
- sql-server - SQL 中使用的 Modern_Spanish_CI 函数是什么?
- ffmpeg - ffmpeg + 树莓派 4b + stream_loop CPU 优化
- angular - 在以下位置找不到赛普拉斯可执行文件:/root/.cache/Cypress/3.8.3/Cypress/Cypress
- python - 需要使用 python 替换配置文件中的相似键值
- android - Last remote notification overrides the previous one android
- c - 打印一个 const deque 结构
- java - 将 ARCore 的 Estimated Environmental HDR Cube Map 保存到内存
- mysql - 未填充外键
- assembly - 为什么必须取消引用数据标签才能在其中存储一些东西:Assembly 8086 FASM
- python - 如何在python tkinter中将原点更改为画布的中心