0

早上好,

我正在尝试修复“重新调整尺寸”图后面的输出下载按钮,但到目前为止我还没有得到解决方案。你有什么想法吗?

这是一个示例代码:

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) 
4

2 回答 2

1

我假设您希望您的“下载情节”按钮停留在预定位置?我能想到的最基本的解决方案就是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) 

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

下载按钮停留在情节上

于 2019-03-18T10:31:02.140 回答
1

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

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

这样,下载按钮就在情节下方。

于 2019-03-18T10:42:08.787 回答