HOW TO R markdown
- Get link
- X
- Other Apps
This is like a summary how to start to finish the R markdown notebook (RMD.file)
Great! VDO
Overview

Earlier in this course, you created a visualization using the ggplot() function. You also learned how to create an R Markdown notebook. In this activity, you will combine and apply your knowledge by adding the code you used to create a visualization to a RMarkdown notebook.
By the time you complete this activity, you will be able to make and format an .rmd file containing the visualizations you created using ggplot2. This will allow you to track and share analysis and also share the R code you use to create visualizations. This will prove useful if you want to give other data analysts an interactive way to test out your code and better communicate findings to your stakeholders and colleagues.
Add code chunks to your RMarkdown notebook

1. To start, log in to your RStudio Cloud account.
2. Next, open a new R Markdown notebook and create a code block section. Notebook chunks can be inserted quickly using the keyboard shortcut Ctrl + Alt + I (Windows) or Cmd + Option + I (Mac). Code chunks can also be added using the Insert menu in the editor toolbar.

Code chunks are designated in R Markdown with delimiters. A delimiter is a character that indicates the beginning or end of a data item. In this case, the code chunk is marked using three ticks followed by a curly bracket, descriptive text, and a closed curly bracket. You then have an empty space to add the appropriate code. Here is the general syntax:
```{r}
```
When creating code chunks, it is useful to keep in mind that the output of the code chunk will appear immediately after the chunk when it is executed. Because of that, it is good practice to split chunks that produce multiple outputs into two or more chunks. That way, each code chunk only produces one output, which can be easier for users to execute and explore.
3. Using the code from your ggplot() visualization, create two new chunks. Type the following in the first code chunk to call the required libraries, load the penguins data, and return a view of the penguins data:
```{r ggplot for penguin data}
library(ggplot2)
library(palmerpenguins)
data(penguins)
View(penguins)
```
Note that the only output from the code chunk is a tabular view of your data as result of the View function.
4. Then, type the following in the second code chunk to create the visualization:
```{r ggplot for penguin data visualization}
ggplot(data = penguins) +
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
```
5. Finally, run each code chunk to examine the results. You might recognize this visualization from a previous activity.
Congratulations! You can now add your visualizations to an R Markdown notebook. Code chunks are a great way to explain your data analysis process and allow other users to explore your work. Try adding as many code chunks to your R Markdown notebook as possible to make your notebook an interactive experience.
Export your R Markdown notebook

For this activity, you will export a notebook you created in previous activities as both an html document and a pdf.
You will use the Knit option to export your work. This will allow you to convert the Rmd file to another file type that is more readable and useful to other users.
To begin, follow these steps:
1. Open a document in RStudio. Then, find the Knit button on the toolbar at the top of your document window. Click it to open a dropdown menu with a few export options.
2. Click on the option that matches the file type you want to export your notebook as. Begin with html. Once you click Knit to HTML, your console might take a moment to process. When it is finished, it will automatically open your new file.

3. Now, repeat Step 2 but select Knit to PDF. Explore how these file formats are different from your original Rmd file.
4. To download the files you exported, find them in the Files explorer in the bottom-right of the screen.
5. Check the boxes of the file(s) you want to download and click the More dropdown.

6. Click Export… and name the file by a title that will help you find it later. Click Download. Now your exported file will be downloaded to your computer.

After you have successfully exported and downloaded your R Markdown notebook, you can share it with a friend, colleague, or the discussion forums to get feedback. After you have gotten their feedback, you can make revisions and continue to share your work to refine your notebook even more.
Sharing your R Markdown notebook is a great way to give other users insights into your data analysis. R Markdown comes with a lot of different options for exporting notebooks, so you can choose the best file type for your needs.
Understanding how to export your R Markdown notebooks will make documenting and sharing your data analysis process easier. Now that you are familiar with this process, you can export your own notebooks for future projects.
Example template in RStudio

You’re already familiar with a template in R Markdown from earlier videos and activities. When you create a new R Markdown document from the RStudio menu (File -> New File -> R Markdown), a default example document appears in the RStudio source editor:

Many customized templates in R packages have a similar structure that includes a YAML header, code chunks, and text headers.
R packages with templates

Some popular packages with templates for R Markdown include the following:
The vitae package contains templates for creating and maintaining a résumé or curriculum vitae (CV)
The rticles package provides templates for various journals and publishers
The learnr package makes it easy to turn any R Markdown document into an interactive tutorial
The bookdown package facilitates writing books and long-form articles
The flexdashboard package lets you publish a group of related data visualizations as a dashboard
Access the CV template in RStudio

To examine the CV template included in the “vitae” package, follow these steps:
1. First, log in to RStudio.
2. In the console, type install.packages("vitae") to install the vitae package.
3. Type library(vitae) to load the package.
4. You can access available templates in the R Markdown dialog box that appears when you create a new file. To create a new file in R Markdown, click File > New File > R Markdown.

5. From the R Markdown dialog box, click From Template to access a list of R Markdown templates provided by all installed packages.

In the viewer, you may notice some template options from the vitae package: Curriculum Vitae (Awesome-CV format), Curriculum Vitae (Hyndman format), Curriculum Vitae (ModernCV format), etc. These are different types of CV templates.
6. Scroll down and click Curriculum Vitae (Twenty seconds format).
7. Add a name for the new file directory that will contain the files bundled in the template, such as “CV-Example.”
8. Finally, click OK.
Convert the template to PDF format

The template will appear in the source editor pane. It contains a YAML header, code chunks, and text headers, just like the default example document that appears when you create a new R Markdown document. The example CV uses the scientist Marie Curie, the first woman to win a Nobel Prize (and the first person to win two Nobel Prizes). The YAML header contains entries for general information, such as name, address, phone number, and more.

If you scroll down, you’ll find header text that introduces separate sections for topics like personal information:

To display the output format of the template, click Knit to render the file. You don't need to open the dropdown menu to select a format, as this template defaults to a pdf.
Note: If your browser blocks pop-ups and returns an error, make sure to click Try Again.
This will result in a pdf that displays the custom template for the CV:

This pdf can be found and downloaded from the folder in the Files tab of the lower-right console.
The information in the YAML Header appears on the left side, and the information in the various sections appears on the right side.
You can replace these details with your own information to adapt the template for your own needs.
- Get link
- X
- Other Apps
Comments
Post a Comment