楼主: oliyiyi
1461 1

Create Dynamic R Statistical Reports Using R Markdown [推广有奖]

版主

已卖:2994份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

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

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

楼主
oliyiyi 发表于 2016-6-18 19:02:39 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
Introduction - What is R markdown?

According to RStudio,

"R Markdown is a format that enables easy authoring of reproducible web reports from R. It combines the core syntax of Markdown (an easy-to-write plain text format for web content) with embedded R code chunks that are run so their output can be included in the final document".

In this document, we will introduce how to create dynamic R statistical reports in HTML.

  • What do you mean by dynamic? The traditional way to write a report is save your analysis from R first, i.e. saving the ANOVA table or using pdf() to save the graphs. A report written in this way could be problematic.  For instance, imagine your client telling you that they want to use a sub-sample instead of the entire sample. You would have to redo all of your work!! Therefore, in this way dynamic also means reproducible, in the sense that people who get the file from you can reproduce the entire work in the report.
  • How does R Markdown work out to be .html file? R Markdown is the file with the file extension .Rmd, the knitr package will then transform the file into a Markdown file with the extension .md. Then Rstudio will load another package called markdown to transform the file into .html.
  • Is this a popular method for creating reports? Check out Rpubs. This website shares lots of documents written in the way we will introduce below.

Let's take this in four steps:

  • Getting the Software Ready
  • Creating a Simple Report
  • Customizing your CSS  to Make Your Report Pretty
  • Become a Ninja!
Step 1: Getting the Software Ready
  • Download R studio at : http://rstudio.org/
  • Set options for R studio: Tools > Options > Click on Sweave and choose Knitr at “Weave Rnw files using Knitr”. Check Image
Step 2:  Install the Knitr Package
  • To install a package in RStudio, you use Tools > Install Packages and then select a CRAN mirror and package to install. Another way to install packages is to use the function install.packages(). For example:

# installs the knitr package from the Carnegi Mellon Statlib CRAN mirror
install.packages("knitr", repos = "http://lib.stat.cmu.edu/R/CRAN/")

Step 3: Creating a Simple Report
  • Create a blank R Markdown file: File > New > R Markdown. Check Image here You will open a new .Rmd file.

  • When you create the blank file, you can see an already-written module. One simple way to go is to replace the corresponding parts with your own information.

  • If you want to write a completely new file, check the markdown reference. It's only one page and you can find this on the right-hand panel of Rstudio by clicking MD.

  • After all your information is entered, click Knit HTML.


Now you will see that you have generated an .html file. What happens after you click Knit HTML? Simply speaking, the knitr package is loaded and the function knit() is applied to get the markdown file you wrote. Then RStudio uses markdown package to convert markdown to HTML.

Step 4: Customizing your CSS  to Make Your Report Pretty

Here we use table as an example. The R Markdown syntax for a table is:

First Header  | Second Header ------------- | ------------- Content Cell  | Content Cell Content Cell  | Content Cell

In your output file, you will have a table that will then look like this:

First HeaderSecond Header
Content CellContent Cell
Content CellContent Cell

Not very pretty, it is! What we can do now is customize the CSS. We will set the CSS for table by applying a bit or CSS code such as:

/*  CSS chunck 1  */th {  background-color:#E0E0E0 ;      border-bottom:1px solid black;      padding:5px;}td{border-bottom:1px dotted black;padding:5px;}table{ border-collapse:collapse;margin:auto; border: 1px solid black;}

To embed the CSS file, there are two methods:

  • At the beginning of the .Rmd file, place the following code:

    <style type="text/css">Insert CSS chunk 1</style>
  • Put the CSS chunk 1 in a .css stylesheet and then at the beginning of the file, add the following code:

    <link href="custom.css" rel="stylesheet">

By customizing the CSS file, you will have created a brand new-look for the table:

Step 5. Become a Ninja!

There is much more for you to play with, please check the website: http://yihui.name/knitr/ for more chunk options and package options.

Want to Learn More?

Register for STAT 497R: Topics in R: Statistical Language (1 credit)


二维码

扫码加我 拉你入群

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

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

关键词:Statistical R Markdown statistica statistic Reports Create

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

沙发
hyq2003 发表于 2016-6-18 19:21:47
good job
已有 1 人评分论坛币 学术水平 收起 理由
oliyiyi + 20 + 1 精彩帖子

总评分: 论坛币 + 20  学术水平 + 1   查看全部评分

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

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