如何在 "redimensioned" 图后面制作输出的下载按钮?

How can I make the output's download button behind a "redimensioned" plot?

早上好,

我正在尝试修复 "redimensioned" 图后面的输出下载按钮,但到目前为止我还没有找到解决方案。你有什么想法吗?

这是一个示例代码:

ui <- fluidPage(

  titlePanel(
    sidebarLayout(
    sidebarPanel(
      wellPanel(h3("Feed the parameters below"))),

    mainPanel(
      tabsetPanel(
        tabPanel(
          h4("plot"),
          plotOutput(outputId = "plot"),
          downloadButton(outputId = "download_plot", label = "Download plot")))))))


server <- function(input, output){ 

  plot_input <- reactive({ 

      p <- ggplot(data=ChickWeight, aes(x=Time, y=weight, color=Diet, group=Chick)) + geom_line() 

    }) 

  output$plot <- renderPlot({

    print(plot_input())}, width = 1200, height = 800)}

output$download_plot <- downloadHandler(

  filename = function() { paste0("plot_", Sys.Date(), ".png") },

  content = function(file) {

    device <- function(..., width, height) {
      grDevices::png(..., height = 20, res = 300, units = "cm")}

    ggsave(file, plot = plot_input(), device = device)


  })


shinyApp(ui = ui, server = server) 

我假设您希望 "Download plot" 按钮停留在预先确定的位置? 我能想到的最基本的解决方案就是将 downloadButton 向上移动到主面板,如下所示:

library(shiny)

ui <- fluidPage(

  titlePanel(
    sidebarLayout(
      sidebarPanel(
        wellPanel(h3("Feed the parameters below"))),

      mainPanel(
        downloadButton(outputId = "download_plot", label = "Download plot"), #  This 
                                                                              # is where I moved it to
        tabsetPanel(

          tabPanel(
            h4("plot"),
            plotOutput(outputId = "plot")
       # This is where the code-snippet used to be
           ))))))


server <- function(input, output){ 

  plot_input <- reactive({ 
    df <- ChickWeight
    p <- ggplot(data=df, aes(x=Time, y=weight, color=Diet, group=Chick)) + geom_line() 

  }) 

  output$plot <- renderPlot({

    print(plot_input())}, width = 1200, height = 800)}

output$download_plot <- downloadHandler(

  filename = function() { paste0("plot_", Sys.Date(), ".png") },

  content = function(file) {

    device <- function(..., width, height) {
      grDevices::png(..., height = 20, res = 300, units = "cm")}

    ggsave(file, plot = plot_input(), device = device)


  })


shinyApp(ui = ui, server = server) 

输出(下载按钮停留在图上):

如果server中不需要定义宽度和高度,可以在ui中设置:

plotOutput(outputId = "plot", width = "1200px", height = "800px")

这样下载按钮就在剧情下方了