Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display images in Markdown on github generated from knitr without using external image hosting?

I like uploading repositories to github that include multiple R Markdown and Markdown files.

Here is an example of such a markdown file on github. And here's a screen grab.


enter image description here


The problem is that images do not display. You can click on the image, and you will go to where the file is stored. The file referenced is:
 https://github.com/... /blob/.../myfigure.png

whereas I presume it needs to reference

 https://github.com/... /raw/.../myfigure.png

Things I considered:

imgur: I could use external image hosting (e.g., see this example) by adding the following code:

```{r setup}
opts_knit$set(upload.fun = imgur_upload) # upload all images to imgur.com
````

However, for various reasons I don't want to do this (I have trouble uploading when behind a firewall; it's slow; it creates an unnecessary dependency)

Rpubs: There's also RPubs which is quite cool. However, at time of posting it seems more suited to single markdown documents rather than multiple R markdown documents. And it doesn't provide such a close link between source R Markdown and the Markdown document.

Question

  • Is there a workflow for using R Markdown and knitr to produce Markdown files which when uploaded to github permit the Markdown file to display images stored in the github repository?
like image 474
Jeromy Anglim Avatar asked Jun 28 '12 03:06

Jeromy Anglim


People also ask

How do I display an image in markdown?

Images can be added to any markdown page using the following markdown syntax: ![ alt text for screen readers](/path/to/image. png "Text to show on mouseover") .

How do I embed an image in GitHub?

You can drag and drop the image from your computer to a comment field in GitHub. Wait for the file to upload and then the URL to the image is right there!


2 Answers

This used to be part of the minimal example, use

opts_knit$set(base.url='https://github.com/.../raw/.../')

See the changes here and here.

Also see http://yihui.name/knitr/options.

EDIT [with update to restore base.url to former value

Regarding switching, you could define a function as

create_gitpath <- function(user, repo, branch = 'master'){
   paste0(paste('https://github.com', user, repo, 'raw', branch, sep = '/'),'/')
}

my_repo <- create_gitpath(user, repo)

knit.github <- function(..., git_url  ){
 old_url <- opts_knit$get('base.url')
 on.exit(opts_knit$set(base.url = old_url))
 opts_knit$set(base.url  = git_url)
 knit(..., envir = parent.frame())
}

Run with knit until you want to push to github then run knit.github(..., git_url = my_repo)

like image 161
mnel Avatar answered Sep 19 '22 15:09

mnel


What about the following code at the beginning of your markdown file?

``` {r setup,echo=FALSE,message=FALSE}
gitsubdir <- paste(tail(strsplit(getwd(),"/")[[1]],1),"/",sep="")
gitrep <- "https://github.com/mpiktas/myliuduomenis.lt"
gitbranch <- "master"
opts_knit$set(base.url=paste(gitrep,"raw",gitbranch,gitsubdir,sep="/"))
```

It is possible to tweak it so that gitrep and gitbranch will be reported by git. Here I assumed that I am one directory level below the main git repository directory. Again this might be tweaked to accommodate more complicated scenarios.

I've tested on github, here is the Rmd file and corresponding md file.

like image 20
mpiktas Avatar answered Sep 21 '22 15:09

mpiktas