how to hyperlink an image in R Markdown - r

Based upon my understanding of markdown syntax, this is how you add a hyperlink to an embedded image, yet this does not seem to work using R Markdown. What am I doing wrong?
[![alt text][local path to saved image]][web link]

Use this way: ![Alt text](Web link or path)
Examples: ![Image name](img/image.png) or
![Image name](http://www.host.com/image.png)
Combining with the normal syntax for a hyperlink [text](URL), we have:
[![Alt text](Web link or path)](web link to website)
Example:
[![Image name](image.png)](http://www.host.com/link.html)

The following worked for me knitted to HTML. The code references an image from a local data store "./data/appStore.png" The image links to the app store url "https://itunes.apple.com/us/app/my-toy-box/id1217665205?mt=8". When hovering over the image the user sees, "My Toy Box on App Store". The alt text "MyToyBox" doesn't appear.
[![MyToyBox](./data/appStore.png "My Toy Box on App Store")](https://itunes.apple.com/us/app/my-toy-box/id1217665205?mt=8)

I was looking the same and <a href = "https://the link"> ![](images/logo.png) works as well.

Related

kable table not correctly displayed on github

I am working on a color package for the company I work for. It contains a table to see the color of the company in an interactive way, with the actual color under the hex code (see picture below).
This works fine as long as I compile my rmd locally. However, when the file is compiled on Github, the underlying color disappears (see https://github.com/qwertzlbry/bsscol or picture below).
Does anyone know why Github is not displaying my table correctly?
The entire rmd file including all codes can be found at https://github.com/qwertzlbry/bsscol, any help on this topic is greatly appreciated.
It appears the differences are due to two different renders. From what I can see, kable_paper outputs a Markdown text-document. When you are viewing it, something is rendering it into HTML, where the <span>-tag is allowed.
In the second screenshot, on GitHub, it is GitHub that is rendering the Markdown document into HTML. Alas, the GitHub Flavored Markdown does not support the <span> tag. So it simply ignores it.

How to do internal links in Google Colab

I would like to make a reference list with the sections of my Colab notebook. The notebook is saved in my Google Drive.
I am trying HTML and Markdown hyperlinks, which work fine if the link is to an HTTP URL, but it is not working for internal Sections in the notebook.
For example, in a text cell I set the outline:
1. [Section 1](#s1)
2. [Section 2](#s2)
and in the destination section:
<a id='s1'></a>
#Section 1
.....
<a id='s2'></a>
#Section 2
The hyperlink in the list of the outline it is showed as a hyperlink but when I click on it or it does not do anything, or it opens a new tab in the browser with an error message:
Colab creates its own Content list using the markdown sections and subsections but internal links from one section to another are not possible.
With Colab you have to insert an <a name="id"></a> tag in the cell you want to link to.
The link works like normal:
[Link Text](#cell-id)
And the destination cell with the tag would be:
<a name="cell-id"></a>
# Heading
This is the cell I'm linking to
If you look close to your current colab url
https://colab.research.google.com/drive/BLAHBLAH?authuser=SOMENUMBER#scrollTo=BLOCKADDRESS
Every block has its own #scrollTo address.
Try clicking other blocks.
So,
I use it like
1. [LINK TEXT](#scrollTo=BLOCKADDRESS)
This way you don't have to put id tags
The following lines worked for me
Origination: text
Destination:
A typical mistake must be the use of " ". If you look at my above lines carefully, it applies "" for the destination line Link, whereas not for the origination line.
Please do not add " " like "#Link" in the origination, otherwise it will direct you to the "Sorry, the file you have requested does not exist." page.
I Create the internal links, in colab, as follows:
In the source text cell:
text of the link
In the target cell:
<div class="markdown-google-sans"><a name="id-link"></a></div>
Note: I used 'name' instead of 'id' because 'id' doesn't work.

Hyperlink a pdf on local drive to rmarkdown html output

I am trying to make the citation of my report in rmarkdown to hyperlinks of the pdfs. I try the following:
[the citation](thefile.pdf)
This works on my computer but if I send it to someone else the links don't work. I understand that I am not doing this properly, but this is how I do it for URLs. Is there any way I can have these pdfs attached to the rmarkdown?
You would need to upload your pdf's to the internet somewhere, and then put that link in the parentheses in the code snippet in your post. That code you posted would only work if the person had the same file in the same place on their computer.
[Your file](A google drive/dropbox link)

Can I add pdf tags to R markdown document?

I am trying to make a "accessible" or 508 compliant PDF using R markdown. To do this I need to have pdf tags attached to figure that provide alternative text. I also need to be able to add tags to section headers etc.
The idea is if you open the pdf in a pdf viewer that then the tags are read in in the "table of context" and allow a user to move between sections.
If you use a markdown header like
# header
R markdown seems to add a label to this so it appear in the table of context. I would like to be able to add these kind of labels manually as well.
Does anyone have any ideas of how to do this?
You should be able to achieve this using pandoc formatting (see http://pandoc.org/MANUAL.html), for example the alt text for an image can be specified as ![alt text or image title](path/to/image)

Embedding image in ipython notebook for distribution

I have an ipython notebook with an embedded image from my local drive. I was expecting it to be embedded in the JSON along with the output of code cells, but when I distributed the notebook, the image did not appear to users. What is the recommended way (or ways) to embed an image in a Notebook, so that it doesn't disappear if users rerun code cells, clear cell output, etc.?
The notebook system caches images included with ![label](image.png), but they last only until the python "kernel" serving the notebook is restarted. If I rename the image file on disk, I can close and reopen the notebook and it still shows the image; but it disappears when I restart the kernel.
Edit: If I generate an image as code cell output and then export the notebook to html, the image is embedded in the html as encoded data. Surely there must be a way to hook into this functionality and load the output into a markdown (or better yet "raw nbconvert") cell?
from IPython.display import Image
Image(filename='imagename.png')
will be exported (with ipython nbconvert) to html that contains the following:
<div class="output_png output_subarea output_execute_result">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnAAAAFgCAYAAAA...
</div>
However, even when I manually embedded this snippet into a markdown cell, I couldn't get the image to display. What am I doing wrong?
Update (2020)
Apparently, the problem has (finally!) been addressed in the newer notebook / Jupyter versions: as of 2018 (thanks for the link #Wayne), the html sanitizer will accept an embedded html image, as in <img src="data:image/png;base64,iV...> . Markdown image syntax also accepts images as embedded data, so there are two ways to do this. Details in these helpful answers:
markdown image syntax (answer by #id01)
html element syntax (in answer by #tel -- note that it works now!)
Are you happy to use an extra code cell to display the image? If so, use this:
from IPython.display import Image
Image(filename="example.png")
The output cell will have the raw image data embedded in the .ipynb file so you can share it and the image will be retained.
Note that the Image class also has a url keyword, but this will only link to the image unless you also specify embed=True (see the documentation for details). So it's safer to use the filename keyword unless you are referring to an image on a remote server.
I'm not sure if there is an easy solution if you require the image to be included in a Markdown cell, i.e. without a separate code cell to generate the embedded image data. You may be able to use the python markdown extension which allows dynamically displaying the contents of Python variables in markdown cells. However, the extension generates the markdown cells dynamically, so in order to retain the output when sharing the notebook you will need to run ipython nbconvert --to notebook original_notebook.ipynb --output preprocessed_notebook using the preprocessor pymdpreprocessor.py as mentioned in the section "Installation". The generated notebook then has the data embedded in the markdown cell as an HTML tag of the form <img src="data:image/png;base64,..."> so you can delete the corresponding code cell from preprocessed_notebook.ipynb. Unfortunately, when I tried this the contents of the <img> tag weren't actually displayed in the browser, so not sure if this is a viable solution. :-/
A different option would be to use the Image class in a code cell to generate the image as above, and then use nbconvert with a custom template to remove code input cells from the notebook. See this thread for details. However, this will strip all code cells from the converted notebook, so it may not be what you want.
The reason why the
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnAAAAFgCAYAAAA...
tag doesn't do anything when you put it in a markdown cell is because IPython uses an HTML sanitizer (something called Google Caja) that screens out this type of tag (and many others) before it can be rendered.
The HTML sanitizer in IPython can be completely disabled by adding the following line to your custom.js file (usually located at ~/.ipython/profile_default/static/custom/custom.js):
iPython.security.sanitize_html = function (html) { return html; };
It's not a great solution though, as it does create a security risk, and it doesn't really help that much with distribution.
Postscript:
The ability to render base64 encoded strings as images != obvious security concern, so there should be a way for the Caja people to eventually allow this sort of thing through (although the related feature request ticket was first opened back in 2012, so don't hold your breath).
I figured out that replacing the image URL in the ![name](image) with a base64 URL, similar to the ones found above, can embed an image in a markdown container.
Example markdown:
![smile](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==)
If using the IPython HTML() function to output raw HTML, you can embed a linked image in base64 inside an <img> tag using the following method:
import base64
import requests
from IPython.core.display import HTML
def embedded_image(url):
response = requests.get(url)
uri = ("data:" +
response.headers['Content-Type'] + ";" +
"base64," + str(base64.b64encode(response.content).decode('utf-8')))
return uri
# Here is a small example. When you export the notebook as HTML,
# the image will be embedded in the HTML file
html = f'<img src="{embedded_image("https://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg")}" />'
HTML(html)
UPDATE: As pointed out by #alexis, this doesn't actually answer the question correctly, this will not allow users to re-run cells and have images persist (this solution only allows one to embed the images into exports).
As of Jupyter Notebook 5, you can attach image data to cells, and refer to them from the cell via attachment:<image-file-name>. See the menu Edit > Insert Image, or use drag and drop.
Unfortunately, when converting notebooks with attached (embedded) images to HTML, those images will not show up.
To get them into the HTML code, you can use (for instance) nbtoolbelt.
It will replace those attachment: references by data: with the image data embedded in the img tag.

Resources