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
Rselenium, Chrome, How to Set Download Directory, File Download Error
Stacked Bar Chart, Reorder by Total (Sum Up of Values) Instead of Value Ggplot2 + Dplyr
R: Adding a "Tool Tip" to Interactive Plot (Plotly)
R: Matrix by Vector Multiplication
Rhtml: Warning: Conversion Failure on '<Var>' in 'Mbcstosbcs': Dot Substituted for <Var>
Unexpected Symbol Error in Parse(Text = Str) with Hyphen After a Digit
Force a Regular Plot Object into a Grob for Use in Grid.Arrange
Q-Q Plot with Ggplot2::Stat_Qq, Colours, Single Group
In R, Check If String Appears in Row of Dataframe (In Any Column)
Splitting String Based on Letters Case
How to Replace Multiple Strings with the Same in R
Extent of Boundary of Text in R Plot
Getsymbols and Using Lapply, Cl, and Merge to Extract Close Prices
Compute Projection/Hat Matrix via Qr Factorization, Svd (And Cholesky Factorization)
How to Add Se Error Bars to My Barplot in Ggplot2
Renaming and Hiding an Exported Rcpp Function in an R Package