如何保存闪亮阴谋的PNG图像,以便它符合我的屏幕上的尺寸?

问题描述:

我已经绘制了闪亮的图像,并创建了一个保存按钮以将图像保存为.png文件。如何保存闪亮阴谋的PNG图像,以便它符合我的屏幕上的尺寸?

server <- function(input, output) { 

    output$plot <- renderPlot({ 
    plot(1:500,rep(1:5,100)) 
    }) 

    plotInput <- function(){ 
    plot(1:500,rep(1:5,100)) 
    } 

    output$savePlot <- downloadHandler(
    filename = "file.png", 
    content = function(file) { 
     png(file) 
     plotInput() 
     dev.off() 
    } 
) 
} 

当查看我的笔记本电脑的屏幕(1366×768分辨率)上的应用程式闪亮的图像,积变为伸出(即,长于其宽)。 [注:这就是我想要的]。

  • 注意:我使用的是fluidRow,所以图像的大小取决于我查看的屏幕。

当我保存绘图时,输出文件不再像在应用程序本身中查看时那样水平长,而是保存为方形。

现在我知道我可以修改使用widthheightunits参数在png()函数的输出图像的大小。

但是,我不知道我的Shiny应用程序中打开图像的大小。

所以我的问题:

有没有办法来保存图像中一个闪亮的应用程序,因为它在屏幕上查看时不自动具有相同的“尺寸”?

注:我意识到,当我将应用程序移动到新的显示器或设备时,图形的形状/尺寸会发生变化,所以我正在寻找某种方式来主动确定图像的当前尺寸。


这里是我的阴谋看起来像我的笔记本电脑屏幕上的闪亮应用:

enter image description here


同样,我最想要的解决方案来自动确定当前尺寸显示在我的屏幕上,并将这些尺寸应用于我保存的png输出图像。

添加以下JavaScript到您的UI:

tags$script("$(document).on('shiny:connected', function(event) { 
var myWidth = $(window).width(); 
Shiny.onInputChange('shiny_width',myWidth) 

});"), 

    tags$script("$(document).on('shiny:connected', function(event) { 
var myHeight = $(window).height(); 
Shiny.onInputChange('shiny_height',myHeight) 

});") 

然后,改变你的downloadHandler代码如下:

output$savePlot <- downloadHandler(
    filename = "file.png", 
    content = function(file) { 
     png(file, 
      width = input$shiny_width, 
      height = input$shiny_height) 
     plotInput() 
     dev.off() 
    } 
) 
+0

真棒!这工作出色!谢谢! – theforestecologist

+0

哦,这非常有用!谢谢@ kostr – infominer