R Studio and Shiny

R Studio has released a web application that is run (nearly) entirely through R (R Studio). It’s called Shiny and it’s great! It easily lets you turn your R scripts into a webpage. This is great for teaching purposes, showing off some code, and publishing to the web.

R Studio has given its users everything they need to make a web app using templates they have provided.  Everything fits into one “.R” file for easy editing and publishing.

You can find the Shiny page here: http://shiny.rstudio.com/

Here’s a link to my Shiny app. This has 4 statistical distributions (normal, lognormal, weibull, exponential) and let’s the user interact with the variables. The box plot and histogram of the data respond to the user controlled inputs.

Check it out here: My Shiny App
(Make sure to give it about 30 seconds to fully load for the first time.)

screenshot

Advertisements

Statistical Distributions

Statistical distributions are the meat and potatoes of R.  Generating random numbers from any distribution is easy in R.  Below I have listed the code for several popular statistical distributions.  The code is nearly the same for each distribution.  The “r” designates random, which is the first letter of the call for the distribution.  “unif” calls a uniform distribution, “norm” calls a normal distribution, “binom” calls a binomial distribution, etc.

To get random data from the normal distribution (rnorm), several other parameters are needed. n is the number of random variables to create, mean is the mean of the data distribution, and sd is the standard deviation of the data distribution.  I am requesting 10 random variables from the normal distribution with a mean of 0 and a standard deviation of 1.

#Uniform Distribution
runif(n=10, min=0, max=1)

#Normal Distribution
rnorm(n=10, mean=0, sd=1)

#Binomial Distribution
rbinom(n=10, size=5, prob=0.2)

#The log-normal Distribution
rlnorm(n=10, meanlog=0, sdlog=1)

#Weibull Distribution
rweibull(n=10, shape=1, scale = 1)

#Exponential Distribution
rexp(n=10, rate = 1)

#Poisson Distribution
rpois(n=10, lambda=1)

#Gamma Distribution
rgamma(n=10, shape=1, rate = 1)

#Chisquare Distribution
rchisq(n=10, df=3, ncp=1)
#where df is degrees of freedom, and ncp is non-centrality parameter