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!
Related
I have an rdocx and I want to manipulate something in the xml code. That's my document:
library(officer)
doc <- read_docx() %>%
body_add_par("centered text", style = "centered") %>%
slip_in_seqfield("STYLEREF 1 \\s") %>%
slip_in_text("\u2011") %>%
slip_in_seqfield(sprintf("SEQ %s \\* ARABIC \\s 1", "Table")) %>%
slip_in_text(str_c(": ", "My Caption")) %>%
body_bookmark("my_bookmark")
With doc$doc_obj$get() I can get the xml code with classes xml_document and xml_node. Now I want to replace some code, in detail I want the part with w:bookmarkEnd to appear later so the bookmarked part gets bigger. How can I achieve this? If I could achieve this with str_replace it would be awesome.
You can use run_bookmark() as in the following example (the manual does not state that lists are supported, I'll add that info soon):
library(officer)
bkm <- run_bookmark(
bkm = "test",
list(
run_word_field(field = "SEQ tab \\* ARABIC \\s"),
ftext(" Table", prop = fp_text_lite(color = "red"))
)
)
doc <- read_docx()
doc <- body_add_fpar(
x = doc,
value = fpar(bkm)
)
# how to make a reference to the bkm
doc <- body_add_fpar(
x = doc,
value = fpar(run_reference("test"))
)
print(doc, "zz.docx")
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 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.
When exporting a flextable to *.pptx using 'officer' if the flextable has a footer it seems to corrupt the power point and it needs to be repaired.
I've tried using the other footer functions. I also tried adding a header to see if that similar code throws the error. Adding headers seem fine.
I also tried going back to officer v. 0.3.3.
Officer is v. 0.3.4
flextable is v. 0.5.4
R is 3.6.0 "Planting of a Tree"
library(officer)
library(flextable)
library(dplyr)
pdoc <- read_pptx()
footer <- "foot"
header <- "head"
myft <- iris %>% head() %>% flextable()
myft <- myft %>% add_header_lines( values = header)
pdoc <- pdoc %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_flextable(myft , type = "body")
print(pdoc, "test1passes.pptx")
myft <- myft %>% add_footer_lines( values = footer)
pdoc <- pdoc %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_flextable(myft , type = "body")
print(pdoc, "test2fails.pptx")
I can open the "test1passes.pptx" file with no problem.
When I open the "test2fails.pptx" I get an error from pptx saying the file is corrupted when I expected that it should open with no problem.
Using reportRs pacakge, I'm trying to add several graphs(.png/.jpg) which are named as e.g. test-0,test-1,test-2 etc into a pptx file. These graphs have been extracted from a pdf named e.g. test using im.convert function.I can add them individually but not able to automate the code for graphs,title, slide number, date etc in loop which can figure out how many graphs with 'test' name are there in a folder and then import them in the pptx one by one in a new slide ata time and one final pptx file.
sample code:
library(animation)
im.convert("Test.pdf", output = "Test.png", extra.opts="-density 150")
library("ReporteRs")
doc <- pptx()
doc <- pptx(template = templateDir)
doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
doc <- addTitle(doc, paste("Test-0"))
doc <- addImage(doc, "Test-0.png")
:
:
:
:
doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
doc <- addTitle(doc, paste("Test-3"))`enter code here`
doc <- addImage(doc, "Test-3.png")
You could try using the list.files function to find the number of png files with the name Test in a folder.
sample code:
list_of_files=list.files(path = "C:/output_folder", pattern = c("Test",".png"))
library("ReporteRs")
doc <- pptx()
doc <- pptx(template = templateDir)
for( i in 0:(length(list_of_files)-1))
{
doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
doc <- addTitle(doc, paste0("Test-",i))
doc <- addImage(doc, paste0("Test-",i,".png"))
}
You could also try the eoffice package:
install.package("eoffice")
fig<-infigure("figes",savegg=T)
topptx(fig,file="test.pptx")
##or
infigure("figs",showfig=T)
topptx(fig,file="test.pptx")