如何使输出的下载按钮位于“尺寸”图的后面?

时间:2019-03-18 10:20:57

标签: r shiny shinydashboard shiny-server

早上好

我正在尝试将输出的下载按钮固定在“尺寸”图的后面,但是到目前为止,我还没有解决方案。你有什么主意吗?

这是示例代码:

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) 

2 个答案:

答案 0 :(得分: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) 

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

Download Button stays over the plot

答案 1 :(得分:1)

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

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

通过这种方式,下载按钮位于情节下方。