Upload and View a PDF in R Shiny

upload and view a pdf in R shiny

I think the issue is with the binary reading and writing. Instead trying to copy the files using file.copy seems to work. Also I've taken the iframe inside observeEvent for the iframe to update every time the pdf is uploaded in the same session.

Updated Code:

library(shiny)

ui <- shinyUI(fluidPage(

titlePanel("Testing File upload"),

sidebarLayout(
sidebarPanel(
fileInput('file_input', 'upload file ( . pdf format only)', accept = c('.pdf'))
),

mainPanel(
uiOutput("pdfview")
)
)
))

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

observe({
req(input$file_input)

#test_file <- readBin(input$file_input$datapath, what="raw")

#writeBin(test_file, "myreport.pdf")

#cat(input$file_input$datapath)

file.copy(input$file_input$datapath,"www", overwrite = T)

output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src="0.pdf")
})

})

})

shinyApp(ui = ui, server = server)

Upload an image to R shiny downloadable to pdf

The issue is that RenderImage will delete the file after usage unless deleteFile = False. This is the line to change, Remove this:

 re1 <- reactive({gsub("\\\\", "/", input$file1$datapath)})

Add this:

output$uploaded_image <- renderImage({list(src = input$file1$datapath)}, deleteFile = FALSE)

R Shiny import multiple PDFs and view them one by one using actionButton

I have worked out a solution! I use reactiveVal for variable x, and each time the actionButton is clicked, x will increase 1. On that basis, I can view the PDF one by one by specifying datapath[]. It can be very useful for people with multiple pdf files.

library(shiny)

ui <- shinyUI(fluidPage(

titlePanel("Testing File upload"),

sidebarLayout(
sidebarPanel(
fileInput('file_input', 'upload file ( . pdf format only)',
accept = c('.pdf'),multiple = T),
tableOutput("files"),
actionButton("next_pdf", "Next PDF"),
textOutput("testing")
),

mainPanel(
uiOutput("pdfview")
)
)
))

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

x = reactiveVal(1)

output$files <- renderTable({input$file_input})

observeEvent(input$file_input,{

file.copy(input$file_input$datapath[1],"www", overwrite = T)

output$pdfview <- renderUI({
tags$iframe(style="height:1200px; width:100%", src="0.pdf")
})

})

observeEvent(input$next_pdf,{
x(x()+1)

file.rename(input$file_input$datapath[x()], "0.pdf")
file.copy("0.pdf","www", overwrite = T)

output$pdfview <- renderUI({
tags$iframe(style="height:1200px; width:100%", src="0.pdf")
})

output$testing = renderText(x())
})



})

shinyApp(ui = ui, server = server)

R shiny, upload word doc/PDF and run function on uploaded document, Output issues

fileInput doesn't return the filename/path directly.

It returns a dataframe with the name , size, type and datapath.

What you probably want is datapath, so try this.

file <- input$text

wordtest<-readtext(file$datapath)

Download and display PDF in Shiny

You need to download the PDF in binary mode in a subfolder of your current directory, then call addResourcePath to allow shiny to serve it:

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


Related Topics



Leave a reply



Submit