Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening PDF within R studio using file.show

Tags:

r

pdf

I am making making an R tutorial that runs in R within Swirl. I am trying open specific PDF files within R. I am using:

file.show(paste(getwd(),"/cv.pdf",sep=""), title="some title")

But the display is like this:

Output in R-studio

It does not show PDF file. Works well for TXT file.

I am running OSX 10.11.1. Default PDF viewer is "Preview" and I do not have Adobe Reader installed. Is there a way I can have PDF file opened up through an R script?

like image 705
DocZhivago Avatar asked Nov 18 '15 22:11

DocZhivago


People also ask

How do I read a PDF in R?

You can name the function whatever you like e.g Rpdf. The readPDF function has a control argument which we use to pass options to our PDF extraction engine. This has to be in the form of a list, so we wrap our options in the list function. There are two control parameters for the xpdf engine: info and text.

What does PDF () do in R?

pdf() opens the file file and the PDF commands needed to plot any graphics requested are sent to that file.


2 Answers

file.show() is only designed to open text files. If you'd like to open PDFs and you know which platform you'll be deploying the script on—not a problem if it's just your OS X machine, but will you be sharing this tutorial?—you can use system2() to run any command the shell can, including Preview.app.

To open a PDF in your OS X system's default PDF viewer:

system2('open', args = 'myfile.pdf', wait = FALSE)

To open a PDF specifically in Preview:

system2('open', args = c('-a Preview.app', 'myfile.pdf'), wait = FALSE)

Note that you'll need to give a full path, rather than just a file name, if you're executing the script from a different directory to your PDF.

like image 198
jimjamslam Avatar answered Oct 28 '22 16:10

jimjamslam


R command to open pdf file on specified page with specified zoom with Foxit Reader in Windows 10 :

prog = "C:\\Program Files\\...\\FoxitReader.exe"
# or
prog = r"(C:\Program Files\...\FoxitReader.exe)"

fpath = r"(E:\...\myfile.pdf)"

loc = paste0('/A zoom=106% page=', as.character(13))

system(paste(shQuote(prog, type='cmd'), shQuote(fpath, type='cmd'), noquote(loc)))
like image 31
rahul-ahuja Avatar answered Oct 28 '22 15:10

rahul-ahuja