Data management, markdown, latex, notebooks, Git (X. de Pedro) - http://seeds4c.org/markdown2020
1.1. Introduction
1.1.1. Data Management
See: |
|
See also Radiant as GUI to allow business analytics using R and Shiny
http://vnijs.github.io/radiant/
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
- Programming: Integrate Radiant’s analysis functions with your own R-code
- Context: Data and examples focus on business applications
https://www.youtube.com/watch?v=7L3hDpLw53I
1.1.2. Markdown
1.1.2.1. Introduction
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:
- Introduction - https://rmarkdown.rstudio.com/lesson-1.html
- How It Works - https://rmarkdown.rstudio.com/lesson-2.html
- Code Chunks - https://rmarkdown.rstudio.com/lesson-3.html
- Inline Code - https://rmarkdown.rstudio.com/lesson-4.html
- Code Languages - https://rmarkdown.rstudio.com/lesson-5.html
- Parameters - https://rmarkdown.rstudio.com/lesson-6.html
- Tables - https://rmarkdown.rstudio.com/lesson-7.html
- Markdown Basics - https://rmarkdown.rstudio.com/lesson-8.html
- Output Formats - https://rmarkdown.rstudio.com/lesson-9.html
- Notebooks - https://rmarkdown.rstudio.com/lesson-10.html
- Slide Presentations - https://rmarkdown.rstudio.com/lesson-11.html
- Dashboards - https://rmarkdown.rstudio.com/lesson-12.html
- Websites - https://rmarkdown.rstudio.com/lesson-13.html
- Interactive Documents - https://rmarkdown.rstudio.com/lesson-14.html
- 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:
- https://support.rstudio.com/hc/en-us/articles/200552056-Using-Sweave-and-knitr
- https://rpubs.com/YaRrr/SweaveIntro
- http://keziamanlove.com/latex-in-rstudio/
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 ( 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 ( 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 What is LaTeX?
- 2 Why learn LaTeX?
- 3 Writing your first piece of LaTeX
- 4 The preamble of a document
- 5 Adding a title, author and date
- 6 Adding comments
- 7 Bold, italics and underlining
- 8 Adding images
- 9 Creating lists in LaTeX
- 10 Adding math to LaTeX
- 11 Basic Formatting
- 12 Creating tables
- 13 Adding a Table of Contents
- 14 Downloading your finished document
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:
- https://www.datacamp.com/community/blog/jupyter-notebook-r
- http://minimaxir.com/2017/06/r-notebooks/
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.
More information:
https://github.com/rstudio/webinars/blob/master/25-Notebooks/R%20Notebooks%20Webinar.pdf
1.1.6. Git
https://gitlab.com/radup/curs-r-introduccio/-/blob/master/codi/10.compartir.via.git.pdf
Knitted from:
https://gitlab.com/radup/curs-r-introduccio/-/blob/master/codi/10.compartir.via.git.Rmd
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
- In RStudio, create a new R Markdown document by choosing
File
|New
|R Markdown
. - Click the
Knit HTML
button in the doc toolbar to preview your document. - In the preview window, click the
Publish
button. - 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
- Create an account at gitlab.com (free for public and private git repos)
- Create an almost empty repository (with some readme file)
- Clone itthe repository locally in your RStudio IDE, by means of "Create project from Version Control > git > ..."
- Add more files (for instance, the previous Rmd file you created) under git
- Push your changes to your repository
- Send an email to the session professor ( xavier.depedro@seeds4c.org ) with the link of your gitlab project with your Rmd file/s.
1.3. Useful links & tips
1.3.1. Some links
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
Addins List: install with:
install.packages("addinslist")
Recommended Rstudio addins related to the contents of this session:
Data Management/Wrangling:
- Radiant
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