简体   繁体   English

GitHub网站在RStudio中发布“编织”生成的HTML文件

[英]GitHub Website Publishing of “Knit”-Generated HTML files in RStudio

I want to share a step-by-step illustration on how to publish online your work on RStudio using GitHub pages. 我想分享有关如何使用GitHub页面在RStudio上在线发布您的作品的分步说明。

A lot of people taking the Coursera courses on data have problems with it if they don't come from computer science, including myself, so I want to share a system that works. 如果不参加计算机科学课程的很多人(包括我自己)都不是计算机科学的专家,那么我想分享一个有效的系统。 I'm sure it can be greatly improved. 我相信它可以大大改善。

Let's do it step-by-step, really easy: 让我们一步一步地做,非常简单:

1. Start with Github , not with RStudio . 1. Github开始,而不是RStudio

Don't have an account, sign-up for one - it's free and incredibly valuable. 没有帐户,只需注册一个帐户-它是免费的,而且非常有价值。 Now go to Repositories (top menu bar), and press on the green button New in the upper-right-hand corner. 现在转到“ 存储库” (顶部菜单栏),然后按右上角的绿色按钮“ 新建 ”。 Give a name to the repository (" test " for our puposes). 给存储库起个名字(我们的目的是“ test ”)。 You can skip the Description if you are in a hurry to proving to yourself that it works (same for the prompt to write some memo style one-liner under README). 如果您急于向自己证明它起作用,则可以跳过“描述”(与在README下写一些便笺样式的提示相同)。 So go... click on Create Repository (green button). 所以...单击“ 创建存储库” (绿色按钮)。 Now don't panic... just go directly to Settings (the cogwheel symbol in the vertical menu on the right-hand side). 现在不要惊慌...只需直接转到“设置” (右侧垂直菜单中的齿轮符号)。 Perfect! 完善! One more click and we're there... Launch automatic page generator . 再单击一次,我们就在其中... 启动自动页面生成器 Now we enter the guided part. 现在我们进入引导部分。 Feel free to leave all this as is for now... We can modify it later, or we can go back and do it all over again when we know the end of the story (with a real repository that we do intend to publish. Click on Continue to layouts . Click on any of the designs (I chose "Architect)... And Publish page (green button upper-right corner). Beautiful! 随时保留所有这些……我们可以稍后对其进行修改,或者当我们知道故事的结尾时(我们打算发布一个真实的存储库),可以回头再做一遍。单击Continue to layouts 。单击任何设计(我选择了“ Architect” ...),然后单击Publish页面 (右上角的绿色按钮)。

Quick check: Click on Settings again, and check under GitHub Pages for the message: "Your site is published at http://...github.io/test . In my case the address is http://rinterested.github.io/test/ (was... I erased it after I finished this post) beacuse my Github account is RInterested. Very critical: pay attention to the structure: github.io without this there's no website. Now you can go to your browser, type the address and see that a new site has been born (caution: sometimes this takes minutes to materialize). 快速检查:再次单击“ 设置” ,然后在GitHub Pages下检查消息:“您的网站发布在http://...github.io/test上 。对于我来说,该地址是http://rinterested.github。 io / test / (是...我在完成这篇文章后删除了它),因为我的Github帐户是RInterested。非常关键:请注意结构: github.io,没有这个,没有网站。现在您可以使用浏览器,请输入地址,然后查看是否有新站点诞生(注意:有时需要几分钟的时间才能实现)。

2. Now it's time to go to RStudio . 2.现在是时候去RStudio了 If you're taking the Coursera course this is your comfort zone. 如果您选择Coursera课程,这是您的舒适区域。 If not I will assume that you've been working with [R] , and that a simple download of RStudio (free online) is about to change your life for the better. 如果没有,我将假设您一直在使用[R] ,并且简单下载RStudio(免费在线)将使您的生活变得更好。

So we start RStudio and we go to the upper-right corner, where there is a cube containing an R, and the word Project . 因此,我们启动RStudio,然后转到右上角,那里有一个包含R的多维数据集和单词Project See it? 看见? Great, because we are going to start a New Project from the pull-down menu. 太好了,因为我们将从下拉菜单中启动一个新项目 Pop-up... which one to choose? 弹出...选择哪一个? Version Control is the answer. 版本控制就是答案。 More pop-ups... No panicking... Click on the gray/red/green sideways GIT symbol ("Git Clone a project from a Git repository"). 更多弹出窗口...不要惊慌...单击侧面的灰色/红色/绿色GIT符号(“ Git从Git存储库克隆项目”)。 We have that, remember? 我们有那个,记得吗? We called it "Test". 我们称其为“测试”。 And we are facing a final screen with three fields to fill in. Under Repository URL we need to enter information that will require a quick trip back to Github for a second... 最后,我们要面对的最后一个屏幕是要填写的三个字段。在“ 存储库URL”下,我们需要输入一些信息,这些信息将需要一秒钟的时间快速回到Github。

Are you there? 你在那里吗? OK... Do you see the HTTPS clone URL thing with a clipboard symbol on the right, half-way down within the test repository? OK ...您是否在测试存储库中途看到右侧的带有剪贴板符号的HTTPS克隆URL内容? Click on the clipboard symbol... Copied! 单击剪贴板上的符号...已复制! Great! 大! Now we go back to the screen with questions on RStudio, where we got re-routed, and do a right-click and paste, filling in the Repository URL field of the questionnaire. 现在,我们回到屏幕,在RStudio上询问问题,然后重新路由,然后右键单击并粘贴,填写问卷的“存储库URL”字段。 The rest is not critical. 其余的都不重要。 Press on Create Project ... That's it! 创建项目 ...就这样!

3. Time to work. 3.上班时间。 Let's now create a new R Markdown document by clicking on the corresponding option on the pull-down menu in the left-upper corner (green plus sign on a sheet of paper icon). 现在,通过单击左上角下拉菜单上的相应选项(在纸质图标上绿色加号),创建一个新的R Markdown文档。 Give it a name. 给它起个名字。 You are the author. 您是作者。 Select HTML. 选择HTML。 Click OK. 单击确定。 Now you can modify, add, or just leave what popped-up on the screen as is to finish up the demonstration. 现在,您可以按原样修改,添加或仅将弹出窗口保留在屏幕上,以完成演示。 Notice that there are so-called chunks of code started with ``{r} which indicate that the following code will be executed upon rendering the html document (or pdf). 请注意,存在以``{r}''开头的所谓代码 ,它们表示在渲染html文档(或pdf)时将执行以下代码。 echo=FALSE means that only the result of the code (not the actual command) will be printed. echo=FALSE表示仅打印代码结果(而不是实际命令)。

OK. 好。 Press on the top menu where you see a knitting pin ( Knit HTML )... give it a name and save the document... Very important... whatever you name it it has to end up in .Rmd (case sensitive). 按下顶部菜单,在该菜单上您会看到一个编织针( Knit HTML )...给它命名并保存文档...非常重要...无论用什么名字,它都必须以.Rmd (区分大小写) 。 What about "cars.Rmd"? 那“ cars.Rmd”呢? Original enough... Save it and watch RStudio do its magic... Hopefully you're now admiring a beautiful webpage with a plot and numbers... Only one problem... It is NOT online. 足够原始了...保存并观看RStudio发挥神奇的作用...希望您现在正在欣赏带有情节和数字的漂亮网页...只是一个问题...它不在线。

4. Pushing our work to GitHub: 4.将我们的工作推向GitHub:

We are going now in the opposite direction. 我们现在正朝相反的方向前进。 First, click on the GiT super-cool symbol on the top menu (above "knit HTML" depending on the configuration of your RStudio). 首先,单击顶部菜单上的GiT超酷符号(在“编织HTML”上方,具体取决于RStudio的配置)。 It's a pull-down menu. 这是一个下拉菜单。 Select Commit . 选择提交 You can select cars.html , or everything. 您可以选择cars.html或所有内容。 Disregard a warning message, and post an ultra-short note under "Commit message". 忽略警告消息,并在“提交消息”下张贴超短注释。 Whatever you want to write (eg "update"?). 无论您要写什么(例如“更新”?)。 Click on the Commit button - very important! 单击提交按钮-非常重要! Now you are ready to click on Push ... You guessed it... It's the green button. 现在您可以单击“ 推”了 。您猜对了……这是绿色按钮。 You will be prompted to enter your Github account Username and Password. 系统将提示您输入您的Github帐户用户名和密码。 Do so. 这样做。

5. Back to GitHub: 5.返回GitHub:

Refresh the page displaying our "test" repository. 刷新显示“测试”存储库的页面。 You should now see the additional documents, including cars.html 现在,您应该看到其他文件,包括cars.html

Quick check: Go to your browser and enter in the URL bar what for me would be rinterested.github.io/test/cars.html (remember that you can get your own website address by going under Settings as explained above), and then just tag on the name of the document we have worked on with RStudio. 快速检查:转到浏览器,然后在URL栏中输入对我来说应该是rinterested.github.io/test/cars.html (请记住,您可以通过如上所述在“设置”下获得自己的网站地址),然后只需在我们使用RStudio处理过的文档的名称上加标签即可。

6. Hyperlink the pages: 6.超链接页面:

As a test click on "index.html" under the "test" repository and somewhere there, in the body embed the following: 作为测试,请单击“测试”存储库下及其中某个位置的“ index.html”,在主体中嵌入以下内容:

Click <a href="cars.html">here</a> to go to cars.

You will have to click on the pencil icon to edit this file. 您将必须单击pencil图标来编辑此文件。 When done click on Commit changes . 完成后,单击“ 提交更改” Go back on the browser to see your site. 返回浏览器以查看您的站点。 Refresh until it's updated with the hyperlink you just embedded. 刷新,直到使用您刚刚嵌入的超链接对其进行更新。 Click on it... You are now watching your RStudio work fully functional online. 单击它...您现在正在观看RStudio的在线完整功能。 Magic! 魔法!

Now it's just a matter to change things around using some html code quickly accessible online, changing the wording on the index.html (home page), or perhaps building a nice site with internal cohesiveness from scratch. 现在,只需使用可快速在线访问的一些html代码,更改index.html(主页)上的措辞,或者也许从头开始构建具有内部凝聚力的好站点,就可以改变事情。

Good luck! 祝好运!

An alternative (more manual) approach - anything covered on the initial answer skipped. 另一种替代方法(更多手册)-跳过初始答案中涵盖的所有内容。

1. Create a Repository in GitHub 1.在GitHub中创建一个存储库

Let's call it Course_project without need to README - we're just going to need its URL. 我们称它为Course_project,而无需自述-我们只需要它的URL。

2. Open Windows PowerShell (or Git Bash) 2.打开Windows PowerShell(或Git Bash)

Opening Git Bash is as simple as to right click on the directory in Windows Explorer we want to select, and click on Git Bash here . 打开Git Bash非常简单,只需右键单击我们要选择的Windows资源管理器中的目录,然后在此处单击Git Bash I'll use PowerShell. 我将使用PowerShell。 You can get the directory you're in by typing pwd . 您可以通过输入pwd获得您所在的目录。 From there we can type ls to list subfolders, all along changing directory to reach where we want to be - for instance cd R to get R as the working directory. 从那里我们可以键入ls来列出子文件夹,一直到更改目录以到达我们想要的位置-例如cd R以获得R作为工作目录。

Clone project by typing https://github.com/RInterested/Course_project.git (I am RInterested, so change that part to your GitHub username; also remember that "Course_project" is the name of this project). 通过键入https://github.com/RInterested/Course_project.git克隆项目 (我是RInterested,所以将这一部分更改为您的GitHub用户名;还要记住“ Course_project”是该项目的名称)。 Change the directory to Course_project by typing cd Course_project . 通过键入将目录更改为Course_project cd Course_project We now create a branch without parent branches: git checkout --orphan gh-pages . 现在,我们创建一个没有父分支的分支: git checkout --orphan gh-pages Make sure that the cloned Course_project is empty by typing: git rm -rf . 通过键入以下命令来确保克隆的Course_project为空: git rm -rf . We'll soon work with real RStudio markdown, but just to test what we have done so far, we can type into the empty working directory a name of a webpage: echo "Test Page" > index.html . 我们将很快使用真实的RStudio降价,但是为了测试到目前为止所做的工作,我们可以在空白的工作目录中键入网页的名称: echo "Test Page" > index.html Time now to Add/Commit/Push: git add index.html followed by git git commit -a -m "first commit" , and git push origin gh-pages . 现在是添加/提交/推送的时间: git add index.html后跟git git commit -a -m "first commit" ,以及git push origin gh-pages This page is now visible at: http://RInterested.github.io/Course_project/index.html . 现在可以在以下页面看到此页面: http : //RInterested.github.io/Course_project/index.html

3. Creating content with RStudio 3.使用RStudio创建内容

We have a directory ( Course_project ) and a toy index.html in it... We need real content. 我们有一个目录( Course_project )和一个玩具index.html在其中...我们需要真实的内容。 So we go to RStudio. 所以我们去了RStudio。 Create an R Markdown file and name it (I'm calling it mtcars ). 创建一个R Markdown文件并将其命名(我称其为mtcars )。 Check HTML as the output. 检查HTML作为输出。 Change the document (some info about it in the answer above). 更改文档(以上答案中有关此文档的一些信息)。 Finally, an important step: Save it as Index.Rmd (make sure that the R working directory is the same as in PowerShell). 最后,重要的一步:将其另存为Index.Rmd (确保R工作目录与PowerShell中的目录相同)。 Click on knit HTML to create the html file rendering the R code embedded in R Markdown. 单击knit HTML来创建呈现嵌入R Markdown的R代码的html文件。

4. Pushing it upstream with PowerShell 4.使用PowerShell将其推向上游

Now we have material within the directory Course_project , but it is not online - it is still local (you can go to it with Windows Explorer and see the html file under "index"). 现在,我们在Course_project目录中有了资料 ,但是它并不在线-仍然是本地的(您可以使用Windows资源管理器转到该资料,并在“索引”下看到html文件)。 Type git status to see the changes made, and again follow the routine Add/Commit/Push: git add . 键入git status以查看所做的更改,然后再次遵循例程Add / Commit / Push: git add . , followed by git commit -a -m "commit" , and git push origin gh-pages . ,然后是git commit -a -m "commit"git push origin gh-pages

After a while the page will be live in http://RInterested.github.io/Course_project/index.html 稍后,该页面将位于http://RInterested.github.io/Course_project/index.html中

5. Adding a second page 5.添加第二页

We can now start a second R Markdown file on RStudio (I labeled it Second_page and saved it as Second_page.Rmd . After writing what we want we knit HTML , and we can see these two new files appear in the Course_project local folder. We simply have to Add/Commit/Push like before in PowerShell. The new page will be in http://rinterested.github.io/Course_project/Second_page.html . It is very easy now to type a hyperlink on the first ( index.Rmd ) to the second ( Second_page.html ), knit the index.Rmd, and push it up to the web. 现在,我们可以在RStudio上启动第二个R Markdown文件(我将其标记为Second_page并将其保存为Second_page.Rmd 。在编写了我们想要的内容之后,我们编织了HTML ,我们可以看到这两个新文件出现在Course_project本地文件夹中。必须像以前在PowerShell中一样添加/提交/推送。新页面将位于http://rinterested.github.io/Course_project/Second_page.html中 。现在在第一个( index.Rmd )到第二个( Second_page.html ),编织index.Rmd,然后将其推送到网络上。

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

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