楼主: oliyiyi
1489 4

Test drive R Markdown [推广有奖]

版主

已卖:2994份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
101105 个
通用积分
31671.0967
学术水平
1454 点
热心指数
1573 点
信用等级
1364 点
经验
384134 点
帖子
9629
精华
66
在线时间
5508 小时
注册时间
2007-5-21
最后登录
2025-7-8

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

楼主
oliyiyi 发表于 2016-6-18 20:05:47 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

2016-06-15: I have high-jacked this for my useR! tutorial. Once I migrate content over to the happy git repo, I will put this more back to the way it was!

Overview

This describes a hands-on activity where the goal is to author an R Markdown document and render it to HTML. We discuss how to keep the intermediate Markdown file, the figures, and what to commit to Git and push to GitHub. If GitHub is the primary venue, we render directly to GitHub-flavored markdown and never create HTML.

Here is the official R Markdown documentation: http://rmarkdown.rstudio.com


Step 0: Software installation and configuration

We assume the following


Step 1: Get ready to work

Launch RStudio, probably in the Project that corresponds to the repository where you are keeping all STAT 545 coursework. Make sure the workspace is clean and you’ve launched a fresh R process. Make sure the working directory is sensible.

this should be a repo that is already syncing with a github remote


Step 2: Practice with RStudio’s boilerplate R Markdown document

I am modelling “walk before you run” here. It is best, especially for novices, to increase complexity in small increments. We will test our system’s ability to render the “hello world” of R Markdown documents before we muddy the waters with our own, probably buggy, documents.

Do this: File > New File > R Markdown …

  • Give it an informative title. This will appear in the document but does not necessarily have anything to do with the file’s name. But the title and filename should be similar! The title is for human eyeballs, so it can contain spaces and punctuation. The filename is for humans and computers, so it should have similar words in it but no spaces and no punctuation.
  • Accept the default Author or edit if you wish.
  • Accept the default output format of HTML.
  • Click OK.

Save this document to a reasonable filename and location. The filename should end in .Rmd or .rmd. Ihighly recommend saving in the top-level of the directory that is also also a Git repository for your coursework and that is also an RStudio project and that is also current working directory. Trust me on this.

commit here

Click on “Knit HTML” or do File > Knit Document. RStudio should display a preview of the resulting HTML. Also look at the file browser (which should be pointed at the directory where you saved the .Rmd file). You should see the R Markdown document, i.e. foo.Rmd AND the resulting HTML foo.html.

Congratulations, you’ve just made your first reproducible report with R Markdown.

commit here


Step 3: Take control of the output format

Do you really want HTML? Do you only want HTML? If so, you can skip this step!

The magical process that turns your R Markdown to HTML is like so: foo.Rmd --> foo.md --> foo.html. Note the intermediate markdown, foo.md. By default RStudio discards this, but you might want to hold on to that markdown.

Why? GitHub gives very special treatment to markdown files. They are rendered in an almost HTML-like way. This is great because it preserves all the charms of plain text but gives you a pseudo-webpage for free when you visit the file in the browser. In contrast, HTML is rendered as plain text on GitHub and you’ll have to take special measures to see it the way you want.

In many cases, you only want the markdown. In that case, we switch the output format to github_document. This means render will be foo.Rmd --> foo.md, where foo.md is GitHub-flavored markdown. If you still want the HTML but also the intermediate markdown, there’s a way to request that too.

Output format is one of the many things we can control in the YAML frontmatter – the text at the top of your file between leading and trailing lines of ---.

You can make some changes via the RStudio IDE: click on the “gear” in the top bar of the source editor, near the “Knit HTML” button. Select “Output options” and go to the Advanced tab and check “Keep markdown source file.” Your YAML should now look more like this:

    ---      title: "Something fascinating"      author: "Jenny Bryan"      date: "`r format(Sys.Date())`"    output:        html_document:          keep_md: true      ---  

You should have gained the line keep_md: true. You can also simply edit the file yourself to achieve this.

In fact this hand-edit is necessary if you want to keep only markdown and get GitHub-flavored markdown. In that case, make your YAML look like this:

    ---      title: "Something fascinating"      author: "Jenny Bryan"      date: "`r format(Sys.Date())`"    output: github_document    ---  

Save!

commit here

Render via “Knit HTML” button.

Now revisit the file browser. In addition to foo.Rmd, you should now see foo.md. If there are R chunks that make figures, the usage of markdown output formats will also cause those figure files to be left behind in a sensibly named sub-directory, foo_files.

If you commit and push foo.md and everything inside foo_files, then anyone with permission to view your GitHub repo can see a decent-looking version of your report.

If your output format is html_document, you should still see foo.html. If your output format isgithub_document and you see foo.html, that’s leftover from earlier experiments. Delete that. It will only confuse you later.

commit here


Step 4: Swap out the “guts” of the document

Select everything but the YAML frontmatter and … delete it!

Write a single English sentence.

Insert an empty R chunk, via the “Chunk” menu in upper right of source editor or with corresponding keyboard shortcut.

` ``{r}## insert your brilliant WORKING code here```

Insert 1 to 3 lines of functioning code that begin the task at hand. “Walk through” and run those lines using the “Run” button or the corresponding keyboard shortcut. You MUST make sure your code actually works!

Satisfied? Save!

commit here

Now render the whole document via “Knit HTML.” Voilà!

commit here


Step 5: Develop your report

In this incremental manner, develop your report. Add code to this chunk. Refine it. Add new chunks. Go crazy! But keep running the code “manually” to make sure it works.

If it doesn’t work with you babysitting it, I can guarantee you it will fail, in a more spectacular and cryptic way, when run at arms-length via “Knit HTML” or rmarkdown::render().

Clean out your workspace and restart R and re-run everything periodically, if things get weird. There are lots of chunk menu items and keyboard shortcuts to accelerate this workflow. Render the whole document often to catch errors when they’re easy to pinpoint and fix. Save often and commit every time you reach a point that you’d like as a “fall back” position.

You’ll develop your own mojo soon, but this should give you your first successful R Markdown experience.


Step 6: Publish your report

If you’ve been making HTML, you can put that up on the web somewhere, email to your collaborator, whatever.

No matter what, technically you can publish this report merely by pushing a rendered version to GitHub. However, certain practices make this effort at publishing more satisfying for your audience.

Here are two behaviors I find very frustrating:

  • “Here is my code. Behold.” This is when someone only pushes their source, i.e. R Markdown or R code AND they want other people to look at their “product”. The implicit assumption is that target audience will download code and run it. Sometimes the potential payoff simply does not justify this effort. Communication fail.
  • “Here is my HTML. Behold.” This is when someone doesn’t bother to edit the default output format and accepts HTML only. What am I supposed to do with HTML on GitHub? Communication fail.

Creating, commiting, and pushing markdown is a very functional, lighweight publishing strategy. Useoutput: github_document or keep_md: true if output is html_document. In both cases, it is critical to also commit and push everything inside foo_files. Now people can visit and consume your work like any other webpage.

This is (sort of) another example of keeping things machine- and human-readable, which is bliss. By makingfoo.Rmd available, others can see and run your actual code. By sharing foo.md and/or foo.html, others can casually browse your end product and decide if they even want to run your code.

HTML on GitHub

HTML files, such as foo.html, are not immediately useful on GitHub (though your local versions are easily viewable). Visit one and you’ll see the raw HTML. Yuck. But there are ways to get a preview: such ashttps://rawgit.com or http://htmlpreview.github.io. Expect some pain with HTML files inside private repos. When it becomes vital for the whole world to see proper HTML in its full glory, it’s time to use a more sophisticated web publishing strategy.

I have more general ideas about how to make a GitHub repo function as a website.






二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:R Markdown drive Down Mark test drive

已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
janyiyi + 5 + 3 + 3 + 3 精彩帖子

总评分: 论坛币 + 5  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html

沙发
oliyiyi 发表于 2016-6-18 20:06:16
Troubleshooting
Make sure RStudio and the rmarkdown package (and its dependencies) are up-to-date. In case of catastrophic failure to render R Markdown, consider that your software may be too old. R Markdown has been developing rapidly (written 2015-09), so you need a very current version of RStudio and rmarkdown to enjoy all the goodies we describe in this course.

Get rid of your .Rprofile, at least temporarily. I have found that a “mature” .Rprofile that has accumulated haphazardly over the years can cause trouble. Specifically, if you’ve got anything in there relating to knitr,markdown, rmarkdown and RStudio stuff, it may be preventing the installation or usage of the most recent goodies (see above). Comment the whole file out or rename it something else and relaunch or even re-install RStudio.

Insert a chunk in your .Rmd document so that it renders even when there are errors. Some errors are easier to diagnose if you can execute specific R statements during rendering and leave more evidence behind for forensic examination. Put this chunk:

` ``{r setup, include = FALSE, cache = FALSE}  
knitr::opts_chunk$set(error = TRUE)  
```
near the top of your R Markdown document if you want to soldier on through errors, i.e. turn foo.Rmd intofoo.md and/or foo.html no matter what. This is also helpful if you are writing a tutorial and want to demo code that throws an error. You might want to keep this as an RStudio snippet for easy insertion.

Tolerate errors in one specific chunk. If it’s undesirable to globally accept errors, you can still do this for a specific chunk like so:

` ``{r wing-and-a-prayer, error = TRUE}  
## your sketchy code goes here ;)
```
Check your working directory. It’s going to break your heart as you learn how often your mistakes are really mundane and basic. Ask me how I know. When things go wrong consider:

What is the working directory?
Is that file I want to read/write actually where I think it is?
Drop these commands into R chunks to check the above:

getwd() will display working directory at run time. If you monkeyed around with working directory with, e.g., the mouse, maybe it’s set to one place for your interactive development and another when “Knit HTML” takes over?
list.files() will list the files in working directory. Is the file you want even there?
Don’t try to change working directory within an R Markdown document. Just don’t. That is all.

Don’t be in a hurry to create a complicated sub-directory structure. RStudio/knitr/rmarkdown (which bring you the “Knit HTML” button) are rather opinionated about the working directory being set to the .Rmdfile’s location and about all files living together in one big happy directory. This can all be worked around. But not today.
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
janyiyi + 3 + 3 + 3 精彩帖子

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

藤椅
oliyiyi 发表于 2016-6-18 20:06:39
This work is licensed under the CC BY-NC 3.0 Creative Commons License.

板凳
hyq2003 发表于 2016-6-18 20:12:26
good job
已有 1 人评分论坛币 收起 理由
oliyiyi + 10 精彩帖子

总评分: 论坛币 + 10   查看全部评分

报纸
william9225 学生认证  发表于 2016-6-18 20:38:35 来自手机
谢谢分享
已有 1 人评分论坛币 收起 理由
oliyiyi + 10 精彩帖子

总评分: 论坛币 + 10   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-22 13:15