Example of an RTutor interactive Problemset Author: Sebastian Kranz Date: 12.06.2014
Adapt the working directory below and press Ctrl-Alt-R (run all chunks). This creates the problem set files and the sample solution from this solution file.
library(restorepoint) # facilitates error detection set.restore.point.options(display.restore.point=TRUE) library(RTutor) #library(restorepoint) setwd("D:/libraries/RTutor/examples") ps.name = "Example"; sol.file = paste0(ps.name,"_sol.Rmd") libs = c("ggplot2") # character vector of all packages you load in the problem set #name.rmd.chunks(sol.file) # set auto chunk names in this file create.ps(sol.file=sol.file, ps.name=ps.name, user.name=NULL,libs=libs, stop.when.finished=FALSE) show.shiny.ps(ps.name, load.sav=FALSE, sample.solution=TRUE, is.solved=FALSE, catch.errors=TRUE, launch.browser=TRUE) stop.without.error()
a) First part
Some Math:
$$x = \beta^2$$
$$y = \alpha^2$$
We often want to compute some summary statistic of a vector. For example:
#< show x = 10:20 # Computing the sum of x sum(x) # Generating a data frame df = data.frame(a=c(1,1,2,2,2,3,3,4), b = runif(8), c="Hi") #> #< show_notest df #>
Now compute the mean of x.
mean(x) #< hint cat("There already exist R functions for many things. To find them use Google, e.g. search for 'R compute mean'.") #>
Here are examples for useful R functions
max(c(1,5,2)) # returns maximum min(c(1,5,2)) # returns minimum sum(c(1,5,2,NA), na.rm=TRUE) # returns sum of all numbers, ignore NA cumsum(c(1,5,2)) # returns cummulated sum diff(c(1,5,2)) # returns the vector of differences
Well, in some occasions one can just guess the name of an R function. The function to compute the mean of a vector, or matrix is called 'mean'. Usually, it is much quicker to goggle than to guess function names, however.
#< settings import.var = c("x") #>
a) Let y be a vector that contains the squared elements of x. Show y
y = x^2
y
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.