我下面有一个闪亮的应用程序,我正在尝试通过downloadHandler()
下载该绘图。问题是Im试图下载的对象是数据表或绘图,因此我不知道如何将其传递给downloadHandler()
。请注意,我的下载按钮也是动态的,因为在第一种情况下,它应该下载表格,在第二种情况下,它应该下载图表。请注意,我只关心这里的情节。在浏览器中打开应用程序。首先安装:
library(webshot)
install_phantomjs()
然后:
library(shiny)
library(plotly)
d <- data.frame(X1 = rnorm(50,mean=50,sd=10),
X2 = rnorm(50,mean=5,sd=1.5),
Y = rnorm(50,mean=200,sd=25))
ui <-fluidPage(
title = 'Download Plotly',
sidebarLayout(
sidebarPanel(
selectInput("S","SELECT",choices = c("Table","Plot"),selected = "Plot"),
uiOutput('down')
),
mainPanel(
uiOutput('regPlot')
)
)
)
server <- function(input, output, session) {
output$down<-renderUI({
if(input$S=="Table"){
output$downloadData <- downloadHandler(
filename = function() {
paste(input$filename, input$extension, sep = ".")
},
# This function writes data to a file given to it by the argument 'file'.
content = function(file) {
sep <- "txt"=","
# Write to a file specified by the 'file' argument
write.table(data.frame(mtcars), file, sep = sep,
row.names = FALSE)
}
)
downloadButton("downloadData", "Download",class = "butt1")
}
else{
output$downloadData <- downloadHandler(
filename = function(){
paste0(paste0("test", Sys.Date()), ".png")
},
content = function(file) {
export(regPlot, file=file)
})
downloadButton("downloadData", "Download",class = "butt1")
}
})
output$regPlot<-renderUI({
if(input$S=="Plot"){
output$pl<-renderPlotly(
plot_ly(d, x = d$X1, y = d$X2, mode = "markers"))
plotlyOutput("pl")
}
else{
output$tbl = DT::renderDataTable(datatable(
d
))
dataTableOutput("tbl")
}
})
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:1)
根据ploty site使用以下代码:
if (!require("processx")) install.packages("processx")
p <- plot_ly(z = ~volcano) %>% add_surface()
orca(p, "surface-plot.png")