本文介绍了从Shiny(R)下载png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我对Shiny(和R)来说很新,并且将Shiny中的情节导出到png文件中。 我看了这两个线程,但无法弄清楚: 在闪亮的应用程序中保存的地图 闪亮的downloadHandler不保存PNG文件 我设法在ui中创建下载按钮,服务器似乎也在做我想做的一切。当我在预览窗口中点击下载按钮时,弹出窗口要求我指定文件的位置和名称,但不保存任何文件。当我在浏览器窗口中执行相同操作时,会创建一个png文件,但它是空的。 任何见解非常感谢! ui.R $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b sidebarLayout( sidebarPanel( fileInput('datafile','Choose CSV file', accept = c('text / csv','text / value,text / plain')) uiOutput(varselect1), uiOutput(varselect2), downloadButton(' mainPanel( h4(这是你的散点图), plotOutput(plot1 ))))) server.R library(foreign) ShinyServer(function(session,input,output)){ DataInput< --active({ infile< - input $ datafile if(is.null(infile)){ return(NULL)} read.csv(infile $ datapath)}) 输出$ varselect1 < - renderUI({ if(identical(DataInput(),'')||相同(DataInput(),data.frame()))return(NULL) cols< - 名称(DataInput()) selectInput(var1,选择一个变量: ,choices = c(---,cols [3:length(cols)]),selected =(---)) }) 输出$ varselect2< - renderUI({ if(identical(DataInput(),'')|| same(DataInput(),data.frame()))return(NULL) cols< - names(DataInput()) selectInput(var2,选择一个变量:,choices = c(---,cols [3:length )]),selected =(---)) }) plotInput< - reactive({ a x_lab< - as.numeric(DataInput()[,a]) b< - 其中(name(DataInput())==输入$ var2) y_lab< - as.numeric(DataInput()[,b]) main .text< - paste(Scatterplot of the variables,colnames(DataInput())[a],and,colnames(DataInput())[b],sep =,collapse = NULL ) plot(x_lab,y_lab,main = main.text,xlab = colnames(DataInput())[a],ylab = colnames(DataInput())[b],xlim = c (x_lab),max(x_lab)* 1.05),ylim = c(min(y_lab),max(y_lab)* 1.05)) 观察 文本(x_lab,y_lab,labels = observations,pos = 3) }) 输出$ plot1< - renderPlot { print(plotInput())}) 输出$ downloadPlot< - downloadHandler( filename =Shinyplot.png, content = function(file){ png(file) print(plotInput()) dev.off()}) }) 解决方案已经讨论了这个奇怪场景的解决方法在闪亮讨论的Google群组。您可以做的只是将您的反应性 plotInput 语句更改为正常的功能。不确定为什么 downloadHandler 对于反应对象不会很好。 #change plotInput< - reactive({...}) #into this plotInput< - function(){...} 您还可以删除 downloadHandler 调用中的print语句: 输出$ downloadPlot< - downloadHandler( filename =Shinyplot.png, content = function (文件){ png(file) plotInput() dev.off()}) I am pretty new to Shiny (and R) and struggling with exporting the plot I make in Shiny to a png-file. I looked at these two threads but could not figure it out:Save plots made in a shiny appShiny downloadHandler doesn't save PNG filesI manage to create the download button in the ui and the server seems to be doing everything I want it to do, too. When I hit the download button in the preview window, a pop up window asks me to specify the file location and name but no file is saved. When I do the same in a browser window, a png file is created but it is empty. Any insight is much appreciated!ui.Rlibrary(shiny)shinyUI(fluidPage( titlePanel("This is a scatterplot"), sidebarLayout( sidebarPanel( fileInput('datafile', 'Choose CSV file', accept=c('text/csv', 'text/comma-separated-values,text/plain')), uiOutput("varselect1"), uiOutput("varselect2"), downloadButton('downloadPlot', 'Download Plot') ), mainPanel( h4("Here is your scatterplot"), plotOutput("plot1") ) )))server.Rlibrary(foreign)shinyServer(function(session,input, output) { DataInput <- reactive({ infile <- input$datafile if (is.null(infile)) { return(NULL) } read.csv(infile$datapath) }) output$varselect1 <- renderUI({ if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL) cols <- names(DataInput()) selectInput("var1", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---")) }) output$varselect2 <- renderUI({ if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL) cols <- names(DataInput()) selectInput("var2", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---")) }) plotInput <- reactive({ a <- which(names(DataInput())==input$var1) x_lab <- as.numeric(DataInput()[,a]) b <- which(names(DataInput())==input$var2) y_lab <- as.numeric(DataInput()[,b]) main.text <- paste("Scatterplot of the variables",colnames(DataInput())[a],"and", colnames(DataInput())[b],sep = " ", collapse = NULL) plot(x_lab, y_lab, main=main.text, xlab=colnames(DataInput())[a], ylab=colnames(DataInput())[b], xlim=c(min(x_lab),max(x_lab)*1.05), ylim=c(min(y_lab), max(y_lab)*1.05)) observations <- DataInput()[,1] text(x_lab, y_lab, labels=observations, pos=3) }) output$plot1 <- renderPlot({ print(plotInput()) }) output$downloadPlot <- downloadHandler( filename = "Shinyplot.png", content = function(file) { png(file) print(plotInput()) dev.off() }) }) 解决方案 A workaround for this strange scenario was discussed on the shiny-discuss google group. What you can do is simply change your reactive plotInput statement into a normal function. Not sure why downloadHandler doesn't play nice with reactive objects.# changeplotInput <- reactive({...})# into thisplotInput <- function(){...}You can also remove the print statement in the downloadHandler call:output$downloadPlot <- downloadHandler( filename = "Shinyplot.png", content = function(file) { png(file) plotInput() dev.off() }) 这篇关于从Shiny(R)下载png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-23 09:10