简体   繁体   English

是否可以在github上放置R代码的自述文件,以显示输出?

[英]Is it possible to put a readme file for R code on github, that displays output?

Recently I participated in the #100daysofmlcode challenge on Linkedin, started by Siraj Raval. 最近,我参加了Siraj Raval 发起的 Linkedin上的#100daysofmlcode挑战。 I do all of my coding in R. But when I push an RMarkdown file or readme file for my rcode, on Github, it doesn't show the output generated from the code. 我在R中进行所有编码。但是,当我在Github上为我的rcode推送RMarkdown文件或自述文件时,它不会显示从代码生成的输出。 This makes it really difficult for viewers to catch up with the explanation. 这使得观看者很难跟上解释。 Is there a way we could display the code and output, so that it becomes easier for readers to understand? 有没有一种方法可以显示代码和输出,从而使读者更容易理解? I know they can pull the changes I make from github and see them on their local machines. 我知道他们可以从github提取我所做的更改,并在本地计算机上查看它们。 But considering the time limitations that everyone has, I would still like to know if there is a way we can display both Rcode and output in a readme file on github. 但是考虑到每个人的时间限制,我仍然想知道是否有一种方法可以在github上的自述文件中同时显示Rcode和输出。

Thank you 谢谢

在markdown中,对内联代码使用``,对代码块(多行)使用``````

GitHub is just a server, it can't process your Rmarkdown file. GitHub只是一台服务器,它无法处理您的Rmarkdown文件。 Two strategies are: 两种策略是:

  1. Call your file README.Rmd , and run render() on it to generate a README.md file that contains the output and push both to GitHub. 调用您的文件README.Rmd ,并在其上运行render()以生成一个包含输出的README.md文件,并将两者都推送到GitHub。
  2. Setup a continuous integration service like Travis-CI and instruct it to render your README and push the result back to GitHub. 设置像Travis-CI这样的持续集成服务,并指示其呈现自述文件并将结果推回到GitHub。

The first option is easiest from a technical setup perspective - you just have to render() . 从技术设置的角度来看,第一个选项是最简单的-您只需要render()

The second option is more convenient but requires some setup in your repo, configuring Travis to build (but not build on its own commits), and setting up credentials on Travis to do the push back to GitHub. 第二个选项更方便,但是需要在仓库中进行一些设置,配置Travis进行构建(但不能基于自己的提交),并在Travis上设置凭据以将其推回GitHub。 To do this you'll need a .travis.yml file that looks something like: 为此,您需要一个看起来像如下的.travis.yml文件:

language: r
script:
  - bash renderreadme.sh

And a bash script file in your repo called renderreadme.sh : 在您的存储库中有一个bash脚本文件,名为renderreadme.sh

#!/bin/bash
set -o errexit -o nounset
renderreadme(){
  ## Set up Repo parameters
  git init
  git config user.name "your_github_username"
  git config user.email "your_email@example.com"
  git config --global push.default simple

  ## Get drat repo
  git remote add upstream "https://$GH_TOKEN@github.com/$TRAVIS_REPO_SLUG.git"
  git fetch upstream
  git checkout master

  Rscript -e 'rmarkdown::render("README.Rmd")'

  git add README.md
  git commit -m "knit README [skip ci]"
  git push
}
renderreadme

And you'll need to use the travis client (or something equivalent) to store the secure GitHub credentials needed for the git push operation in that script to succeed. 而且,您将需要使用travis客户端 (或类似的客户端 )来存储git push操作成功所需的安全GitHub凭据。 The general guidance in "Building an R Project" for Travis will be useful for these general configuration aspects. Travis的“构建R项目”中的常规指南对于这些常规配置方面将很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM