r - 如何在不使用外部图像托管的情况下在从 knitr 生成的 github 上的 Markdown 中显示图像?

标签 r github knitr rstudio

我喜欢将包含多个 R Markdown 和 Markdown 文件的存储库上传到 github。

这是一个 example of such a markdown file on github .这是一个屏幕抓取。

enter image description here

问题是图像不显示。您可以单击图像,然后您将转到存储文件的位置。
引用的文件是:

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

而我认为它需要引用
 https://github.com/... /raw/.../myfigure.png

我考虑的事情:

imgur:我可以通过添加以下代码来使用外部图像托管(例如, see this example ):
```{r setup}
opts_knit$set(upload.fun = imgur_upload) # upload all images to imgur.com
````

但是,由于各种原因,我不想这样做(我在防火墙后面上传时遇到问题;它很慢;它会产生不必要的依赖)

Rpubs:还有RPubs这很酷。但是,在发布时它似乎更适合单个 Markdown 文档而不是多个 R markdown 文档。并且它没有在源 R Markdown 和 Markdown 文档之间提供如此紧密的链接。


  • 是否有使用 R Markdown 和 knitr 生成 Markdown 文件的工作流程,这些文件在上传到 github 时允许 Markdown 文件显示存储在 github 存储库中的图像?
  • 最佳答案

    这曾经是最小示例的一部分,请使用

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

    查看更改 herehere .

    另见 http://yihui.name/knitr/options .

    编辑 [更新以将 base.url 恢复为以前的值

    关于切换,您可以将函数定义为
    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())
    }
    

    运行 knit直到你想推送到 github然后运行 ​​knit.github(..., git_url = my_repo)

    关于r - 如何在不使用外部图像托管的情况下在从 knitr 生成的 github 上的 Markdown 中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11237715/

    相关文章:

    bash - Github 操作 - 错误 : Process completed with exit code 1

    java - Gradle - 如何从 GitHub 创建和获取简单的类

    Markdown 中的 R anchor

    R Markdown 水平规则也适用于 LaTeX pdf?

    python - 等效于 Python Pandas 中的 R rbind.fill

    python - 如何在Python中使用ODBC链接SQL数据库并进行SQL查询

    r - 无法使用 ggplot2 绘制多线图

    r - 如何在运行 r 脚本时忽略错误

    windows - 代理后面的 Windows 7 上的 Git Bash 不再工作

    r - 获取在knitr中编织的文档类型