Hi!
I have a Rshiny app with various tabpanels, and in one of the tabpanels i want to display certain pages of a PDF file and i wuould like to know how i can do this.
For now I have simply tried to add the PDF file to a tab and have seen the following codes:
(Upload and view a PDF in Shiny )
(https://stackoverflow.com/questions/48194513/binary-file-read-and-write-in-shiny )
For the server:
observe({
req(input$file1)
test_file <- readBin(con=input$file1$datapath,what = 'raw',n=input$file1$size)
writeBin(test_file,'www/myreport.pdf')
})
output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src="myreport.pdf")
})
OR
observe({
raw <- readBin(input$file1$datapath, what="raw")
zz <- file("testbin", "wb")
writeBin(raw, zz)
})
output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src=zz)
})
And the Ui as:
tabPanel('Estructura de Derivados Texto', uiOutput("pdfview")),
None of these two codes seem to work for me, the first server input tells me: Warning in file(con, "wb") :cannot open file 'www/myreport.pdf': No such file or directory,
and the second one tells me that is an Error in readBin:invalid connection
.
If anyone has any ideas how i can fix it and also to later make it more complicated and only display some of the pages it would be of great help!
Thank you!
raytong
February 1, 2020, 10:24am
2
Hi @macarenadiaz . First, you have to setup a resource path by addResourcePath
to store the pdf file. Then, you can use the resource path to save and retrieve the pdf file.
library(tidyverse)
library(shiny)
ui <- fluidPage(
fileInput("file1", "File"),
uiOutput("pdfview")
)
server <- function(input, output, session) {
addResourcePath("pdf", tempdir())
test_file <- reactive({
req(input$file1$datapath)
readBin(con=input$file1$datapath,what = 'raw',n=input$file1$size)
})
observe({
temp <- paste0(resourcePaths(), "/myreport.pdf")
writeBin(test_file(), temp)
output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src="pdf/myreport.pdf")
})
})
}
shinyApp(ui, server)
Thank you for your help! For some reason now I have the following error: Warning: Error in file: invalid 'description' argument [No stack trace available]
. Would you know how to fix it? I used the exact code that is written!
raytong
February 3, 2020, 12:20pm
4
@macarenadiaz . I can't guess where the error come from. Can you share the code with error?
options(DT.options = list(scrollY="800px",scrollX="300px", pageLength = 100, autoWidth = TRUE))
ui <- dashboardPagePlus(
dashboardBody(
tabItems(
# Add two tab items, one with tabName "dashboard" and one with tabName "inputs"
tabItem(tabName= "dashboard",
navbarPage(
title = 'Opciones',
tabPanel('Estructura de Derivados Texto', uiOutput("pdfview")),
)
############################################# 1
############################################# 1
,
tabItem(tabName= "Upload",
tags$head(
tags$style(
HTML('
h3 {
font-weight: bold;
}
')
)
),
titlePanel("Carga de PDFs"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Estadistica Financiera CMF (en PDF)',
accept = c(".pdf")
),
),
mainPanel(
tableOutput('contents'),
tableOutput('contents2'))
)
)
)
)
)
# Server ------------------------------------------------------------------
server <- function(input, output, session) {
#Transformación de primer PDF a formato trabajable
txt <- reactive({
infile <- input$file1
if (is.null(infile)) {
# Usuario no ha subido el archivo
return(NULL)
}
pdf_text(infile$datapath) %>% strsplit(split = "\n") #eliminacion de \n cmo espaciado
})
##########################################################################################
addResourcePath("pdf", tempdir())
test_file <- reactive({
req(input$file1$datapath)
readBin(con=input$file1$datapath,what = 'raw',n=input$file1$size)
})
observe({
temp <- paste0(resourcePaths(), "/myreport.pdf")
writeBin(test_file(), temp)
output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src="pdf/myreport.pdf")
})
})
##########################################################################################
}
shinyApp(ui, server)
@macarenadiaz . The ui
layout cannot be run. And I cannot reproduce your error. Can you check the code that can be run and reproduce your error.
system
Closed
March 28, 2020, 6:10pm
8
This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.