1
votes

I'd like to pull child documents from github to knit as child items inside an rmarkdown document.

Using yihui's example from allowing child markdown files we can have a main doc (modified) that refers to the child doc on github instead of downloading it first.

I've resolved a Windows compatibility issue, and am now getting a setwd() fail.

How do you correctly setup a knitr document to knit child items from a URL? (if possible)

Initial (on windows)

You can also use the `child` option to include child documents in markdown.

```{r test-main, child='https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd'}
```

You can continue your main document below, of course.

```{r test-another}
pmax(1:10, 5)
```

Error output

## Quitting from lines 4-4 (https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd) 
## Error in readLines(if (is.character(input2)) { : 
##   cannot open the connection
## Calls: <Anonymous> ... process_group.block -> call_block -> lapply -> FUN -> knit -> readLines
## In addition: Warning message:
## In readLines(if (is.character(input2)) { : unsupported URL scheme
## Execution halted

This was erroring because the readLines command was unable to access HTTPS by default when working on Windows.

v2 (on windows)

To correct for the readLines issue I added a chunk that adds the ability to access HTTPS

You can also use the `child` option to include child documents in markdown.

```{r setup, results='hide'}
setInternet2(use = TRUE)
```

```{r test-main, child='https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd'}
```

You can continue your main document below, of course.

```{r test-another}
pmax(1:10, 5)
```

Error output

## processing file: https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd
## Quitting from lines 2-2 (https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd) 
## Quitting from lines NA-7 (https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd) 
## Error in setwd(dir) : cannot change working directory
## Calls: <Anonymous> ... process_group.inline -> call_inline -> in_dir -> setwd
## Execution halted

Trying to add in a setwd("~") into chunk setup has no impact on the error message

2

2 Answers

4
votes

I don't think you can do this because as I understand it, there is a directory change (to the child document's directory) during knitting. Because your child document is not a local file, the implicit setwd will fail.

A solution would be to add a hidden chunk that downloads the github file to a temporary directory and then deletes the downloaded file. Something like:

```{r setup, echo=FALSE, results='hide'}
setInternet2(use = TRUE)
x <- tempfile(fileext = "Rmd")
on.exit(unlink(x))
download.file("https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd", x)
```

```{r test-main, child=x}
```

You can continue your main document below, of course.

```{r test-another}
pmax(1:10, 5)
```
1
votes

If the child is only markdown (no R code to process), then this may work for you.

```{r footer, echo=FALSE, results='asis'}
url <- "https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd"
childtext <- readLines(url)
cat(childtext, sep="\n")
```