Showing posts with label knitr. Show all posts
Showing posts with label knitr. Show all posts

12 September 2014

A bit about R Markdown

---
title: "About R Markdown Try Out"
author: "Dasapta Erwin Irawan"
date: "Friday, September 12, 2014"
output: 
  word_document:
   fig_width: 5
   fig_height: 5
   fig_caption: true
---

Dear all,

I've finally experienced an R history, especially in the evolution of R Markdown. I first knew R in 2011. It was when I attended a short course about Top Model package, delivered by Wouter Bouytaert of Imperial College London (pardon for mis-spelling). 

This post was written in R Markdown. For those of you that havent't used it, just go to File -> New -> R Markdown. The R Markdown (Rmd) was one of the flavour of the original Markdown markup that was made mainly for HTML. You can write web page easier with MD then convert it to HTML before then upload it to your web page.

If we use the older R studio, the conversion process is based on command lines using `knitr` package following this flow:

from Rmd -> rendered to md -> rendered to html (the basic final format). 

Knitr is an R package written by Yihui Xie. With this package you can convert an Rmd file to HTML, directly with in R Studio. The newer `knitr` package had included the `pandoc` ability. So you can convert Rmd to pdf, LaTex, odt, or even Word. 

Then what's Pandoc? 
It is a command line based software, written by John MacFarlane. Previously we had to separately installed `pandoc`  outside R std. Then convert Rmd to many formats including Word, via terminal or Windows prompt.to pdf or word. Pandoc project is still running now. With this software you can amazingly turn your txt file in to a full size book without any word processor software. 

Back to Rmd, the following sections talk about the components and basic features of Rmd. It basically consists of YAML header and body text.



# 1. YAML Header

The YAML (Yet Another Markup Language) header is placed on top of the Rmd file, and it must be started and closed with three dashes `---`. I tried with two or five dashes, it didn't work.

If we use the newest version of R Studio (Version 0.98.1049), Rstd will automatically make the the YAML header, based on our choices in new file window.

We can insert in YAML header our figure size using this command: 

+ `fig_width:`, 
+ `fig_height:`, and 
+ `fig_caption:` 



# 2. Embedding R code

The default `echo` will be `TRUE`. If we use option `{r, echo = FALSE}` then Rstd will still run the command but not showing the actual code in the final doc / html / pdf document.

We can use 

+ three back ticks without ```{r}``` to make a dummy code. R std will show the code but not running it.
+ or use ```{r, eval=FALSE}```

We can also name the chunk by typing ```{r chunkname}```, with no spaces. So then we can use it to make an in-text reference.

Here's an example.

```{r chunk1}
A <- 2="" font="">
B <- 2="" font="">
C <- a="" b="" font="">
```

We can also cache a chunk so it won't be re-run by R Std. If the result is figure, then the figure is named based on the chunk name.

Example: Here's an equation 

```{r chunk2, cache=TRUE}
A <- 2="" font="">
B <- 2="" font="">
C <- a="" b="" font="">
```

# 3. Embedding image

We can automatically embed R plots as you show me. We also can embed offline images by using this command.

```
![Alt text](/path/to/img.jpg)
```

Here's an example

![Fig 1 Citroen DS is the most beautiful car ever made](C:\Users\dira0651\Documents\14-09-12\CitroenDS.jpg) 

from [Wikipedia](http://en.wikipedia.org/wiki/Citroën_DS)


# 4. Bold, italics and bullets

We can use under score symbol to print bold and italic font:

+ __Bold__
+ _italic_ 

We can use symbols like `-` or `+`. Add two `tabs` for nested bullets.

+ Bullet 1
+ Bullet 2
    + Bullet 2.1
    + Bullet 2.2
+ Bullet 3

Or combined with number:

1. List 1
2. List 2
    + Bullet 2.1
    + Bullet 2.2
3. List 3




22 May 2014


#writing with Markdown: Installing <em>knitcitations</em> package

#writing with Markdown: Installing knitcitations package

Installing bits

Knitcitations is an R package that allows us to write manuscripts or web pages complete with the citations and bibliography. Kind of JabRef or EndNote. With this tools we can write a complete document or even a thesis with #Markdown. I use Linux Ubuntu 13.10 and R 3.1.0.

First check if your development library in your system is complete. I'll explain why we need this zero step later.

Step 0: open your Terminal window and type

sudo apt-get install libcurl4-openssl-dev libxml2-dev

This step should install your Linux Delopment library.

Step 1: install the packages

Open your R Studio and run

  1. install.packages("knitcitations") or

  2. install.packages("~/Downloads/knitcitations_0.5-0.tar.gz", repos = NULL, type = "source") # to install from source "tar.zip" format

If any of both commands not working (I don't really know why), then we have to build the package from source. So to do this you have to install “devtools” package and you need to have a complete Linux development library.

So you need run this command

  1. install.packages("devtools") # to install R development tools then

  2. install_github("knitcitations", "cboettig") # to install the package directly from _cboettig's_ Git Hub account

Both above commands will look for the development library that you've installed on the previous step. It should work nicely. I hope, because I've followed several discussion on the net then the errors pop mostly because your system don't have the required library.

Real example of using Knitcitations

I'll post my update later on based on an example from my manuscript.

References:

21 May 2014

#writing: Example of Markdown to docx conversion


Review Cikapundung and Bandung Groundwater Basin This was an example of conversion between Markdown draft to docx
  1. Make a draft of anything like the following example using R
  2. the run library('knitr')
pandoc('filename.md', format='docx')
I assume you have 'knitr' package installed. You can change the 'filename' and 'docx' with 'latex' or 'html' or any formats that supported by Pandoc.

Review Cikapundung and Bandung Groundwater Basin

“version 10/05/2015”

Contents

  1. Introduction
  2. Regional Overview
    2.1 Geomorphology
    2.2 Volcanic Stratigraphy
    2.3 Hydrostratigraphy unit (HSU)
  3. Hydrological analysis and modeling
  4. Economic and socio-environment
  5. Research questions

1. Introduction

This catchment is part of Bandung Basin in West Java Province, Java Island, Indonesia. The Bandung Basin area is 348,786 hectare (ha), consists of: conservation area (54%) and built area (46%). The Cikapundung Catchment is 43,439 ha, crosses several cities and regencies: City of Bandung and Cimahi and Regency of Bandung, West Bandung, and Subang. The average run-off is 529,5 juta m3/thn and sedimentation rate of 1.023.347 ton/thn (BPLHD 2010). and this is what you'll get... neat huh... :-) docx result

14 May 2014

#sci-writing: drafting the old fashioned way




This post is related to:
  1. http://erwinirawansblog.blogspot.com.au/2014/07/bibliography-and-markdown.html
  2. http://erwinirawansblog.blogspot.com.au/2014/05/toc-in-markdown-doc.html

Why I draft mostly in txt or rtf format: 

  • it's light on my drive space, 
  • notepad is always in everyone's machine n smartphones, 
  • it's virusfree
  • it allows me to move freely across multiple OS (Mac,Win,Linux), 
  • it allows me to move between wordprocessor (even different Word version can give u trouble)
When drafting, I usually put my graphs and tables list at the end of the file. 

After enough drafting, then I polish the format, insert the graphs-tables, and make a second n 3rd draft in LibreOffice or LaTeX.

Or it even goes simpler for R users:

  • Type your draft using R Markdown
  • Then with "knitr" package type on your R prompt:

> library(knitr)
> pandoc('foo.md', format='html')  # to convert from markdown to HTML
> pandoc('foo.md', format='latex') # to convert from markdown to LaTeX/PDF        > pandoc('foo.md', format='docx')  # to convert from markdown to MS Word          > pandoc('foo.md', format='odt')   # to convert from markdown to OpenDocument

:-) {@dasaptaerwin}