简体   繁体   English

如何使用python网状闪亮的下载按钮?

[英]How to use Download Button in shiny with python reticulate?

I'm building a shiny app and I have a "download button" ! 我正在构建一个闪亮的应用程序,我有一个“下载按钮”! I'm also using python from reticulate because I have a script that generate a PDF for me according to the generated charts in the app. 我也使用来自网状的python,因为我有一个脚本,根据应用程序中生成的图表为我生成PDF。 The package I'm using to create the pdf is FPDF 我用来创建pdf的包是FPDF

Here is my function in R from python that create my pdf 这是我在py中创建我的pdf的函数

createPdf <- function(path){
     source_python("plots/create_pdf.py")
       pdf <- PDF()
       pdf$alias_nb_pages()
       pdf$add_page()
       pdf$plot_charts_field('farmer', 'region', 'produto')
       pdf$output(path + 'report.pdf', 'F')
   }

and here is my download button output 这是我的下载按钮输出

output$download <- downloadHandler(
     filename = 'report.pdf',
     content = function(file) {
      createPdf (file)
     })

When I call the function the function "createPdf" I need to pass in the argument the path where the pdf will download and the user will choose the directory, but I don't know how to do that. 当我调用函数“createPdf”函数时,我需要传递参数pdf将下载的路径,用户将选择目录,但我不知道该怎么做。 Is it possible to do that? 有可能吗? How could I do it? 我怎么能这样做?

My problem was that I needed to pass to the python script the path where the pdf would be saved. 我的问题是我需要将保存pdf的路径传递给python脚本。 Here is my solution to the problem: 这是我解决问题的方法:

ui.R ui.R

sidebarLayout
  (
    sidebarPanel(width = 3,
      radioButtons(inputId = "choices", "Tipo", c("Individual", "Global"), selected = "Individual"),
      uiOutput("Filters"), downloadButton('download', "Download")
    )

server.R server.R

  makePdf <- function(filename){
     source_python("plots/create_pdf.py")
     if (input$choices == 'Individual')
     {
       pdf <- PDF()
       pdf$alias_nb_pages()
       pdf$add_page()
       pdf$plot_charts_field('farmer', 'region', 'produto')
       pdf$output(filename, 'F')
     } else
     {
       source_python("plots/plot_mapa.py")
       plot_mapa(alltables_filter())
       pdf <- PDF()
       pdf$alias_nb_pages()
       pdf$add_page()
       pdf$plot_charts_global()
       pdf$output(filename, 'F')
     }
   }


output$download <- downloadHandler('report.pdf', function(theFile) {

       makePdf(theFile)

     })

This way, the user can dowload the pdf 这样,用户可以下载pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM