Question: 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](https://github.com/jeromyanglim/gelman-bayesian-data-analysis/blob/master/exercise-2-11-1/exercise-2-11-1.md). And here's a screen grab.

---

![enter image description here](https://i.stack.imgur.com/hI7ww.png)

---

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](https://raw.github.com/yihui/knitr/master/inst/examples/knitr-upload.Rmd)) 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](http://rpubs.com/) 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?

Answer: 
This used to be part of the minimal example, use 

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

```

See the changes [here](https://github.com/yihui/knitr/commit/a562a1792a07aa1eaaa159afd01c037ac86c585e) and [here](https://github.com/yihui/knitr/commit/e85848c6c2e3464fdc5d6c74154d19b049f32926). 

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)`
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](https://github.com/mpiktas/myliuduomenis.lt/blob/master/2012-05-23-roziniai-akiniai/2012-05-23-roziniai-akiniai.Rmd) file and corresponding [md](https://github.com/mpiktas/myliuduomenis.lt/blob/master/2012-05-23-roziniai-akiniai/2012-05-23-roziniai-akiniai.md) file.