Including images in R package documentation file - r

In fact, I have the same question than Teema Daniel Laajala, but the answer provided does not work for me.
The question was about displaying images in R package documention.
When I write exactly the same code than in the ?par documentation file, which is :
\item{\code{mai}}{A numerical vector of the form \code{c(bottom,
left, top, right)} which gives the margin size specified in
inches.\cr
\if{html}{\figure{mai.png}{options: width="35\%" alt="Figure: mai.png"}}
\if{latex}{\figure{mai.pdf}{options: width=7cm}}
}
the image does not display and all I can see is : imagenotdisplayed.
Though, I did the exact same code as above, put my image in the folder figures in the folder man...
So I was wondering If I was missing something, like a package I should install or something else...
Thanks for your help !

Related

Link to R6 method from separate package in help pages and pkgdown

Cross posted from: https://community.rstudio.com/t/link-to-r6-method-from-separate-package-in-help-pages-and-pkgdown/134702
I'm currently writing an R package and would like to link to the help page for an R6 method in a separate package. The page I want to link to is here: https://mc-stan.org/cmdstanr/reference/model-method-sample.html, and there is an .Rd file for the method as well (https://github.com/stan-dev/cmdstanr/blob/master/man/model-method-sample.Rd). Finally, I can also access the help page from R directly with ?cmdstanr::`model-method-sample`.
However, when I try to add a link to my own help page using the normal link to another package syntax described here, [cmdstanr::`model-method-sample`], I get this error:
Warning: Link to unknown topic: cmdstanr::`model-method-sample`
I feel like there must be some way to link to this help page, given that it definitely exists and has an .Rd page, but I haven't found a solution yet. Has anyone else run into this problem or know the solution?
I think you (or Roxygen) are using the wrong syntax for the link. According to Writing R Extensions, the Rd syntax should be:
\link[cmdstanr]{model-method-sample}
I'm not sure how to generate this from Roxygen, but it appears to work as-is if I put it in Roxygen comments.
If you want the link with different text, the syntax is
\link[cmdstanr:model-method-sample]{link text}

How do I resolve this error - Missing $ inserted?

I get the error message:
! Missing $ inserted.
<inserted text>
$
l.4 .... Graphic was created using BioRender.com.}
\label{fig:label}
and am going absolutely bonkers trying to resolve the issue. The text in question is in a figure caption, and I have checked all my math expressions preceding the 'hinted' line. All my math expressions are correctly closed with $'s; I've removed all underscores (_); and I even removed a link to a website.
Is there a way for me to at least be directed to where the problem is located in my file?
I'm using Rmarkdown, papaja::apa6_pdf.
Kind regards to anyone who can help.
EDIT
Thanks for the responses.
I've managed to narrow down the problem. Here is a sample of the chunk I'm having issues with:
{r fig-label, include = TRUE, fig.cap="**This is the title of the image.** (**A**) Explanation A with a special character alpha $\\alpha$5.(**B**) Explanation B with a supercript degree symbol 60^o^C."}
if (Sys.info()[1]=="Windows"){
knitr::include_graphics("C:\\Path\\To\\My\\Image\\File.png")
} else if (Sys.info()[1]=="Mac OS"){
knitr::include_graphics("Path To My Image/File.png")
}
The problem arises with my supercripted degree symbol. Windows can effortlessly render the image to the correct size with all the text options (boldface, special characters, etc). My Mac fails to do so, and even removing the degree symbol makes my Mac include the ** around the text I want to have as boldface. Windows can make the image the correct size, Mac prints the image way too big.
I've followed all the debugging options explained at https://yihui.org/tinytex/r/#debugging.
I very much want (need) this to work on my Mac, so can anyone give me some advice on how I can get my mac to properly knit my markdown document?
Again, kind regards to anyone/everyone who is able to help or offer advice. Let me know if you need additional information.

\code{\link{function-name}} in roxygen2

It is my first experience in writing an R-package. I used roxygen2 by following the instructions given in this link http://kbroman.org/pkg_primer/
Everythig is working fine except few things.. there could be a simpler solution to solve the issues, but I am not finding clues what I am doing wrong. I hope someone here in this blog can give a solution to solve my issues.
First issue is about {\code\link{function-name}} in roxygen2:
In .R script I inlcuded this line:
#' #seealso \code{\link{s2a}}
After documenting (generating .Rd files) there is no hyperlink to s2a ,
in documentation s2a shows like a normal text not like hyperlink..
export(s2a) is listed out in NAMESPACE.
Is there any other place i need to modify ?
Second issue is about data():
I saved the dataset in .Rdata format and placed in the data/ in package directory. I also created the .R script in R/ as like following steps here http://kbroman.org/pkg_primer/pages/data.html
In DESCRIPTION file LazyData: true .
but when I type data(shh) in R console gives a warning message
data(shh)
Warning message:
In data(shh) : data set ‘shh’ not found
Any ideas is of great help:)
It's been a while since you asked this, but I was having the exact same problem with hyperlinks in documentation not appearing correctly, so for anyone who might be having a similar problem: Are you possibly viewing the development documentation? The links don't seem to work there. (You'll know this is the case if you see Using development documentation for your_function_name in your console output when you run ?your_function_name.)
The links should appear in the non-development documentation. To generate this you can try building and reloading your package, for instance by following the steps here: http://r-pkgs.had.co.nz/man.html#man-workflow-2

Including images in R-package documentation (.Rd) files

I am working to document a visualization function in a package of mine. For this purpose I would like to have an image embedded through the Rd-file 'funcname.Rd' that could explain various parameters utilized in the function, and which would pop up if user goes to read the package manual PDF or writes '?funcname' in R. Something in the lines of:
'Writing R extensions'-document doesn't seem to address this issue, and the only references I could find were this blog post and this R-devel discussion. The former has a broken link to a package called 'base64' and the latter only addresses various alternatives like including the images in R package vignettes instead. It would be great if this could be CRAN-compatible.
Any advice on how to approach this issue would be appreciated, thanks!
Untested, but look at the R source on GitHub, it would appear they add an image to ?par documentation file in the following manner:
\item{\code{mai}}{A numerical vector of the form \code{c(bottom,
left, top, right)} which gives the margin size specified in
inches.\cr
\if{html}{\figure{mai.png}{options: width="35\%" alt="Figure: mai.png"}}
\if{latex}{\figure{mai.pdf}{options: width=7cm}}
}
The figure appears to be saved in /man/figures.
For more details see the Writing R Extensions section on the topic.

R & Inkscape: text labels in SVG graphics exported from R did not recognized as a text in Inkscape

I constructed dendrogram in R with the code:
data(iris)
aver<-sapply(iris[,-5],function(x) by(x,iris$Species,mean))
matrix<-dist(aver)
clust<-hclust((matrix),"ave")
clust$labels<-row.names(aver)
plot(as.dendrogram(clust))
I wanted to save the dendrogram as svg file using the code:
install.packages("Cairo")
library(Cairo)
svg("plot.svg")
plot(as.dendrogram(clust))
dev.off()
Here the problem started:
When I imported the "plot.svg" into Inkscape (ver: 0.48.4) and selected any label (e.g. "setosa") it was not recognized as a text, but rather as some "user defined" object. Specifically, when I selected any "letter" in the label and inspect it with the XML Editor (ctrl+shift+X) in Inkscape I obtained this information:
**id**: use117
**x**: 142.527344
**xlink:href**: #glyph0-8
**y**: 442.589844
On the other hand, when I manually wrote "setosa" using "create and edit text objects" tool, and inspected in XML Editor, it returned:
**id**: text4274
**sodipodi:linespacing**: 125%
**style**: font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Palatino Linotype;-inkscape-font-specification:Palatino Linotype
**transform**: scale(0.8,0.8)
**x**: 176.02016
**xml:space**: preserve
**y**: 596.96674
It is likely that Inkscape did not recognize the labels as a text according to the attribute "id" from XML Editor. Hence, I am not able to change neither font, size as well as use other functions related to text objects in Inkscape.
Here is the svg file, that I made with the previous code and imported into Inkscape
I checked previous steps using other versions of Inkscape as well as R, but it would be the same.
Here is the question:
Do you have any suggestion how I can gather labels as a text attribute instead of a "user defined" (or whatever it is object...) when importing svg files from R into Inkscape?
UPDATE
#baptiste linked to the SO thread where #Oscar Perpiñán suggested three packages (gridSVG, SVGAnnotation and RSVGTipsDevice) that manipulate SVG. Unfortunately, neither of packages suggested could solve the problem with the text issue.
So far I found SO thread where #Mo Sander suggested RSvgDevice package since it can preserve text object rather than glyphs. Being stuck with the RSvgDevice installation procedure, I found that it RSvgDevice is only available for 32-bit installations and R < 2.15.0. Otherwise, R returned warning message:
Warning message:
package ‘RSvgDevice’ is not available (for R version 3.0.1)
Beside the requirements for older R versions, currently only RSvgDevice can preserve a text object in SVG.
I'm a bit late to the party, but I've been dealing with this myself. I found a trick to make it work. First, I export the plot as PDF instead of SVG because PDF fonts are recognized by inkscape.
This, however brings a new problem as the text often ends up being defined letter by letter meaning that you can change the font, but the spacing is still defined and it becomes immensely annoying. I found that it was due to the x coordinate being defined at each letter.
I wrote a perl script and put it in this gist to remove all the trailing coordinates. After that I'm able to manipulate all the fonts I wished. Note, that this will only work for horizontal text.
Hope that helps this problem you had over a year ago :)
This is a failing in Cairo. Major, from my point of view.
The cairo SVG surface (i.e. the back-end in Cairo used to "draw" on SVG) simply does not support the "text" tag. It does not understand about strings at all. Instead, it places each character (glyph) individually. So any SVG generated with Cairo is not useful if you want to post-process contained text with a vector editor. :(
The only mention I found on the cairo list was this one:
http://lists.cairographics.org/archives/cairo/2011-February/021777.html
The svglite package exports text on Linux as desired.
[EDIT] According to this thread, there is also a way to remove the squeezing of the edited text into the fixed box width. Just remove the textLength field from the object in the XML editor.
Cheers
Can't directly comment on mgrewe answer because of my low reputation but thank you for the solution.
Implemented the textLength edit into R:
svgitem<-readLines('file.svg')
svgitem<-gsub('textLength=','tL=',svgitem)
writeLines(svgitem,'without_textLength.svg')
Text-box seems to be no longer affected after edition in Inkscape using the without_textLength.svg file and keeps a trace of old textLength renamed 'tL'.
Thanks again mgrewe, I've lost so many hours reformating text in Inkscape before seeing your answer.
R is clearly not using the standard SVG text objects for producing its labels. I have no idea why. I am not an R user.
Perhaps by default it uses it's own custom font that it manually inserts glyph-by-glyph into the output. Are you using the same font in both cases? In Inkscape you are using Palatino. Is that what you are using for the labels in R?

Resources