【问题标题】:How to knit a child document from a URL如何从 URL 编织子文档
【发布时间】:2015-04-06 12:41:51
【问题描述】:

我想从 github 拉取子文档,作为 rmarkdown 文档中的子项编织。

使用来自allowing child markdown files 的yihui 的示例,我们可以有一个主文档(修改后),它引用github 上的子文档,而不是先下载它。

我已经解决了一个 Windows 兼容性问题,现在收到 setwd() 失败。

如何正确设置 knitr 文档以从 URL 编织子项? (如果可能)

初始(在 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)
```

错误输出

## 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

这是错误的,因为在 Windows 上运行时 readLines 命令默认无法访问 HTTPS。

v2(在 Windows 上)

为了纠正 readLines 问题,我添加了一个块,增加了访问 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)
```

错误输出

## 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

尝试将setwd("~") 添加到块setup 对错误消息没有影响

【问题讨论】:

    标签: r knitr r-markdown


    【解决方案1】:

    我认为您不能这样做,因为据我了解,在编织过程中会发生目录更改(到子文档的目录)。因为您的子文档不是本地文件,所以隐式 setwd 会失败。

    一种解决方案是添加一个隐藏块,将 github 文件下载到临时目录,然后删除下载的文件。比如:

    ```{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)
    ```
    

    【讨论】:

      【解决方案2】:

      如果孩子只是降价(没有要处理的 R 代码),那么这可能对你有用。

      ```{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")
      ```
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-12
        • 2020-10-14
        • 1970-01-01
        • 2016-02-15
        • 2019-11-03
        • 1970-01-01
        • 2014-03-19
        • 1970-01-01
        相关资源
        最近更新 更多