首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从URL中编织子文档

如何从URL中编织子文档
EN

Stack Overflow用户
提问于 2015-04-06 12:41:51
回答 2查看 500关注 0票数 1

我想从github中提取子文档,以便在重标记文档中编织子项目。

使用允许子标记文件中的益辉示例,我们可以有一个主文档(修改),它引用github上的子文档,而不是先下载它。

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

如何正确地设置一个针织品文档以从一个URL中编织子项目?(如果可能的话)

初始(在窗口上)

代码语言:javascript
复制
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'}
代码语言:javascript
复制
You can continue your main document below, of course.

```{r test-another}

pmax(1:10,5)

代码语言:javascript
复制

误差输出

代码语言:javascript
复制
## 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

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

v2 (在windows上)

为了纠正readLines问题,我添加了一个块,它增加了访问HTTPS的能力。

代码语言:javascript
复制
You can also use the `child` option to include child documents in markdown.

```{r setup, results='hide'}

setInternet2(use = TRUE)

代码语言:javascript
复制
```{r test-main, child='https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd'}
代码语言:javascript
复制
You can continue your main document below, of course.

```{r test-another}

pmax(1:10,5)

代码语言:javascript
复制

误差输出

代码语言:javascript
复制
## 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中对错误消息没有影响。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-06 19:41:53

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

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

代码语言:javascript
复制
```{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“)

代码语言:javascript
复制
```{r test-main, child=x}
代码语言:javascript
复制
You can continue your main document below, of course.

```{r test-another}

pmax(1:10,5)

代码语言:javascript
复制
票数 4
EN

Stack Overflow用户

发布于 2021-07-02 23:01:18

如果子程序仅被标记(没有需要处理的R代码),那么这可能对您有效。

代码语言:javascript
复制
```{r footer, echo=FALSE, results='asis'}

url <- "https://raw.githubusercontent.com/yihui/knitr/master/inst/examples/child/knitr-child.Rmd

子文本<- readLines(url)

cat(儿童文本,sep="\n")

代码语言:javascript
复制
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29471545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档