Reputation: 3739
I have an image that I want to use as a custom legend - a universal legend of 3 plots in one figure combined with grid.arrange:
I figure I can load this into R with png or some such package and use grid.arrange to place it next to my graphs. Is it possible to include such images in the package that already contains the functions to make my combined chart, and if so where does one put it and how would you call it from within R code then from it's location within the package. I intend to use this sort of graph with this legend often.
UPDATE:
I've created a folder called 'img' in the root of my Package structure and placed the file inside.
In my R code is the line:
legend <- readPNG(system.file("img", "rgblegend.png", package="HybRIDS"), TRUE)
However when build the binary, then install it from local zip file, I go to plot and:
Error in readPNG(system.file("img", "rgblegend.png", package = "HybRIDS"), :
unable to open
If I check my library there is no folder 'img'
EDIT:
I've checked a source version of my package - it includes the 'img' folder, however installing from source also fails to put the folder into my directory. So I guess my question now is, why does making a binary package not include the img folder, and why does the source package include it but not install it to my library - what do I do to correct this?
Upvotes: 1
Views: 1579
Reputation: 58845
agstudy's answer describes how to reference a file in an installed package without having to know where the package was installed. The other part of the problem is getting the image file into the installed package.
From the "Package subdirectories" second of Writing R Extensions, the section of interest is about the inst
subdirectory:
The contents of the
inst
subdirectory will be copied recursively to the installation directory. Subdirectories ofinst
should not interfere with those used by R (currently,R
,data
,demo
,exec
,libs
,man
,help
,html
andMeta
, and earlier versions usedlatex
,R-ex
). The copying of theinst
happens aftersrc
is built so itsMakefile
can create files to be installed.
So for a file to appear in the img
directory off the root of the installed package, it must appear in the inst/img
directory of the source. So move rgblegend.png
from img
to inst/img
and then
legend <- readPNG(system.file("img", "rgblegend.png", package="HybRIDS"), TRUE)
should work.
Upvotes: 2
Reputation: 121588
If I understand the question is how you can be sure to find the image within the package. You can put the png under a folder img
of your package. and call it using something like this :
img <- readPNG(system.file("img", "Zwrch.png", package=package_name))
The complete solution looks to something like this:
library(ggplot2)
library(grid) ## to create the image grobe
library(png) ## to read the png
library(gridExtra) ## to arrange the plots
bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
img <- readPNG(system.file("img", "Zwrch.png", package=package_name))
legend <- rasterGrob(image=img)
grid.arrange(bp,legend)
Upvotes: 1