## ----child = "../setup.Rmd"------------------------------------------- ## ----setup, include=FALSE--------------------------------------------- # R options options( htmltools.dir.version = FALSE, dplyr.print_min = 6, dplyr.print_max = 6, tibble.width = 65, width = 65 ) # figure height, width, dpi knitr::opts_chunk$set(echo = TRUE, fig.width = 8, fig.asp = 0.618, out.width = "60%", fig.align = "center", dpi = 300, message = FALSE) # ggplot2 ggplot2::theme_set(ggplot2::theme_gray(base_size = 16)) # set seed set.seed(1234) # fontawesome htmltools::tagList(rmarkdown::html_dependency_font_awesome()) # magick dev.off <- function(){ invisible(grDevices::dev.off()) } # conflicted library(conflicted) conflict_prefer("filter", "dplyr") # xaringanExtra library(xaringanExtra) xaringanExtra::use_panelset() # output number of lines hook_output <- knitr::knit_hooks$get("output") knitr::knit_hooks$set(output = function(x, options) { lines <- options$output.lines if (is.null(lines)) { return(hook_output(x, options)) # pass to default hook } x <- unlist(strsplit(x, "\n")) more <- "..." if (length(lines)==1) { # first n lines if (length(x) > lines) { # truncate the output, but add .... x <- c(head(x, lines), more) } } else { x <- c(more, x[lines], more) } # paste these lines together x <- paste(c(x, ""), collapse = "\n") hook_output(x, options) }) ## ----packages, echo=FALSE, message=FALSE, warning=FALSE--------------- library(tidyverse) library(magick) library(Tmisc) #library(dsbox) ## ----message=FALSE---------------------------------------------------- starwars ## --------------------------------------------------------------------- glimpse(starwars) ## ----eval = FALSE----------------------------------------------------- ## ?starwars ## ----echo=FALSE------------------------------------------------------- knitr::include_graphics("img/starwars-help.png") ## --------------------------------------------------------------------- nrow(starwars) # number of rows ncol(starwars) # number of columns dim(starwars) # dimensions (row column) ## ----echo=FALSE, out.width="80%"-------------------------------------- knitr::include_graphics("img/tidy-1.png") ## ----dplyr-part-of-tidyverse, echo=FALSE, out.width="70%", caption = "dplyr is part of the tidyverse"---- knitr::include_graphics("img/dplyr-part-of-tidyverse.png") ## ----message=FALSE---------------------------------------------------- hotels <- read_csv("data/hotels.csv") ## ----output.lines=18-------------------------------------------------- names(hotels) ## ----output.lines=18-------------------------------------------------- glimpse(hotels) ## --------------------------------------------------------------------- select(hotels, lead_time) ## ----eval=FALSE------------------------------------------------------- ## select( #<< ## hotels, ## lead_time ## ) ## ----eval=FALSE------------------------------------------------------- ## select( ## hotels, #<< ## lead_time ## ) ## ----eval=FALSE------------------------------------------------------- ## select( ## hotels, ## lead_time #<< ## ) ## --------------------------------------------------------------------- select( hotels, lead_time ) ## --------------------------------------------------------------------- select(hotels, lead_time) ## --------------------------------------------------------------------- select(hotels, hotel, lead_time) ## --------------------------------------------------------------------- hotels %>% select(hotel, lead_time) ## --------------------------------------------------------------------- hotels %>% select(hotel, lead_time) %>% arrange(desc(lead_time)) ## --------------------------------------------------------------------- hotels %>% #<< select(hotel, lead_time) %>% arrange(desc(lead_time)) ## --------------------------------------------------------------------- hotels %>% select(hotel, lead_time) %>% #<< arrange(desc(lead_time)) ## --------------------------------------------------------------------- hotels %>% select(hotel, lead_time) %>% arrange(desc(lead_time)) #<< ## ----magritte, echo=FALSE, out.width="90%", caption = "Magritte's pipe"---- knitr::include_graphics("img/magritte.jpg") ## ----magrittr, echo=FALSE, out.width="100%", caption = "magrittr's pipe"---- knitr::include_graphics("img/magrittr.jpg") ## ----eval=FALSE------------------------------------------------------- ## park(drive(start_car(find("keys")), to = "work")) ## ----eval=FALSE------------------------------------------------------- ## find("keys") %>% ## start_car() %>% ## drive(to = "work") %>% ## park() ## ----error=TRUE------------------------------------------------------- hotels + select(hotel, lead_time) ## ----eval=FALSE------------------------------------------------------- ## hotels %>% ## select(hotel, lead_time) ## ----echo=FALSE, output.lines=6--------------------------------------- hotels %>% select(hotel, lead_time) ## ----error=TRUE------------------------------------------------------- ggplot(hotels, aes(x = hotel, fill = deposit_type)) %>% geom_bar() ## ----out.width="25%"-------------------------------------------------- ggplot(hotels, aes(x = hotel, fill = deposit_type)) + geom_bar() ## ----eval=FALSE------------------------------------------------------- ## ggplot(hotels,aes(x=hotel,y=deposit_type))+geom_bar() ## ----eval=FALSE------------------------------------------------------- ## ggplot(hotels, aes(x = hotel, y = deposit_type)) + ## geom_bar() ## ----echo=FALSE------------------------------------------------------- starwars %>% select(name, height, mass) ## ----echo=FALSE------------------------------------------------------- starwars %>% group_by(gender) %>% summarize(avg_ht = mean(height, na.rm = TRUE)) ## ----eval=FALSE------------------------------------------------------- ## starwars %>% ## select(name, height, mass) ## ----eval=FALSE------------------------------------------------------- ## starwars %>% ## group_by(gender) %>% ## summarize( ## avg_ht = mean(height, na.rm = TRUE) ## )