Bird
Raised Fist0
R Programmingprogramming~5 mins

Code chunks and output in R Programming - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a code chunk in R Markdown?
A code chunk is a section in R Markdown where you write R code that will be run when the document is knitted. It starts with ```{r} and ends with ```.
Click to reveal answer
beginner
How do you show the output of a code chunk in the knitted document?
By default, the output of the code inside a chunk is shown below the code when you knit the document.
Click to reveal answer
beginner
What does the chunk option `echo=FALSE` do?
It hides the R code in the output but still shows the results of running the code.
Click to reveal answer
intermediate
How can you prevent both code and output from showing in the final document?
Use chunk options `echo=FALSE` and `results='hide'` together to hide both code and output.
Click to reveal answer
intermediate
What is the purpose of the chunk option `eval=FALSE`?
It prevents the code inside the chunk from running, so no output or results are generated.
Click to reveal answer
In R Markdown, which symbol starts a code chunk?
A```{r}
B~~~{r}
C<code>
D//{r}
What happens if you set `echo=FALSE` in a code chunk?
ACode runs twice
BCode and output are both hidden
CCode is shown but output is hidden
DCode is hidden but output is shown
Which chunk option stops the code from running?
Aeval=FALSE
Becho=FALSE
Cresults='hide'
Dinclude=FALSE
How do you hide both code and output in a chunk?
Aecho=TRUE only
Becho=FALSE and results='hide'
Ceval=FALSE only
DNo option needed
What is the default behavior of code chunks in R Markdown?
AShow output only
BHide code and output
CShow code and output
DShow code only
Explain what a code chunk is and how its output appears in an R Markdown document.
Think about how you write and run R code inside a document.
You got /3 concepts.
    Describe how to control the visibility of code and output using chunk options.
    Chunk options let you decide what the reader sees.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is the purpose of an R code chunk in an R Markdown document?
      easy
      A. To run R code and show its output inline with text
      B. To write plain text without any code
      C. To create a table of contents automatically
      D. To add images to the document

      Solution

      1. Step 1: Understand R Markdown structure

        R Markdown uses code chunks to embed R code inside text documents.
      2. Step 2: Purpose of code chunks

        Code chunks run R code and display results inline, mixing code and output.
      3. Final Answer:

        To run R code and show its output inline with text -> Option A
      4. Quick Check:

        Code chunk = run code + show output [OK]
      Hint: Code chunks run code and show output together [OK]
      Common Mistakes:
      • Thinking code chunks only hold text
      • Confusing code chunks with document sections
      • Assuming code chunks add images automatically
      2. Which of the following is the correct way to start an R code chunk in R Markdown?
      easy
      A. {r}```
      B. ```r
      C. ```{r}
      D. {r}

      Solution

      1. Step 1: Recall R Markdown chunk syntax

        R code chunks start with three backticks followed by {r} exactly.
      2. Step 2: Check each option

        Only ```{r} correctly starts a chunk; others are invalid syntax.
      3. Final Answer:

        ```{r} -> Option C
      4. Quick Check:

        Chunk start = ```{r} [OK]
      Hint: Start chunks with three backticks and {r} [OK]
      Common Mistakes:
      • Omitting curly braces around r
      • Using only two backticks
      • Confusing HTML tags with chunk syntax
      3. What will be the output of this R code chunk?
      ```{r}
      x <- 5
      x * 2
      ```
      medium
      A. [1] 10
      B. 10
      C. x * 2
      D. Error: object 'x' not found

      Solution

      1. Step 1: Understand the code execution

        Variable x is assigned 5, then multiplied by 2, resulting in 10.
      2. Step 2: R output format

        R prints numeric output with index [1] before the value.
      3. Final Answer:

        [1] 10 -> Option A
      4. Quick Check:

        5 * 2 = 10 with [1] prefix [OK]
      Hint: R prints numeric output with [1] prefix [OK]
      Common Mistakes:
      • Ignoring the [1] prefix in output
      • Expecting just '10' without index
      • Thinking code prints the expression itself
      4. Identify the error in this R code chunk:
      ```{r}
      print("Hello)
      ```
      medium
      A. Missing closing curly brace for chunk
      B. Missing closing quote in the string
      C. Incorrect function name 'print'
      D. No error, code runs fine

      Solution

      1. Step 1: Check the string syntax

        The string starts with a double quote but lacks the closing double quote.
      2. Step 2: Consequence of missing quote

        This causes a syntax error because the string is not properly closed.
      3. Final Answer:

        Missing closing quote in the string -> Option B
      4. Quick Check:

        Unclosed string causes syntax error [OK]
      Hint: Check all quotes are closed in strings [OK]
      Common Mistakes:
      • Assuming chunk braces cause error
      • Thinking print is misspelled
      • Ignoring missing quote errors
      5. You want to run an R code chunk but hide the code and only show the output. Which chunk option should you use?
      hard
      A. ```{r, include=FALSE}
      B. ```{r, eval=FALSE}
      C. ```{r, message=FALSE}
      D. ```{r, echo=FALSE}

      Solution

      1. Step 1: Understand chunk options

        echo=FALSE hides the code but runs it, showing output only.
      2. Step 2: Compare other options

        eval=FALSE skips running code, include=FALSE hides both code and output, message=FALSE hides messages only.
      3. Final Answer:

        ```{r, echo=FALSE} -> Option D
      4. Quick Check:

        echo=FALSE hides code, shows output [OK]
      Hint: Use echo=FALSE to hide code but show output [OK]
      Common Mistakes:
      • Using eval=FALSE hides output too
      • Using include=FALSE hides everything
      • Confusing message=FALSE with code hiding
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard