Inserting external image into PowerPoint with officer without changing image dimensions - r

Is it possible to insert external images into a Powerpoint with officer without changing the image dimensions?
This is the code I have now where x is an rpptx object:
library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
t_file <- tempfile(pattern = "logo_", fileext = ".png")
download.file(image_url, destfile = t_file)
x <- x %>%
ph_with(value = external_img(t_file), location = ph_location(left = 9.4, top = 4.6))
This will change the dimensions of the image to 4x3 inches (the default for ph_location()).
I know you can use external R packages to get image metadata then pass this to the function so the actual image dimensions are used, but I don't want to add any more external R package dependencies.

You can always specify the image dimensions within your external_img function. I'm assuming you know what dimensions you want, so say your dimensions are width = 9, height = 5 it would look something like this:
library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
t_file <- tempfile(pattern = "logo_", fileext = ".png")
download.file(image_url, destfile = t_file)
x <- x %>%
#specify height and width
ph_with(value = external_img(t_file, width = 9, height = 5), location = ph_location(left = 9.4, top = 4.6))
Hope that is helpful!

Related

displaying ggplot .png in RStudio viewer pane with correct dimensions

I'm writing a function to save a plot to the R temp directory, then display it in the viewer pane. (Based in part on the last paragraph of this question.) If I save the plot as an .svg, it saves and shows up in the viewer pane at just the right size, based on the current pane dimensions:
library(ggplot2)
library(grDevices)
plt <- ggplot(iris) +
geom_point(aes(Sepal.Width, Sepal.Length))
dev.size()
# 5.250000 2.927083
plt_path <- tempfile(fileext = ".svg")
ggsave(plt_path, plt)
# Saving 5.25 x 2.93 in image
viewer <- getOption("viewer")
viewer(plt_path)
If I save it as a .png, however, the displayed image is way too big. (Note that ggsave() still reports saving it to the correct size.)
plt_path <- tempfile(fileext = ".png")
ggsave(plt_path, plt)
# Saving 5.25 x 2.93 in image
viewer <- getOption("viewer")
viewer(plt_path)
The issue persists using type = "cairo", and even if I manually set the ggsave dimensions:
dims <- dev.size(units = "in")
plt_path <- tempfile(fileext = ".png")
ggsave(plt_path, plt, width = dims[[1]], height = dims[[2]], units = "in")
How can I save and display the .png image to match the viewer pane dimensions?
The resolution needs to match that of your device. Default dpi is 300 in ggsave, but monitors are typically 96 dpi:
dims <- dev.size(units = "in")
plt_path <- tempfile(fileext = ".png")
ggsave(plt_path, plt, width = dims[[1]], height = dims[[2]], units = "in",
dpi = 96)
viewer <- getOption("viewer")
viewer(plt_path)

How can I embed an image on echart plot?

I was trying to embed an image on an echart plot. The image is coming perfectly if I provide a link of the image, but while I am trying to call the same image from my directory, the image is not coming in the plot.
I was tring the example provided in this website and the code I am using is as below.
library( echarts4r)
cars %>%
e_charts(speed) %>%
e_scatter(dist) %>%
e_image_g(
right = 20,
top = 20,
z = -999,
style = list(
image = "~/Rlogo.png",
width = 150,
height = 150,
opacity = .6
)
)
So how can I capture this Rlogo image from my local directory to the plot?

How can I add a baseplot with no fixed values to a document

I want to add a baseplot to a word document.
In the documentation for the officer package there's an example that uses the plot_instr function:
anyplot <- plot_instr(code = {
barplot(1:5, col = 2:6)
})
doc <- read_docx()
doc <- body_add(doc, anyplot, width = 5, height = 4)
print(doc, target = tempfile(fileext = ".docx"))
I want to add a plot to a word document inside a function so I need variable input for the plot function like this:
x=1:5
cols=2:6
anyplot <- plot_instr(code = {
barplot(x,col=cols)
})
doc <- read_docx()
doc <- body_add(doc, anyplot, width = 5, height = 4)
print(doc, target = tempfile(fileext = ".docx"))
But the code above doesn't work and I can't find any other examples of plot_instr usage.
I think I found a solution!
When I set a breakpoint before the barplot(...) call, I could see the code when body_add is called with a plot_instr wrapper function. The code creates a temporary png file. I copied this code and adapted it:
x=1:5
cols=2:6
doc <- read_docx()
file <- tempfile(fileext = ".png")
options(bitmapType = "cairo")
png(filename = file, width = 5, height = 5, units = "in", res = 300)
tryCatch({
barplot(x,col=cols)
}, finally = {
dev.off()
})
on.exit(unlink(file))
value <- external_img(src = file, width = 5, height = 5)
body_add(doc, value)
print(doc, target = tempfile(fileext = ".docx"))
The code is generating the following error
Error in barplot.default(x, col = cols) :
'height' must be a vector or a matrix
The issue here is that function body_add has an argument x and you are defining an x before the call. Changing the name to something else solves the issue:
z <- 1:5
cols=2:6
anyplot <- plot_instr(code = {
barplot(z, col = cols)
})
doc <- read_docx()
doc <- body_add(doc, anyplot, width = 5, height = 4)
print(doc, target = "temp.docx")

Save .png by webshot in R

I'm using webshot to save a png file from HTML :
p <- plot_ly(plotly::wind, r = ~r, t = ~t) %>% add_area(color = ~nms)%>%
layout(radialaxis = list(ticksuffix = "%"), orientation = 270)
saveWidget(as.widget(p), "temp.html")
webshot("temp.html",file = "temp.png",cliprect = "viewport")
This is giving me a plot like this. It returns a small image in the upper left corner. What should I do to have a full-size image in a center?

Watermark adding in R

I am using magick library in R. I want to add watermark on some pictures.
I used image_annotatefunction as below.
img <- image_read("C:\\Users\\Maydin\\Desktop\\manzara.png")
image_annotate(img, "my watermark", gravity = "northwest", location = "+200+275",
degrees = -30, size =50, font = NULL, color = "transparent",
strokecolor = "gray90", boxcolor = NULL)
At the end, the output looks like this;
However, what I want to have is something like this ,
Is that doable in magick in R?
For instance, this
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
library(magick)
img <- image_read(tf)
library(extrafont)
truetype_path <- paste0("#", subset(fonttable(), FullName=="Matura MT Script Capitals", fontfile)[1,])
image_annotate(img, "my watermark", gravity = "northwest", location = "+70+220",
degrees = -30, size = 80, font = truetype_path, color = "#FFFFFF66",
strokecolor = NULL, boxcolor = NULL)
gives this image:
I.e., choose a nice font like maybe Matura MT Script Capitals, tell image_annotate where to find it on your harddrive, adjust the opacity in the color argument - et voila. The font does not drop a shadow or show a relief, but maybe you can emulate this by plotting the text two times, the dark shadow one with a little offset to the other light one.
#lukA nicely demonstrates a solution using extrafonts package with magick package, but it looks like you can refer to the font by name within image_annotate() without the clever lookup of the full path. Use extrafonts::fonttable() to find the name.
library(extrafonts)
library(magick)
#download original example file
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
img <- image_read(tf)
#Use stroke to create an outline of the text with 50% alpha
magick::image_annotate(img, "Preview", location = "+100+175", degrees = -30, size=75, weight=700, font = "MonotypeCorsiva" , color = "transparent",
strokecolor = "#00000050", boxcolor = NULL)

Resources