# Data management, markdown, latex, notebooks, Git (X. de Pedro) - http://seeds4c.org/markdown2020

## 1.1. Introduction

### 1.1.1. Data Management

 Using Tidyverse Cheatsheets: https://www.rstudio.com/resources/cheatsheets/ Installation: install.packages("tidyverse") Book: R for Data Science. Garrett Grolemund, Hadley Wickham. O’Reilly January 2017 First Edition http://r4ds.had.co.nz Welcome This is the website for “R for Data Science”. This book will teach you how to do data science with R: You’ll learn how to get your data into R, get it into the most useful structure, transform it, visualise it and model it. In this book, you will find a practicum of skills for data science. Just as a chemist learns how to clean test tubes and stock a lab, you’ll learn how to clean data and draw plots—and many other things besides. These are the skills that allow data science to happen, and here you will find the best practices for doing each of these things with R. You’ll learn how to use the grammar of graphics, literate programming, and reproducible research to save time. You’ll also learn how to manage cognitive resources to facilitate discoveries when wrangling, visualising, and exploring data. Exercises See: https://www.tidyverse.org

Radiant is an open-source platform-independent browser-based interface for business analytics in R.

Key features

• Explore: Quickly and easily summarize, visualize, and analyze your data
• Cross-platform: It runs in a browser on Windows, Mac, and Linux
• Reproducible: Recreate results and share work with others as a state-file or an Rmarkdown report
• Context: Data and examples focus on business applications

### 1.1.2. Markdown

#### 1.1.2.2. Short demo

RStudio

---
title: "Untitled"
author: "Ajuntament de Barcelona"
date: "28 febrer de 2018"
output: html_document
---

{r setup, include=FALSE}
knitr::opts_chunk\$set(echo = TRUE)


## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

{r cars}
summary(cars)


## Including Plots

You can also embed plots, for example:

{r pressure, echo=FALSE}
plot(pressure)


Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#### 1.1.2.3. Custom Example

See a custom example fetching and processing Public Health Data from Barcelona out of a PDF report with R:
http://rpubs.com/Xavi/ubiss16d3

#### 1.1.2.4. Lessons from RStudio

See all lessons from RStudio:

1. Introduction - https://rmarkdown.rstudio.com/lesson-1.html
2. How It Works - https://rmarkdown.rstudio.com/lesson-2.html
3. Code Chunks - https://rmarkdown.rstudio.com/lesson-3.html
4. Inline Code - https://rmarkdown.rstudio.com/lesson-4.html
5. Code Languages - https://rmarkdown.rstudio.com/lesson-5.html
6. Parameters - https://rmarkdown.rstudio.com/lesson-6.html
7. Tables - https://rmarkdown.rstudio.com/lesson-7.html
8. Markdown Basics - https://rmarkdown.rstudio.com/lesson-8.html
9. Output Formats - https://rmarkdown.rstudio.com/lesson-9.html
10. Notebooks - https://rmarkdown.rstudio.com/lesson-10.html
11. Slide Presentations - https://rmarkdown.rstudio.com/lesson-11.html
12. Dashboards - https://rmarkdown.rstudio.com/lesson-12.html
13. Websites - https://rmarkdown.rstudio.com/lesson-13.html
14. Interactive Documents - https://rmarkdown.rstudio.com/lesson-14.html
15. Cheatsheets - https://rmarkdown.rstudio.com/lesson-15.html

### 1.1.3. From Rmd to Shiny

See the gradients of interactivity and complexity going from interactive Rmd documents to full Shiny apps
https://github.com/rstudio/webinars/blob/master/75-reproducibility-in-production/reproducibility-in-production.pdf

### 1.1.4. Latex

The minimum content you need to include for LaTeX to build your document are a specification of the document class, indicators of document beginning and ending, and a little content. Comments in LaTeX are indicated with a leading “%” symbol.

You could also use other programs to write latex, such Texstudio (multiplatform, open source), among others.

See:

Example:

% trial .tex file %
\documentclass[10pt]{article}  % specifies document class (article) and point size (10pt)

\begin{document}               % starts document

\title{Example Document}       % specifies big, fancy title
\maketitle                     % constructs big, fancy title
\section{Section 1}            % makes a section header
Here is some text in section 1.  Section 1 also contains an itemized list:
\begin{itemize}              % initiates an itemized list
\item Here is an item in the list
\item Here is a second item
\end{itemize}                % ends itemized list

\section{Section 2}            % makes header for section 2
Some text on section 2 here.  Section 2 contains an enumerated list.
\begin{enumerate}            % initiates enumerated list
\item A first enumerated item
\item A second enumerated item
\end{enumerate}              % ends enumerated list

\end{document}                 % ends document

Once you have this script written into your .tex file in RStudio, hit the Compile .pdf button at the upper right of the script window. Doing so will prompt LaTeX (via RStudio) to compiled the .pdf document you’ve specified. It will store the .pdf file (along with several auxiliary files LaTeX files) in your working directory

#### Latex Collaborative Editors online

Overleaf

Overleaf ( https://www.overleaf.com ) is yet another online LaTeX editor. Similar to ShareLaTeX, it offers separate pricing plans for professionals and students. It also includes a free plan where you can sync with GitHub, check your revision history, and add multiple collaborators.

There’s a limit on the number of files you can create per project – so it could bother if you are a professional working with LaTeX documents most of the time.

ShareLatex

ShareLaTeX ( https://www.sharelatex.com ) is an online LaTeX editor. If you want someone (or a group of people) to collaborate on documents you are working on, this is what you need.

It offers a free plan along with several paid packages. Even the students of Harvard University & Oxford University utilize this for their projects. With the free plan, you get the ability to add one collaborator.

The paid packages let you sync the documents on GitHub and Dropbox along with the ability to record the full document history. You can choose to have multiple collaborators as per your plan. For students, there’s a separate pricing plan available.

#### 1.1.4.1. Learn LaTeX in 30 minutes

From sharelatex.com:
https://es.sharelatex.com/learn/Learn_LaTeX_in_30_minutes

You can practice online here: https://latexbase.com

### 1.1.5. Notebooks

You could use either R Notebook (using R Markdown from within RStudio) or Jupyter notebook, both of them allow you to embed R o rpython code inside, even if the first notebook is mainly coming from the R's community, and the second, from Python's community. See a few comparisons here:

We will stick in this session to the R Notebook from RStudio, while in further sessions in this course you might get introduced to Python Jupyter's notebook.

---
title: "R Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.

{r}
plot(cars)


Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

## 1.2. Hands-on work

### 1.2.1. Hello-World Demo

Follow this tutorial as a starting point:
https://medium.com/@sorenlind/create-pdf-reports-using-r-r-markdown-latex-and-knitr-on-windows-10-952b0c48bfa9

### 1.2.2. Upload it to RPubs

Create an account at RPubs (if you don't have one already), and upload the knitted html document you created in the previous step.

#### Getting Started with RPubs

RStudio lets you harness the power of R Markdown to create documents that weave together your writing and the output of your R code. And now, with RPubs, you can publish those documents on the web with the click of a button!

#### Prerequisites

You'll need R itself, RStudio (v0.96.230 or later), and the knitr package (v0.5 or later).

#### Instructions

1. In RStudio, create a new R Markdown document by choosing File | New | R Markdown.
2. Click the Knit HTML button in the doc toolbar to preview your document.
3. In the preview window, click the Publish button.
4. Send an email to the session professor ( xavier.depedro at seeds4c.org ) with the link of your gitlab project with your Rmd file/s.

### 1.2.3. Tweak this step-by-step markdown example

You can clone this step by step guide to learn the basics of RMarkdown (copy to a local file in your RStudio IDE), and tweak it to your own likes/needs.
https://gist.github.com/jeromyanglim/2716336

When finished, upload it to RPubs too.

### 1.2.4. Share your files with git at gitlab.com

1. Create an account at gitlab.com (free for public and private git repos)
2. Create an almost empty repository (with some readme file)
3. Clone itthe repository locally in your RStudio IDE, by means of "Create project from Version Control > git > ..."
4. Add more files (for instance, the previous Rmd file you created) under git
6. Send an email to the session professor ( xavier.depedro at seeds4c.org ) with the link of your gitlab project with your Rmd file/s.

R Pubs - Easy web publishing from R
Write R Markdown documents in RStudio. Share them on RPubs. (It’s free, and couldn’t be simpler!)
https://rpubs.com/

Rstudio Cloud (with free plan)
https://rstudio.cloud

An R Markdown Template for Academic Manuscripts
"You should consider no longer using LaTeX as a front-end for your manuscripts. Use a wrapper for LaTeX instead, like R Markdown...":
http://svmiller.com/blog/2016/02/svm-r-markdown-manuscript/

### 1.3.2. Some tips: selected Rstudio Addins

install.packages("addinslist")

Recommended Rstudio addins related to the contents of this session:
Data Management/Wrangling:

A Shiny interface to visualize, summarize, transform, and combine data

Working with R Markdown within RStudio IDE:

• Remedy
to easily embed rmarkdown tags in context where/when needed
• beautifyR
Format RMarkdown tables beautifully
• imageclipr
Copy and paste images from clipboard to Rmarkdown .Rmd files
• insertImage
Insert image into R Markdown with file chooser. Copy image file to project.
• inserttable
RStudio add-in facilitating insertion of nicely formatted tables in R markdown documents or plain R scripts
• splitChunk
Split code chunk in R Markdown

Seed: noun \ˈsēd\ : the beginning of something which continues to develop or grow