Get access to all lessons in this course.
-
RMarkdown
- Why Use RMarkdown?
- RMarkdown Overview
- YAML
- Text
- Code Chunks
- Wrapping Up
-
Data Wrangling and Analysis
- Getting Started
- The Tidyverse
- select
- mutate
- filter
- summarize
- group_by
- count
- arrange
- Create a New Data Frame
- Crosstabs
- Wrapping Up
-
Data Visualization
- An Important Workflow Tip
- The Grammar of Graphics
- Scatterplots
- Histograms
- Bar Charts
- color and fill
- scales
- Text and Labels
- Plot Labels
- Themes
- Facets
- Save Plots
- Wrapping Up
-
Wrapping Up
- You Did It!
Fundamentals of R
Code Chunks
This lesson is locked
This lesson is called Code Chunks, part of the Fundamentals of R course. This lesson is called Code Chunks, part of the Fundamentals of R course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
Your Turn
Change the default chunk options in the setup code chunk so that
echo = FALSE
. Knit your report and notice what is different.Change the chunk options in the cars code chunk so that
include = FALSE
. Knit your report and notice what is different.Add a new code chunk (don’t worry about naming it). In it, load the package
skimr
(hint: use thelibrary
function). Then, use theskim
function that you learned about in the Getting Started course (see the Examine our Data lesson in particular) to skim thecars
data frame. Knit your report to see the output.
Learn More
Many people print out the RMarkdown cheatsheet and keep it as a reference. It’s got a lot of information on YAML, Markdown text, and code chunks.
Interested in why you might considering naming code chunks? Maëlle Salmon wrote an article on this topic. Note that for most beginners, naming code chunks isn’t necessary. It’s only when you’re doing more complicated things with RMarkdown that it makes a difference.
You need to be signed-in to comment on this post. Login.
Lucilla Piccari
March 20, 2021
When knitting the added code chunk (the one where we used the skim() function) I got a different-looking result as the one demonstrated on the solution video, more "polished" looking, and I'm not sure why.
David Keyes
March 20, 2021
Is your result more polished or is the one in the video more polished? Do you mind posting a screenshot of what yours looks like? You can use imgur or something similar to do so.
Atlang Mompe
March 21, 2021
Hi David,
The links for both these sections don't work: On another note, if you have issues getting the histograms to display properly and you’re using Windows, there is a known issue. Please see the skimr documentation for potential solutions.
Thanks, Atty
David Keyes
March 22, 2021
Thanks, Atty! I updated the links so they work now.
Jody Oconnor
March 23, 2021
My html output (after checking that my code is exactly the same as the example solution), also looks more 'polished'... there are no hash tags and the font is the report font, not the code font, and a few other differences. The warnings are not shown in the report, even before adding 'warning = FALSE, message = FALSE' Not sure what I would need to do to make the warnings/messages appear in the report.
David Keyes
March 24, 2021
What format did you knit to?
ODILE DOREUS
August 8, 2021
Hello, I did the steps but having error messages. So I went ahead and install tydiverse still not work.
Error: ERROR: no permission to install to directory ‘/usr/local/lib/R/site-library’ Warning in install.packages : installation of package ‘tidyverse’ had non-zero exit status The downloaded source packages are in ‘/tmp/RtmpGXw6hh/downloaded_packages’ > install.packages("skimr") Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) Warning in install.packages : 'lib = "/usr/local/lib/R/site-library"' is not writable Would you like to use a personal library instead? (yes/No/cancel)
David Keyes
August 9, 2021
What operating system are you working with? And are you working on a computer with a network drive by chance?
ODILE DOREUS
August 9, 2021
Hi, my operating system is Windows 10 Pro and I am not working with a network drive.
Lina Khan
September 18, 2021
Hi David, similar to notes below, the report looks more polished, no ## and stuff.
https://imgur.com/369hjiU
David Keyes
September 20, 2021
This is likely just a difference in how things print on a Mac (which I use) versus Windows (which I'm guessing you use). Since this isn't something you'd likely print in a report for a client (I use skim() to just get a sense of my data before doing analysis), let's not worry too much about it!
Payal Mulchandani
September 20, 2021
Getting this error when I try to due Include=False: > ```{r cars, include=FALSE} Error: attempt to use zero-length variable name
David Keyes
September 21, 2021
Can you post your full RMarkdown code as a GitHub gist so I can see what you have? I can't tell if something weird is happening with the text formatting when you post a comment here on the website.
Lindsay Quarles
October 18, 2021
Everything worked for me except the include = FALSE. The output never went away. I did not get any error message, the output just remained.
David Keyes
October 18, 2021
The output will remain until you clear it (though because you added include = FALSE it won't reappear in the future). To remove the output, click Edit > Clear All Output. See https://show.rfor.us/JKdfOx
Sara Cifuentes
March 24, 2022
Is there a video in this section?
Sara Cifuentes
March 24, 2022
Yesterday something happened with videos, but now it is working!
Charlie Hadley
March 24, 2022
I'm glad it's working now!
Sara Cifuentes
March 24, 2022
Thank you Charlie!
Zaynaib Giwa
March 28, 2022
Hello everyone! I know that we don't go super in-depth with R-Markdown in this class but I was wondering what would be a scenario for giving a code chunk a name? Is it for reusability purposes if you need the same graph multiple times in a report?
Charlie Hadley
March 29, 2022
Hello Zaynaib! This is a good question! There are two reasons that naming code chunks can be useful:
Make sure your code chunks only contain letters, numbers or dashes. Any other characters can break internal cross referencing.
This article gives some more information about the use of code chunk names.
Cheers,
Charlie
Josh Gutwill
September 26, 2022
Where do the data for cars and for pressure come from? I don't see them in the getting-started-master folder, nor in the faketucky dataset. summary(cars) is clearly calling that data, but I don't see how. :-) Thanks!
Charlie Hadley
September 27, 2022
Great question, Josh!
When we first load up RStudio it loads "base R" which is a collection of many packages, including the {datasets} package which contains the cars and pressure data.
Matt Kropp
September 28, 2022
Is there a way to knit code automatically every time it is run?
Charlie Hadley
September 28, 2022
Hi Matt! When we knit an .Rmd document all of the code chunks get run (from top to bottom). Could you help me understand your question a little bit more? Thanks - Charlie
Matt Kropp
September 29, 2022
Now I understand. I didn't realize when you knit it runs the whole code. Can you put a timestamp in the header or footer?
Ellen Wilson
October 5, 2022
Similar to what others have commented, I get a more polished looking result from the cars skim. No warnings or messages, no hashtags, and it has a data summary at the top (telling the number of columns and rows). I'm using a Mac. It doesn't seem like there was ever a definitive answer to what others said about this, and maybe it doesn't matter, but it is a bit puzzling.
David Keyes
October 5, 2022
I'm guessing this is just from a new version of the skimr package, but it's hard to know for sure. If you post a screenshot to imgur.com and share a link here I can take a look.
Julieth Silao
October 31, 2022
https://juliethsilao.imgur.com/all/?third_party=1
link to imgur.com to see my output
David Keyes
October 31, 2022
Unfortunately, that link doesn't work.
Caitlin Tracey-Miller
October 11, 2022
I kept getting errors until I put my library request on the same line as the R code line. Is that strange?
Julieth Silao
October 31, 2022
Hello David. when i install new chuck and run skimr, i didnt get the summary report as in solution you provide
Dimeji Olawuyi
December 23, 2022
The warning message about the version disappeared when I uninstalled the version I was using and installed a recent version.
David Keyes
December 23, 2022
Glad you got it figured out! The error looks an extra close parentheses to me for what it's worth.