class: center, middle, inverse, title-slide # R Markdown tips and GPS data visualization ## R workshop ### Issac Lee ### 2021-02-26 --- class: center, middle, hide-logo # The University of Iowa ![](https://upload.wikimedia.org/wikipedia/en/thumb/4/49/University_of_Iowa_seal.svg/225px-University_of_Iowa_seal.svg.png) ## Statistics and Actuarial Science --- class: center, middle, hide-logo # Why RMarkdown? --- class: hide-logo <iframe width="1300" height="600" src="https://www.youtube.com/embed/s3JldKoA0zw?version=3&rel=0" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: center, middle # We use them in our daily life.. --- # **R**Markdown packages .pull-left[ * [**Bookdown**](https://bookdown.org/yihui/bookdown/) - pdf, epub, or html book * Writing a gitbook like [Prof. Tierney](http://homepage.stat.uiowa.edu/~luke/classes/STAT7400-2021/_book/index.html) * [**Blogdown**](https://bookdown.org/yihui/blogdown/) - Static website builder Hugo wrapper * Personal website like [me](theissaclee.com) * Theme: Academy, Apero (is developing by Rstudio) * [**Pagedown**](https://pagedown.rbind.io/): single html document generation. a.k.a. goodbye LaTeX? ] .pull-right[ * [**Distill**](https://rstudio.github.io/distill/): if you really like minimalizm. * Concept is developed by Google, package developed by Rstudio. * Suitible for academic writing * [**Xaringan**](https://slides.yihui.org/xaringan/#1): Presentation * Like the one you're looking at. * `xaringan themer` - Easy theme for `xaringan` * [**Thesisdown**](https://github.com/ismayc/thesisdown): Thesis with Rmd. ] --- class: center, middle # Use them wisely! # Useful 3 tips! --- # Use **R**md as **Py**md! * R package `reticulate` allows us to use Rmarkdown for handling `python` code. * https://rstudio.github.io/reticulate/index.html .pull-left[ * Use `python` code chunk in your `.Rmd` file! ````markdown ```{r} library(reticulate) ``` ```{python} import numpy import pandas ``` ```` ] .pull-right[ ![](https://rstudio.github.io/reticulate/images/reticulated_python.png) ] --- # Shortcuts! `Alt` + `Shift` + `K` shows all shortcuts! However, some are USEFUL, some are **NOT**! ### Useful **Shortcuts!** .pull-left[ add `%>%` operator: `Ctrl` + `Shift` + `m` add `<-` operator: `Alt` + - add cursors! : `Ctrl` + `Alt` + down or up search functions: `Ctrl` + . search commands: `Ctrl` + `Shift` + `p` ] .pull-right[ Run current chunk: `Ctrl` + `Alt` + `c` R code re-formatting: `Ctrl` + `Shift` + `a` Copy current below: `Shift` + `Alt` + down Insert `R` code chunk: `Ctrl` + `Alt` + i ] --- class: center, middle # However, so many syntax tho.. --- # Including images .pull-left[ Should I type all of these to insert just a picture? ````markdown ```{r chunkname, fig.cap="picture caption", fig.align="center", out.width="50%"} knitr::include_graphics("question.png") ``` ```` ] .pull-right[ <div class="figure" style="text-align: center"> <img src="./question.svg" alt="picture caption" width="50%" /> <p class="caption">picture caption</p> </div> ] --- # Tables .pull-left[ Should I memorize all these options for generating table? ````markdown ```{r} knitr::kable(head(as_tibble(mtcars[,1:3]), 5), position = "!h", col.names = c("col1", "col2", "col3"), caption = "caption", align = "lrc", booktab = TRUE) ``` ```` ] .pull-right[ Table: caption |col1 | col2| col3 | |:----|----:|:----:| |21.0 | 6| 160 | |21.0 | 6| 160 | |22.8 | 4| 108 | |21.4 | 6| 258 | |18.7 | 8| 360 | ] --- # Snippet will **save** you **Snippet** save your time, almost 1 year during your whole life, no joke. .pull-left[ ### What the hack is snippet * **Text macros** that are used for quickly inserting common snippets of code * `Tools` > `Global option` > `Code` > `Snippet` * Some people know there are *code snippet* in Rstudio ] .pull-right[ <div class="figure" style="text-align: center"> <img src="./snippet.jpg" alt="Available snippets languages" width="80%" /> <p class="caption">Available snippets languages</p> </div> ] --- class: center, middle # Live demo --- class: center, middle # GPS data visualization --- # I Know How You Drive * `R` package, `ikhyd`, is for driving behavior analysis, but it has a **GPS visualization** function `track_vis()`. ```r # devtools::install_github("issactoast/ikhyd") library(ikhyd) ``` ``` ## To cite this package use citation('ikhyd') ## ## Text citation: ## ## Issac Lee (2020). ikhyd : I Know How You Drive - Collection of R functions to make telematics analysis easier. ## R package version 0.1.0. URL https://github.com/issactoast/ikhyd ``` * Need to register Google API ```r google_api <- keyring::key_get("GOOGLEMAP_API") ``` --- ## How to access to data set `ikhyd` package contains seven telematics files; One of them is sample trip. .pull-left[ 1. Sample route telematics: `sample_trip.csv` 1. Test route with OBD information: with and w/o OBD 1. 4 Driving telematics files ] .pull-right[ * File path in your local computer ```r system.file("extdata", "sample_trip.csv", package = "ikhyd") ``` ``` ## [1] "C:/R/R-4.0.3/library/ikhyd/extdata/sample_trip.csv" ``` ] --- # Load trip data When you have a path of telematics data, you can load the data using `get_trip()` with `data_option` arguments as follows: ```r # telematics file path sample_trip_path <- system.file("extdata", "sample_trip.csv", package = "ikhyd") # load data sample_trip <- load_telematic_data(sample_trip_path, all_in_one = TRUE) summary(sample_trip) ``` ``` ## Length Class Mode ## gps_data 6 data.frame list ## acc_data 4 data.frame list ## lacc_data 4 data.frame list ## gyro_data 4 data.frame list ## angle_data 4 data.frame list ## speed_data 3 data.frame list ``` --- # Visualization GPS The snippet of `gps_data` for the sample trip looks as follows: ```r gps_data <- sample_trip$gps_data head(gps_data) ``` ``` ## time x y z accuracy_horiz accuracy_vert ## 1 0.000 -91.53799 41.66253 197.2922 5 4 ## 2 0.038 -91.53799 41.66253 197.2922 5 4 ## 3 0.079 -91.53799 41.66253 197.2821 5 4 ## 4 0.120 -91.53799 41.66253 197.2821 5 4 ## 5 0.153 -91.53799 41.66253 197.2821 5 4 ## 6 0.194 -91.53799 41.66253 197.2821 5 4 ``` --- # Google map loading ```r library(ggmap) register_google(key = google_api) center_vec <- colMeans(gps_data[,2:3]) map_obj <- get_googlemap(center = as.numeric(center_vec), maptype = "hybrid", zoom = 17) ggmap(map_obj) ``` <img src="presentation_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- # Track was around IMU and Pappajohn Business Building ```r p <- ggmap(map_obj) + geom_path(data = gps_data, aes(x = x, y = y), size = 0.7, color = "yellow") + geom_point(data = gps_data, aes(x, y), col = "red", size = 3) p ``` <img src="presentation_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> --- # Let's take a look at the speed! ```r plot_speed(sample_trip$speed_data, tripname = "the sample trip") ``` <div class="figure" style="text-align: center"> <img src="presentation_files/figure-html/gpsspeed-1.png" alt="The result of `plot_speed()` code." /> <p class="caption">The result of `plot_speed()` code.</p> </div> --- # Can we add this to the map? ```r library(gganimate) p <- p + transition_reveal(time) + ease_aes('linear') p ``` <img src="presentation_files/figure-html/unnamed-chunk-10-1.gif" style="display: block; margin: auto;" /> --- class: center, middle # Thanks!