Exporting html output to latex, .png or .doc - r

I am doin some cross tabulation with the sjPlot package that produces wonderful tables in HTML.
library(sjPlot)
iris<-iris
tab_xtab(iris$Species,iris$Sepal.Width,show.row.prc = TRUE,show.col.prc = TRUE)
While the output looks great in the consol, I would like to export it and put them in a report. Ideally, I would like to export them into a latex file, but also as a .png as a .doc file would be ok.
Does anyone know how I could do this?
thanks a lot for your help
Best

One option is to use webshot::webshot():
library(sjPlot)
library(webshot)
# location to write html version to
my_html <- tempfile(fileext = ".html")
# write html to temp file
tab_xtab(iris$Species,
iris$Sepal.Width,
show.row.prc = TRUE,
show.col.prc = TRUE,
file = my_html)
# location to write png version to
my_png <- tempfile(fileext = ".png")
# take a webshot of html and save to png
webshot::webshot(my_html, my_png, vheight = 300)
Note, you will likely have to install PhantomJS after installing the webshot package. This can be done with webshot::install_phantomjs().
You may also have to play around with the vwidth and vheight arguments to avoid extra white-space in your png.

Related

magick image library writing incorrect header to PDF files?

I am using the very useful magick library to read and annotate PDF files, and overlay an image on the result. I can generate a PDF file that looks as I would expect it to look. However, when I open the file, the header, which I would expect to read something like %PDF-1.7, reads ‰PNG like this.
It looks to me as if magick is looking at the most recent operation, which is image_composite for a PNG file, and using this for the header. If so, is this a bug? The PDF file that is output appears otherwise well-formed, so it doesn't seem to be causing problems, but I am curious. The following code should enable the issue to be reproduced.
require(magick)
require(pdftools)
pdf_file <- "https://web.archive.org/web/20140624182842/http://www.gnupdf.org/images/d/db/Hello.pdf"
image_file <- "https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/PDF_file_icon.svg/200px-PDF_file_icon.svg.png"
my_image <- image_read(image_file,density = 300)
pdfimage <- image_read_pdf(pdf_file,density = 300)
pdfimage2 <- image_annotate(pdfimage, "test",
location = "+400+700", style = "normal", weight = 400,
size=42)
pdfimage3 <- image_composite(pdfimage2,my_image,operator="atop",
offset = "+100+100")
image_write(pdfimage3, path = "C:/temp/test.pdf", density = 300, flatten = TRUE)
I have held off from answering this because the solution is embarrassingly obvious. In retrospect, I just assumed that, because I used image_read_pdf it should and would save in PDF format. What I needed to do was specify it explicitly. Adding a format = "pdf" argument to the image_write call achieved that.
image_write(pdfimage3, path = "C:/temp/test.pdf", density = 300, format = "pdf", flatten = TRUE)
This results in a well-formed PDF. Problem solved. Lesson learned.

Saving pptx as pdf in R

I have created powerpoint files using officer package and I would also like to save them as pdf from R (dont want to manualy open and save as pdf each file). Is this possible?
you can save the powerpoint object edited using the code which is posted here: create pdf in addition to word docx using officer.
You will need to first install pdftools and libreoffice
library(pdftools)
office_shot <- function( file, wd = getwd() ){
cmd_ <- sprintf(
"/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir %s %s",
wd, file )
system(cmd_)
pdf_file <- gsub("\\.(docx|pptx)$", ".pdf", basename(file))
pdf_file
}
office_shot(file = "your_presentation.pptx")
Note that the author of the officer package is the one who referred someone to this response.
Note that the answer from Corey Pembleton has the LibreOffice iOS path. (Which I personally didn't initially notice). The Windows path would be something like "C:/Program Files/LibreOffice/program/soffice.exe".
Since the initial answer provided by Corey, an example using docxtractr::convert_to_pdf can now be found here.
The package and function are the ones John M commented in Corey initial answer.
An easy solution to this question is to use convert_to_pdf function from docxtractr package. Note: this solution requires to download LibreOffice from here. I used the following order.
First, I need to set the path to LibreOffice and soffice.exe
library(docxtractr)
set_libreoffice_path("C:/Program Files/LibreOffice/program/soffice.exe")
Second, I set the path of the PowerPoint document I want to convert to pdf.
pptx_path <- "G:/My Drive/Courses/Aysem/Certifications/September17_Part2.pptx"
Third, convert it using convert_to_pdf function.
pdf <- convert_to_pdf(pptx_path, pdf_file = tempfile(fileext = ".pdf"))
Be careful here. The converted pdf file is saved in a local temporary folder. Here is mine to give you an idea. Just go and copy it from the temporary folder.
"C:\\Users\\MEHMET~1\\AppData\\Local\\Temp\\RtmpqAaudc\\file3eec51d77d18.pdf"
EDIT: A quick solution to find where the converted pdf is saved. Just replace the third step with the following line of code. You can set the path where you want to save. You don't need to look for the weird local temp folder.
pdf <- convert_to_pdf(pptx_path, pdf_file = sub("[.]pptx", ".pdf", pptx_path))

Inserting an image or pdf into a word document in R

Im working with a loop that creates many tables etc. and exports it into word documents with ReporteRs package. So for example I then have a word document with many pages of different graphs, tables and text.
I want to insert an image (or pdf - either is fine) into it through the loop (since the loop produces many different word documents). I have downloaded the ImageMagick and magick packages to work with the images. Now I have my image in R, but I cant figure out how to add it to my document.
I know that ReporteRs has an addImage command that inserts external images (honestly im having trouble figuring that one out to). Is it possible adding internal images/pdf's to a document?
Hope you guys can gives me some tips. Thank you in advance!
I strongly recommand to migrate your code to officer as ReporteRs will be removed from CRAN on 2018-07-16. From the code #d125q wrote, this would be transformed as :
library(officer)
library(magick)
download.file("https://jeroen.github.io/images/frink.png", "frink.png")
dims1 <- attributes(png::readPNG("frink.png"))$dim/72
sample.image <- image_read("frink.png")
image_write(image_rotate(sample.image, 45), "frink_rotated.png")
dims2 <- attributes(png::readPNG("frink_rotated.png"))$dim/72
sample.doc <- read_docx()
sample.doc <- body_add_img(sample.doc, src = "frink.png", width = dims1[2], height = dims1[1] )
sample.doc <- body_add_img(sample.doc, src = "frink_rotated.png", width = dims2[2], height = dims2[1] )
print(sample.doc, target = "sample.docx")
You can plot images from magick to add them to a document using ReporteRs. Here's an example:
library(ReporteRs)
library(magick)
sample.doc <- docx(title="Sample")
## add original Frink
sample.image <- image_read("https://jeroen.github.io/images/frink.png")
sample.doc <- addPlot(sample.doc,
fun=plot,
x=sample.image)
## add rotated Frink
sample.doc <- addPlot(sample.doc,
fun=function(x) plot(image_rotate(x, 45)),
x=sample.image)
## save the document to disk
writeDoc(sample.doc, "sample.docx")
If anyone is wondering about this with the new officer. I needed to insert a pdf into my document. I converted the pdf to a picture. After migrating to officer, i ended up simply using this code from the officer package:
img.file <- file.path( R.home("doc201"), "P:/path to my picture", "name.png" )
doc201 <- body_add_img(x = doc201, src = "P:/path/name.png", height = 10, width = 6, pos = "after" )
The other answers worked too, but after i got used to officer this was the most simple way for me. Hope this is of help in the future! :)

When using servr::jekyll() on R, where should I save my Rmd files?

With the Rmd files on root (eg. on my /knitr-jekyll/) they are turning into md files, but not on html files. Thus, they appear as simple markdown text.
I tried to put them on /_source and on /_posts but it get's worse, in this case I also don't get the md files.
I found creating a separate folder all together solves the problem.
/kintr-jekyll/_rmd/test.Rmd
But do remember that when you knit your Rmd to md that you knit to the _post folder if you using the standard bootstrap template. Also make sure that you specified your figure output. Easiest is to write some function which does this for you:
KnitPost <- function(input, base.url = "/") {
require(knitr)
opts_knit$set(base.url = base.url)
fig.path <- paste0("figures/", sub(".Rmd$", "", basename(input)), "/")
opts_chunk$set(fig.path = fig.path)
opts_chunk$set(fig.cap = "center")
render_jekyll()
knit(input, envir = parent.frame())
Lastly, go make sure in your .md file in knitr-jekyll/_post that the figures are clearly referenced. This should be within your test.md:
<img align="middle" src="/figures/test/test_image.jpg">
This link might help: R-Bloggers post about jekyll

Saving graphs in both PDF and PNG format but using PDF files in the final document

I'm using knitr for my analysis. I can save graphs in PDF format with \SweaveOpts{dev=pdf} and in PNG format with \SweaveOpts{dev=png}. I'm interested to save graphs both in PDF and PNG format in one run but to use the PDF in the final documents interactively.
How can I do this?
Here comes the real solution:
Knitr 0.3.9 starts to support multiple devices per chunk (for now, you have to install from GitHub); in your case, you can set the chunk option dev=c('pdf', 'png') to get both PDF and PNG files.
Here is a solution that uses ImageMagick to convert PDF files to PNG. Of course you have to install ImageMagick first, and make sure its bin directory is in PATH:
knit_hooks$set(convert = function(before, options, envir) {
# quit if before a chunk or no figures in this chunk
if (before || (n <- options$fig.num) == 0L) return()
# only convert pdf files
if (options$fig.ext != 'pdf') return()
# use ImageMagick to convert all pdf to png
name = fig_path() # figure filename
owd = setwd(dirname(name)); on.exit(setwd(owd))
files = paste(basename(name), if (n == 1L) '' else seq(n), sep = '')
lapply(files, function(f) {
system(sprintf('convert %s.pdf %s.png', f, f))
})
NULL
})
Basically this hook is executed after a chunk and run convert foo.pdf foo.png on all PDF figures. You can use it like
<<test-png, convert=TRUE>>=
plot(1); plot(2)
#
Or if you put all figures in a separate directory, you can run convert directly in that directory (i.e. do not have to call system() in R).
This is not an ideal solution but should work. To make use of R's native png() device, you need to answer my question in the above comment first.

Resources