Shiny

2017-05-02 4 views
2

でPDFをダウンロードして表示するshinyapps.ioのアプリでウェブ上のいくつかのPDFを表示しようとしています。残念ながら、iframeをURLとともに使用する標準的な方法は、混在したコンテンツのセーフガード(pdfはhttp経由で提供される)のためオプションではありません。私は、可能なオプションは、URLからpdfsをダウンロードし、それらをローカルファイルからのiframeに表示することだと思うが、これをtempfile()と動作させることはできない。Shiny

サンプルアプリ:

ui <- fluidPage(
    sidebarLayout(
     sidebarPanel(
     textInput("url", "add a url"), 
     actionButton("button","hit the button"), 
     h5("use case - embed a pdf user guide in the app - embed as a local pdf or from web URL") 
    ), 
     mainPanel(
     tabsetPanel(
      tabPanel("PDF", 
        htmlOutput("pdf") 
        ) 
     ) 
     ) 
    ) 
) 

server <- function(input, output, session) { 

    observeEvent(input$button, { 
    temp <- tempfile(fileext = ".pdf") 
    download.file(input$url, temp) 

    output$pdf <- renderUI({ 
     tags$iframe(src=temp) 
    }) 
    }) 
} 

shinyApp(ui, server) 

サンプルPDF:私は、ブラウザでこれを開くとhttp://www.pdf995.com/samples/pdf.pdf

私は、ブラウザのコンソールでエラーが出ます: のiframeがあるパネルでNot allowed to load local resource: file:///C:/Users/.../Local/Temp/Rtmp8subWX/file19403a2a2fc8.pdfと何も。

shinyapps.ioにアップロードされた同様の試みも失敗し、404 Not Foundというエラーがpdfビューアに表示されます。

これは、shiny/shinyapps.ioが一時ファイルを処理する方法に問題があるとは思いますが、それを理解することはできません。ありがとう。

答えて

3

あなたはそれを提供するために光沢のあるできるようにaddResourcePathを呼び出して、現在のディレクトリのサブフォルダにバイナリモードでPDFをダウンロードする必要があります。

observeEvent(input$button, { 
    pdf_folder <- "pdf_folder" 
    if(!file.exists(pdf_folder)) 
     dir.create("pdf_folder") 
    temp <- tempfile(fileext = ".pdf", tmpdir = "pdf_folder") 
    download.file(input$url, temp, mode = "wb") 
    addResourcePath("pdf_folder",pdf_folder) 

    output$pdf <- renderUI({ 
     tags$iframe(src=temp) 
    }) 
    })