```{r,render=F,include=FALSE,echo=FALSE} knitr::opts_chunk$set(warning=F,message=F) ``` ## Setup - Go to http://rstudio.cloud - Sign up (you can use a gmail or GitHub account, if you have one) - Click the down arrow next to "New Project" - Click "New Project from Git Repo" - In this type: https://github.com/drbjselby/teaching-expo-2019 - Click "Ok" ## Setup - Click the "math-expo-rstudio.Rmd" file in the bottom right - Click install for the packages needed - Wait while the packages install... - Click "Run Document" # Introduction ## About Me - Dr. Bekah Selby - Assistant Professor of Economics at Emporia State University - Data Nerd by trade (applied econometrician) - Teach classes ranging from basic statistics to graduate level applied econometrics ## My Problems - Data can be daunting - Students feel disenfranchised in mathematics courses - AKA they dread them - How to make statistics "come to life" - How to emphasize transparency - How to reduce my own workload ## One Solution - Use technology that combines interactivity with transparency - Rstudio has a feature called "R-Notebooks" - Scripting + Word Processing - Includes code with visuals and other output - Emphasizes process and description - Great place for students to do homework if R is incorporated as part of the curriculum - Also good for the teacher: - Create complementary slides and handouts very easily - Homework is output to a .html file which renders on LMS - Teachers can download .Rmd source file from submissions ## Today - Introduce some features of notebooks that I frequently use - Show you the basic structure of R-Notebooks so you can get started right away - Discuss creation of interactive content and how it can be incorporated in LMS # Introduction to R-Studio's R-Notebook ## The YAML Header - The first thing you see in a notebook is a YAML header. - title: "An Exceptional Title" - author: "Dr. Such and Such" - output: html_notebook - date: "October 4, 2019" - This renders in the output as a title. - You can choose lots of formats for the output, here we choose `html_notebook` to use the R-notebook capabilities ## Purpose of a Notebook - The R-notebook is a way to include all components of an analysis: - Code - Output - Discussion - It also has the capability of using interactives because it is rendered in html - In classroom assignments, this creates an emphasis on transparency of research and analysis (nothing is done "behind the scenes") ## Markdown: Uses markdown syntax to create formatted headers (see above), paragraphs, bulleted lists (this is one!), *font* **emphasis**, [hyperlinks](http://rstudio.com), block quotes, images, and more Renders math-equations using LaTeX. Example: ```{r,echo=FALSE} print("The equation $$Y_t = X_t + \varepsilon_t $$ renders to") ``` $$Y_t = X_t + \varepsilon_t $$ - This is commonly used to write up discussion about the analysis! ## R-Chunks R-chunks are pieces of code that are included in the place where the output is wanted. - Included Chunk ```{r} x<-c("Hello","World") x ``` - Excluded Chunk (you won't see the code on the screen) ```{r,echo=FALSE} x<-c("Hello","World") x ``` - Excluded Output (you won't see the code or the output on the screen!) ```{r,include=FALSE} x<-c("Hello","World") x ``` ## Inline R Code You can also write up code using inline syntax If we want to calculate the average of `cars$speed`, we might write ```{r} mean(cars$speed) ``` or write "The average is `r mean(cars$speed)`" # Creating an Interactive Notebook ## Creating Basic Plots and Tables in R First things first, using R to create visuals. We are going to use data already preinstalled in R called `cars` 1. Create a table containing the first 10 observations from the data set `cars`: ## ```{r} head(cars,10) ``` ## 2. Create a scatter plot of the speed and distance in the data set `cars` ## ```{r} plot(cars) ``` ## 3. Create a summary statistics table including the mean, standard deviation, and min and max for the variables in the data set `cars`. ## ```{r} #install.packages("psych") library(psych) describe(cars,skew = F,trim = 0,IQR = T) ``` ## 4. Create a histogram for the speed of cars in the data set `cars`. ## ```{r} hist(cars$speed) ``` ## Tutorials and Quizzes Suppose we want to create an interactive tutorial where students can practice their R analysis skills! ```{r} #install.packages("learnr") library(learnr) ``` ```{r mean,exercise=T} # Calculate the average speed of cars. Save as an object called `mean` ``` ```{r mean-solution} mean<-mean(cars$speed) ``` ## Interactives using Shiny - Using `runtime: shiny` in the YAML, you can create interactive apps - This presentation is using the `learnr` package which requires the `runtime: shiny_prerendered` option - My workaround, for this presentation: a separate file! ## Interactive Graphs Using Plotly ```{r,echo=F,warning=FALSE,message=FALSE} library(plotly) m <- list( l = 50, r = 50, b = 100, t = 100, pad = 4 ) ``` ```{r,warning=FALSE,message=FALSE} plot_ly(cars, x = ~speed, y = ~dist) ``` ## ```{r} plot_ly(x = cars$speed, type = "histogram") ``` ## ```{r} plot_ly(x = cars$speed, type = "box") ``` ## 2D Histograms ```{r} plot<-plot_ly(x=cars$speed,y=cars$dist) (subplot( plot %>% add_markers(alpha = 0.2), plot %>% add_histogram2d() )) ```