Communication Flashcards
What is the diff btw R markdown and R notebook?
R notebook files show the output of code chunks inside the editor, while hiding the console, when they are edited in RStudio. This contrasts with R markdown files, which show their output inside the console, and do not show output inside the editor. This makes R notebook documents appealing for interactive exploration.
R markdown files can be knit to a variety of formats including HTML, PDF, and DOCX. R notebooks can only be knit to HTML, with the extension .nb.html. The output of an R notebook keeps a copy of the original .Rmd source. If a .nb.html file is opened in RStudio, the source of the .Rmd file can be extracted and edited. In contrast, there is no way to recover the original source of an R markdown file from its output, except through the parts that are displayed in the output itself.
R markdown files and R notebooks differ in the value of output in their YAML headers
ouptut: html_notebook
- –
while for R markdwon is ouptut: html_document
Rnotebook have preview option while Rmarkdwon does not have preview option only knit
more here : https://stackoverflow.com/questions/43820483/difference-between-r-markdown-and-r-notebook/43898504#43898504
What will happned if you change the rnotebook YAML header with r markdwon heder?
Copying the YAML header from an R notebook to a R markdown file changes it to an R notebook, and vice-versa. More specifically, an .Rmd file can be changed to R markdown file or R notebook by changing the value of the output key in the header.
The RStudio IDE and the rmarkdown package both use the YAML header of an .Rmd file to determine the document-type of the file.
Knitr shorcut ?
Cmd/Ctrl + Alt + K.
What is out YML header for PDF , HTML , Word?
output: pdf_document , output: html_document , output: word_document , pdf_document: default
How to format text in R markdown?
italic or italic
bold __bold__code
superscript^2^ and subscript~2~
how to form heading in R markdown?
1st Level Header
2nd Level Header
3rd Level Header
.
How to make list in R markdown?
- Bulleted list item 1
- Item 2
- Item 2a
- Item 2b
- Numbered list item 1
- Item 2. The numbers are incremented automatically in the output
Insert code chunk, 3 ways?
- Cmd/Ctrl + Alt + I
- The “Insert” button icon in the editor toolbar.
- By manually typing the chunk delimiters
{r} and
.
chunk/run code in markdwon?
Cmd/Ctrl + Shift + Enter, which runs all the code in the chunk
Chunk has a name. how can we create name for chunk?
Chunks can be given an optional name: ```{r by-name}. This has three advantages:
- You can more easily navigate to specific chunks using the drop-down code navigator in the bottom-left of the script editor:
- Graphics produced by the chunks will have useful names that make them easier to use elsewhere.
- You can set up networks of cached chunks to avoid re-performing expensive computations on every run.
Chunk has option. What are they usfeul for ?
Chunk output can be customised with options, arguments supplied to chunk header. Knitr provides almost 60 options that you can use to customize your code chunks.
Chunk option eval = FALSE . what does this means?
eval = FALSE prevents code from being evaluated. (And obviously if the code is not run, no results will be generated). This is useful for displaying example code, or for disabling a large block of code without commenting each line.
Include = FALSE chunk option?
Include = FALSE runs the code, but doesn’t show the code or results in the final document. Use this for setup code that you don’t want cluttering your report.
echo= FALSE, chunk option?
echo = FALSE prevents code, but not the results from appearing in the finished file. Use this when writing reports aimed at people who don’t want to see the underlying R code.
message= FALSE , what does it mean? . the chunk options are all True by default
message = FALSE or warning = FALSE prevents messages or warnings from appearing in the finished file.
result = hide?
results = ‘hide’ hides printed output; fig.show = ‘hide’ hides plots.
error = TRUE
error = TRUE causes the render to continue even if code returns an error. This is rarely something you’ll want to include in the final version of your report, but can be very useful if you need to debug exactly what is going on inside your .Rmd. It’s also useful if you’re teaching R and want to deliberately include an error. The default, error = FALSE causes knitting to fail if there is a single error in the document.
How to use Table in Rmarkdwon?
By default, R Markdown prints data frames and matrices as you’d see them in the console:
If you prefer that data be displayed with additional formatting you can use the knitr::kable function. The code below generates Table 27.1.
knitr::kable(
mtcars[1:5, ],
caption = “A knitr kable.”
)
What is Caching in Rmarkdwon?
Normally, each knit of a document starts from a completely clean slate. This is great for reproducibility, because it ensures that you’ve captured every important computation in code. However, it can be painful if you have some computations that take a long time. The solution is cache = TRUE. When set, this will save the output of the chunk to a specially named file on disk. On subsequent runs, knitr will check to see if the code has changed, and if it hasn’t, it will reuse the cached results.
e.g
```{r processed_data, cache = TRUE, dependson = “raw_data”}
processed_data %
filter(!is.na(import_var)) %>%
mutate(new_variable = complicated_transformation(x, y, z))
~~~
Global options , what does that mean in Markdown?
As you work more with knitr, you will discover that some of the default chunk options don’t fit your needs and you want to change them. You can do this by calling knitr::opts_chunk$set() in a code chunk. For example, when writing books and tutorials I set:
knitr::opts_chunk$set( comment = "#>", collapse = TRUE ) This uses my preferred comment formatting, and ensures that the code and output are kept closely entwined. On the other hand, if you were preparing a report, you might set:
knitr::opts_chunk$set(
echo = FALSE
)
That will hide the code by default, so only showing the chunks you deliberately choose to show (with echo = TRUE). You might consider setting message = FALSE and warning = FALSE, but that would make it harder to debug problems because you wouldn’t see any messages in the final document.
How to use inline code?
We have data about r nrow(diamonds)
diamonds. Only r nrow(diamonds) - nrow(smaller)
are larger than 2.5 carats. The distribution of the remainder is shown below:
When the report is knit, the results of these computations are inserted into the text:
We have data about 53940 diamonds. Only 126 are larger than 2.5 carats. The distribution of the remainder is shown below:
what does format() does ?
When inserting numbers into text, format() is your friend. It allows you to set the number of digits so you don’t print to a ridiculous degree of accuracy, and a big.mark to make numbers easier to read. I’ll often combine these into a helper function:
comma [1] “3,452,345”
comma(.12358124331)
#> [1] “0.12”
Troubleshooting Rmarkdwon?
he first thing you should always try is to recreate the problem in an interactive session. Restart R, then “Run all chunks” (either from Code menu, under Run region), or with the keyboard shortcut Ctrl + Alt + R. If you’re lucky, that will recreate the problem, and you can figure out what’s going on interactively.
also , Check the working directory is what you expect by including getwd() in a chunk.
Next, brainstorm all the things that might cause the bug. You’ll need to systematically check that they’re the same in your R session and your R markdown session. The easiest way to do that is to set error = TRUE on the chunk causing the problem, then use print() and str() to check that settings are as you expect