script to add links to all images in markdown file - r

I have a workflow where I'm downloading Google Docs as docx, then processing them to markdown for export to other formats in the R environment.
When I convert to markdown with pandoc_convert, I get embedded images, but they're not linked.
I want to add link syntax similar to this post,
i.e. I want this (not linked):
![m'lady](https://i.imgur.com/v8IVDka.jpg)
to be (linked):
[![m'lady](https://i.imgur.com/v8IVDka.jpg)](https://i.imgur.com/v8IVDka.jpg)
for every image in a document. How to do?

After much toil, I put this function together, which solves my issue:
addLinksToPhotos<-function(mdfile){
d<-readLines(mdfile)
s<-sapply(d,function(x) {
replacementPattern="[\\1(\\2)](\\2)\\3"
gsub('.*?(?<firstpart>!\\[[^\\]]*?\\])\\((?<filename>.*)\\)(?<potentialHTML>\\{.*?\\})?.*?',x=x,replacement=replacementPattern,perl=T)
},USE.NAMES = F)
writeLines(s,mdfile)
}
#usage
addLinksToPhotos("myRMarkdownDoc.rmd")
It will read every line in the markdown file, looking for the photo ![]() pattern, including where it has style coding in braces ![](){}. It will modify each instance to have a linked image [![]()](){}.

Related

Inserting images in R markdown using a path variable

I am new to R and Rmd and trying to generate a report using Rmd. This report has several images inserted along with the text. I am able to insert an image by hardcoding the path of the image. I have no problems with that but I need the path as a variable because it varies with the project. Can anyone help me with the syntax for calling a variable within a path to the image?
![Relatedness check](/data/array_processing/in_progress/Project123/files/data/plots/Project123.ibd.png)
"Project123" changes based on the project. Is there a way I can declare this variable and call it to define the path?
Help please.
Images can use online R code for dynamic paths and/or alt text. (Early adopters of rmarkdown often tried this method as the default method of including R plots in the reports, using png(filepath...); plot(...); dev.off() followed by what I recommend you use.)
This will allow you to do what you need:
![something meaningful](`r filepath`)
as raw markdown (and not inside a traditional code chunk).
If you aren't familiar with inline code blocks, then know that you can put just about anything in an inline code block. This is handy for including dynamic content in a paragraph of text, for example "the variance of the sample is \r var(sample(99))``". (Often it is just a pre-created variable, if numeric it is often rounded or formated to control the display of significant figures.)

Rmarkdown Links - How to get the name of the current file without extension?

I am creating a bookdown document in which I provide a link for people to download the PDF, DOCX and TEX outputs of the current section they are looking at.
All output documents are in a folder named "Compilation" and have the same name of the original Rmd documents they were retrieved.
For exemple, I have the file "1.2-Endowment-effect.Rmd" in which I coded:
Download Links: [[PDF]](./Compilation/1.2-Endowment-effect.pdf)
[[DOCX]](./Compilation/1.2-Endowment-effect.docx)
[[TEX]](./Compilation/1.2-Endowment-effect.tex)
I wanted to know if, instead of writting "1.2-Endowment-effect" in the code, there would be a way to take the name of the current Rmd file and add the extension. Something like:
Download Links: [[PDF]](./Compilation/NameCurrentFile.pdf)
[[DOCX]](./Compilation/NameCurrentFile.docx)
[[TEX]](./Compilation/NameCurrentFile.tex)
I have to repeat that process for lots of Rmd files, and I want to avoid always updating the links.
Thank you for your help.
The output filenames can be generated with inline R expressions, e.g.,
Download Links: [PDF](./Compilation/`r xfun::with_ext(knitr::current_input(), 'pdf'`)
Please read the help pages of the functions xfun::with_ext() and knitr::current_input() if you are not familiar with them.
To include this part in all other Rmd documents, you can put the content in a child document named, say, _download.Rmd, and in your main Rmd documents, use
```{r, child='_download.Rmd'}
```

Changing keywords to highlight in an RMarkdown document

I have been writing a document in bookdown where within the *.Rmd file I call a figure by using the following syntax
\#ref(fig:MyFigureName)
This differs slightly from the notation that you would use in a normal RMarkdown file exporting to latex which would be
\ref{fig:MyFigureName}
The issue I am running into is that when I write something in bookdown the function calling the Figure is not being highlighted properly (see image below).
I have imported my own rsTheme (which is from my understanding, basically a .css file) but I don't see an option to add keywords to highlight.
But I would like for the entire function to be colored differently from the inline text (Photoshop version of desired output shown below)
Does anyone know how I would edit my *rsTheme file in order to accomplish this?
Thanks!

docfx Shared Content between Markdown Files

In DocFX, I have a block of content which i need to put on all (or most) markdown files. Is there a way to merge or pull the contents of another md file in to the current file? Basically i am trying not to repeat myself and looking for some sort of sharing mechanism.
You can do this with file inclusion which is part of the DocFX flavored markdown (DFM).
DFM adds syntax to include other file parts into current file, the
included file will also be considered as in DFM syntax.

how to embed images (data URI scheme) using .rhml files and knitr?

When converting markdown to html the default is (I think) to convert an image file into a string and embed it into the html file. When running knit on an rhtml file this is not the case though. Here a separate figure folder is generated, which is of course a sensible default setting.
But if I want my images to be embedded, is there a way to achieve this using rthml and knitr as well? I can't find any options where to declare this.
Thanks, Mark
Alright, I figured out myself. Seems to work just the same as with .Rmd files, by simply passing the string "base64_images"to the options argument in knit2html.
knit2html("foo.Rhtml", options=c("base64_images"))

Resources