I want to add a graphic in the form of a pdf to a power point file created by officer. The following code works, in the sense that the powerpoint is created and the pdf is included. But to get to the powerpoint you have to let it repair itself. Here's an example
library(officer)
library(magrittr)
my_pres <- read_pptx()
testpdf <- "test.pdf"
extImgObs_pdf <- external_img(src = testpdf)
fp_3 <- fp_text(italic = TRUE, color = "black", font.size = 14)
my_pres <- add_slide(x = my_pres, layout = 'Title Only', master = 'Office Theme')
titleTextObserved <- "some text for the title field"
my_pres <- ph_with( x = my_pres, value = fpar(ftext(titleTextObserved, fp_3)), location = ph_location_type(type = "title") )
my_pres <- ph_with( x = my_pres, value = extImgObs_pdf, location = ph_location(left = 0, top = 1.2) )
print(my_pres, target = "presentations/cmip6/chillHours_Ensemble.pptx") %>% browseURL()
With some arbitrary test.pdf file (I've generated it with the pdf driver in R and removed whitespace with the linux pdfcrop program), the above code generates a powerpoint. If you click the Repair ok message the powerpoint shows up as expected. But when you click Cancel rather than Repair, Powerpoint returns "Sorry, PowerPoint can't read ^O." Is there any way to remove the Repair message other than by manually clicking Repair?
Clicking repair makes the pdf image visible locally but the ppt doesn't show the graphic for others. Need to use something other than pdf for the graphics. png, tiff, bmp and jpeg are options.
Related
library(tidyverse)
library(easyalluvial) # https://github.com/erblast/easyalluvial
library(parcats) # https://github.com/erblast/parcats
# My data
knitr::kable(head(mtcars2))
# My Alluvial
MyAlluvial <- alluvial_wide(data = mtcars2,
max_variables = 5,
fill_by = 'first_variable')
# My Nice alluvial
p <- parcats(MyAlluvial, marginal_histograms = FALSE, data = mtcars2)
p
# Saving PDF
pdf("/Users/Master/Downloads/MyAlluvial.pdf")
p
dev.off()
I'm able to save the plot as png/jpg using RStudio GUI, but I cannot save it in a vector format (neither pdf nor eps).
As far as I known, interactive plot was generated using Plotly.
I can save printing a PDF from the browser but I don't like this!!
I was able to save your graph in a PDF file with the following code :
library(rmarkdown)
library(pagedown)
vector_RMD_Content <- c(
'---',
'title: "Untitled"',
'output: html_document',
'---',
'```{r setup, include=FALSE}',
'knitr::opts_chunk$set(echo = TRUE)',
'```',
'```{r cars}',
'library(tidyverse)',
'library(easyalluvial)',
'library(parcats)',
"MyAlluvial <- alluvial_wide(data = mtcars2, max_variables = 5, fill_by = 'first_variable')",
'p <- parcats(MyAlluvial, marginal_histograms = FALSE, data = mtcars2)',
'p',
'```')
zzfil <- tempfile(fileext = ".Rmd")
writeLines(text = vector_RMD_Content, con = zzfil)
render(input = zzfil,
output_file = "C:/stackoverflow.html")
chrome_print("C:/stackoverflow.html",
output = "C:/testpdf2.pdf")
A html file with your graph is generated with Rmarkdown. After, the HTML file is printed to PDF with the R function chrome_print of the R package pagedown.
i'm trying to create a ggplot editable with ppt or word
Here's the code:
library(tidyverse)
library(rvg)
library(officer)
##I create a dml object
anyplot = dml(code = barplot(1:5, col = 2:6), bg = "wheat")
## add slide
doc <- read_pptx()
##specify object and location of object
doc <- add_slide(doc, "Title and Content", "Office Theme")
doc <- ph_with.dml(doc, anyplot, location = ph_location_fullsize())
## print it in pptx
print(doc, target = 'plot.pptx')
But I keep getting could not find function "ph_with.dml"
note: I understand ph_with.dml is the new version of ph_with_vg (which cannot be find either)
Any suggestion or different approach would be appreciated!
I have an RShiny application that generates a network plot using the diagrammeR package:
blc3001 <- eventReactive(input$datapath,{
data_blc <- readxl::read_xlsx(input$datapath$datapath)
blc3001 <- "digraph test{<TONS OF GRAPH CODE>"
})
I can output to Shiny using:
output$blc3001 <- renderGrViz({
grViz(blc3001())
})
And I can download as a .png using:
output$d_blc3001 <- downloadHandler(
filename = "blc3001.png",
content = function(file) {
grViz(blc3001()) %>% export_svg %>% charToRaw %>% rsvg_png(file)
},
contentType = "image/png"
)
Now I am trying to download this graph into a powerpoint slide, but am running into difficulty. I am open to any method or package, but I am currently trying to do this via the "officer" package.
output$blc3001_powerpoint <- downloadHandler(
filename = function() {
"blc3001.pptx"
},
content = function(file) {
image <- grViz(blc3001()) %>% export_svg %>% charToRaw %>% rsvg_png()
ppt_report <- read_pptx() %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = image, location = ph_location_type(type = "body"))
print(ppt_report, target = file)
}
)
Although I can download the png, when I define ppt_report, I get error:
Error in UseMethod("ph_with", value) :
no applicable method for 'ph_with' applied to an object of class "raw"
I assume this is because I specify content type is img/PNG in the image download handler, but not the powerpoint download handler. I can download the powerpoint without error if I remove "%>% charToRaw %>% rsvg_png()" from defining image, but the powerpoint slide contains the XML code rather than rendering the actual svg. If anyone can please provide any tips on how to proceed I would greatly appreciate it! Thanks!
I'm looking for tidy way to add a hyperlink incorporating a fontawesome icon to an Rmarkdown table (kable) — for incorporation in an html bookdown page.
In other parts of my document, I've used the icon package, to render a hyperlinked fontawesome icon (outside of a table) using standard markdown syntax, e.g.:
`r icon::fa("file-pdf", size = 5)](https://www.google.com/){target="_blank"}`
But this approach doesn't work when I've attempted to incorporate it as part of a kable.
```{r}
library(icon)
library(knitr)
library(tidyverse)
## note this code throws the following error: Error in
## as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors =
## stringsAsFactors) : cannot coerce class "c("knit_asis",
## "knit_icon")" to a data.frame
link_location <- "www.google.com"
data_test_1 <- data.frame(
file = c('Version 1', 'Version 2', 'Version 3'),
last_updated = Sys.Date(),
pdf_logo = icon::fa("file-pdf")) %>%
mutate(pdf_logo = cell_spec(pdf_logo,
link = link_location)) %>%
kable("html", escape = F, align = "c")
data_test_1
```
So far I've come up with a workaround that involves downloading the .svg file from the fontawesome website and adding it as an image. It works... sort of, but I would prefer to have the ability to change the size of the icon and make it more easily reproducible.
This is the code for my current workaround.
```{r fontawesome_table ='asis'}
library(tidyverse)
library(kableExtra)
## download svg from location manually
https://fontawesome.com/icons/r-project?style=brands
data_test_2 <- data.frame(
file = c('Version 1', 'Version 2', 'Version 3'),
last_updated = Sys.Date(),
R_logo = "![](r-project-brands.svg)") %>%
mutate(R_logo = cell_spec(R_logo, link = "https://cran.r-
project.org/")) %>%
kable("html", escape = F, align = "c")
data_test_2
```
Which produces this output...
Does anyone have any ideas for how I could either, adjust the size the icon in the table, or call the icon from another package/css to create a more tidy solution?
Here is a way using the fontawesome package instead. I also had to use a custom link building function:
```{r, echo = F, message=F, warning=F}
library(fontawesome)
library(knitr)
library(tidyverse)
library(kableExtra)
## note this code throws the following error: Error in
## as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors =
## stringsAsFactors) : cannot coerce class "c("knit_asis",
## "knit_icon")" to a data.frame
link_location <- "www.google.com"
addLink <- function() {
paste0("", as.character(fa("file-pdf")), "")
}
data_test_1 <- data.frame(file = c('Version 1', 'Version 2', 'Version 3'),
last_updated = Sys.Date(),
pdf_logo = addLink())
kable(data_test_1, escape = F, align = "c")
```
Another solution is the icons package. With icons you cannot only use fontawesome but several icon packages incl. Material icons, e.g.
Install it with devtools::install_github("mitchelloharawild/icons").
Download the font you want to use, here: download_fontawesome().
Now access the icon with either fontawesome("rocket", style = "solid") or icons::fontawesome$solid$rocket.
In an Rmarkdown document like this:
```{r icon-chunk}
fontawesome("rocket", style = "solid")
```
or inline like this
`r icons::fontawesome("rocket", style = "solid")`
In some cases you may need backticks. E.g. for the R icon
fontawesome$brands$`r-project`
Rookie question, I think.
I'm just trying to replicate this:
http://rpubs.com/gallery/googleVis
Open a new markdown document (in Rstudio), paste:
suppressPackageStartupMessages(library(googleVis))
T <- gvisTable(Exports, options = list(width = 200, height = 280))
G <- gvisGeoChart(Exports, locationvar = "Country", colorvar = "Profit",
options = list(width = 360, height = 280, dataMode = "regions"))
TG <- gvisMerge(T, G, horizontal = TRUE, tableOptions = "bgcolor=\"#CCCCCC\" cellspacing=10")
print(TG, "chart")
Between the ```{r} tags.
The resulting output (from the print function) is just the javascript (I think) code. It's not actually drawing the plot, in the preview window or the online version after publishing.
Is something else needed in the markdown? Like:
```{r something=something}
To get it to interpret the output from the print command properly?
Thanks
ANSWER
```{r anythingyoulike, results='asis'}
The top of this post
http://rpubs.com/gallery/googleVis really should point that out imo...
something like ```{r results='asis'}. However I don't have knitr manual with me now so you need to check.