study Knitr!   

study Knitr!

学习 knitr和 rmarkdown的一些语法

20 Aug 2016

Go back study Knitr
knitr::opts_chunk$set(echo = TRUE,prompt = T) 
#global setting e.g. if you want to set prompt = T for defalut,than you can set in here
#there is an arguement here *include* if you set T, than you can see this command,so as F

The basic symbol

  1. I use list(markdown grammar) to show this context.
  2. *,-,+ the effect of three are the same, there are called the Markdown symbol
  3. the blank line means stat a new paragraph also means trun to a new line
    the continuous two spacebars also means a new line
  4. ` represent ‘inline’

The basic statement


    * If you use separator you should print * * * ,but remember to skip lines up and down

1. title:

you need to know is that there are two type of the grammer in for the title:the Setext-like and atx-like

I just need to know the latter one atx-like.The maximum is six #(pounds)

#This is the title use one pound

This is the title use one pound

##This is the title use two pounds

This is the title use two pounds

###This is the title use three pounds

This is the title use three pounds

####This is the title use four pounds

2. reference or blockquote or annotation

Use > to do so. example >my first reference

my first reference

3. list

  1. nest1
  2. nest2

5. image

e.g.

![picName](/path/to/img.jpg)

6. code

  1. if your code is small use ` your code `, ` symbol is on your Tab key

use warnings

  1. you can also use ``` to wrap some code and assign a computer language

e.g. ``` perl \(a=1;\)b=$a+1 ```

 $a=1;
 $b=$a+1;
  1. And your can use four spacebar and tab to substitute the ```

7. table

following is the table code, it as widly aknowledged that use of table is annoyed

| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| zebra stripes | are neat      |    $1 |
:---  alignment of left
---: aligement of right
:---: middle
Tables Are Cool
col 3 is right-aligned $1600
col 2 is centered $12
zebra stripes are neat $1

8. foottrip

e.g.

*This is a test[^foot]*

*[^foot]:A fist mall*

This is a test1

9. use R code the most important repeat research

1. the basic use

Hi report! #warn: Don’t use Chinese!
the following page is writted by the way of code and it’s effect

\```{r}  
  a<-1
  a+1  
\```  
>   a<-1
>   a+1
## [1] 2

```{r label} You can also plus a label and the label will be the index at the lower left quarter
a<-1
a+1
```


How about we don’t use the curly braces, following is the result.

  a<-1
  a+1

As you can see ,R won’t execute the code, it just highlight them.


So till now I really want to know if we use perl,I mean instead perl with r in the curly braces
Let me try

\```perl 
  $a=1;
  $b=$a+1;
  print "$b";
\```
and 
\```{perl} 
  $a=1;
  $b=$a+1;
  print "$b"; 
\```  
  $a=1;
  $b=$a+1;
  print "$b";
>   $a=1;
+   $b=$a+1;
+   print "$b"; 
## 2

Now we can see it works. Yeah good job!!


2. expansion: the arguement

2.1 annotation in chuck: use # symbol

\```{r}
  a =1:10
  b =11:20
  a #This way you can annotate the code!
\```
>   a =1:10
>   b =11:20
>   a #This way you can annotate the code!
##  [1]  1  2  3  4  5  6  7  8  9 10

2.2 r arg1:echo

\```{r arg1,echo=F} #defalut echo=T
  a =1:10
  b =11:20
  a #This way you can annotate the code!
\```
##  [1]  1  2  3  4  5  6  7  8  9 10

You can see the result is just the out put of the R code!!!


2.3 r arg2:eval

\```{r arg2,eval=F} #defalut eval=T
  a =1:10
  b =11:20
  a #This way you can annotate the code!
\```
>   a =1:10
>   b =11:20
>   a #eval=F

You can see the result is just the R code, same as the basic use!!!
And you can control which line of the code to execute use eval =c(the vector of the lines)


2.4 r arg3:highlight #defalut highlight=T

>   a =1:10
>   b =11:20
>   a #eval=F,highlight=FALSE
>   a =1:10
>   b =11:20
>   a #eval=F,highlight=TRUE

2.5 r arg4:prompt #defalut prompt=F

>   a =1:10
>   b =11:20
>   a #prompt=T
##  [1]  1  2  3  4  5  6  7  8  9 10

2.6 r arg5:tidy #defalut tidy=F note this function rely on the parkage “formatR”

please see the e.g.

> for (i in 1:10 
+      ) 
+   {
+   print(i)
+         }
> #don't use tidy=T
> for (i in 1:10) {
+     print(i)
+ }
> # don't use tidy=T

So in order to show a better look, your can choose to select the arguement prompt, let it be TRUE

3. Plot

3.1 basic use

\```{r}
plot(1:10)
\```
> plot(1:10) #defult arguements

3.2 plot arguement

\```{r,fig.cap="the fig test",fig.width=3,fig.height=3}
  plot(1:10) 
  fig.cap="the fig test";fig.width=3,fig.height=3
\```
> plot(1:10) 
the fig test

the fig test

> #fig.cap="the fig test";fig.width=3,fig.height=3

Other usage need you to development!!!!!!!!!!!

yihui’s R Markdown Document

Okay, some R code

> fit = lm(dist ~ speed, data = cars)
> b = coef(fit)  # coefficients
> summary(fit)
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -29.069  -9.525  -2.272   9.215  43.201 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -17.5791     6.7584  -2.601   0.0123 *  
## speed         3.9324     0.4155   9.464 1.49e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 15.38 on 48 degrees of freedom
## Multiple R-squared:  0.6511, Adjusted R-squared:  0.6438 
## F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12

The code will be highlighted in all output formats.

And some pictures

> par(mfrow = c(2, 2), pch = 20, mar = c(4, 4, 2, .1), bg = 'white')
> plot(fit)
Regression diagnostics

Regression diagnostics

A little bit math

Our regression equation is \(Y=-17.5790949+3.9324088x\), and the model is:

\[ Y = \beta_0 + \beta_1 x + \epsilon\]

Pandoc extension: definition lists

Programmer
A programmer is the one who turns coffee into code. LaTeX
A simple tool that is nothing but a couple of backslashes.

Pandoc extension: examples

We have some examples.

  1. Think what is 0.3 + 0.4 - 0.7. Zero. Easy.
  2. Now think what is 0.3 - 0.7 + 0.4. Still zero?

People are often surprised by (2).

Pandoc extension: tables

A table here.

Demonstration of simple table syntax.
Right Left Center Default
12 12 12 12
123 123 123 123
1 1 1 1

Pandoc extension: footnotes

We can also write footnotes2.

Or write some inline footnotes3.

Pandoc extension: citations

We compile the R Markdown file to Markdown through knitr [@xie2013] in R [@R-base]. For more about @xie2013, see http://yihui.name/knitr.

References


  1. A fist mall

  2. hi, I’m a footnote

  3. as you can see here