I have played a bit with shiny. The RStudio folks provides the possibility of deploying R “Shiny” applications on their server. The final result which will look a lot like this: https://<YourAccount>.shinyapps.io/<YourApp>. There are several plans for this type of deployment, including one that allows you to deploy up to 5 applications for free.
You can sign up using on www.shinyapps.io creating a new account or logging in using your github.com or gmail account. The most recent version of Shiny does no longer require two separate files as I was reporting in the last article on the subject. Now you can condense all the functionality in a single file: app.R. In synthesis, you need to build a app.R file that contains the following:
- The inclusion of the shiny library: library(shiny)
- A ui function: ui <-fluidPage(….
- This will define your page layout and your controls
- A server function: server <- function(input, output) {
- This will be your server logic
- A call to shinyApp(ui=ui, server=server )
- This is the glue that puts everything together
When building your application in RStudio, it recognizes it as a “Shiny” application and it lets run it in your browser, in a separate window or in the plot pane. It also shows a “Publish” button that allows you to upload the application to the www.shinyapps,io website. This you can do provided that you have set up an account.
To learn the basics, I have used the tutorials and the shiny app gallery that you can find at http://shiny.rstudio.com/gallery/. Almost all examples include a link to or a pane with the source code.
As far as the application, I have borrowed some code from this article on r-bloggers.com. This is in relation with a swirl() problem on two of the classes of the Statistical Inference course on Coursera.
The shiny app I propose can be found at this link: Shaded Normal Distribution. It does much less than the original shadenorm() function by Tony Cookson did, I tried to leave all the nice use cases that he proposed as comments in the code. This is a screenshot, You will find all the source code below it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
library(shiny) library(ggplot2) # DEFINE UI ui <-fluidPage( # Application title titlePanel("Shaded Normal Distribution"), # Sidebar with a slider inputs sidebarLayout( sidebarPanel( sliderInput("mu", "Mean:", min = -20, max = 20, value = 0), sliderInput("sig", "Sigma:", min = 1, max = 20, value = 1), checkboxInput("lefttail", label = "Hide Left Tail", value = FALSE), checkboxInput("righttail", label = "Hide Right Tail", value = FALSE), sliderInput("dens", "Density:", min = 1, max = 100, value = 40), sliderInput("below", "Below:", min = -3.0, max = 0.0, value = -1.96, step= 0.005), sliderInput("above", "Above:", min = 0, max = 3.0, value = 1.96, step= 0.005) ), # Show a plot of the generated distribution mainPanel( plotOutput("normPlot") ) ) ) # DEFINE SERVER LOGIC server <- function(input, output) { output$normPlot <- renderPlot({ shadenorm(mu=input$mu, sig=input$sig, justabove = input$lefttail, justbelow = input$righttail, dens=input$dens, below=input$below, above=input$above) }) } # From r-bloggers.com # # How to Shade Under a Normal Density in R # April 3, 2011 # By Tony Cookson # shadenorm = function(below=NULL, above=NULL, pcts = c(0.025,0.975), mu=0, sig=1, numpts = 500, color = "blue", dens = 40, justabove= FALSE, justbelow = FALSE, lines=FALSE,between=NULL,outside=NULL){ if(is.null(between)){ below = ifelse(is.null(below), qnorm(pcts[1],mu,sig), below) above = ifelse(is.null(above), qnorm(pcts[2],mu,sig), above) } if(is.null(outside)==FALSE){ below = min(outside) above = max(outside) } lowlim = mu - 4*sig uplim = mu + 4*sig x.grid = seq(lowlim,uplim, length= numpts) dens.all = dnorm(x.grid,mean=mu, sd = sig) if(lines==FALSE){ plot(x.grid, dens.all, type="l", xlab="X", ylab="Density") } if(lines==TRUE){ lines(x.grid,dens.all) } if(justabove==FALSE){ x.below = x.grid[x.grid<below] dens.below = dens.all[x.grid<below] polygon(c(x.below,rev(x.below)),c(rep(0,length(x.below)),rev(dens.below)),col=color,density=dens) } if(justbelow==FALSE){ x.above = x.grid[x.grid>above] dens.above = dens.all[x.grid>above] polygon(c(x.above,rev(x.above)),c(rep(0,length(x.above)),rev(dens.above)),col=color,density=dens) } if(is.null(between)==FALSE){ from = min(between) to = max(between) x.between = x.grid[x.grid>from&x.grid<to] dens.between = dens.all[x.grid>from&x.grid<to] polygon(c(x.between,rev(x.between)),c(rep(0,length(x.between)),rev(dens.between)),col=color,density=dens) } } <p style="text-align: left;"># THIS IS THE GLUE shinyApp(ui=ui, server=server )</p> # ## ---------------------- ## # # ## Shading under a Normal ## # # ## in R ## # # ## ---------------------- ## # # shadenorm() ## Defaults mu = 0, sig = 1, pcts = c(0.025, 0.975) # # ## Below, Above, Outside and Between # # shadenorm(below=-1.5) ## Sets lower endpoint at -1.5 # # shadenorm(below=-1.5,justbelow=TRUE) ## Just plots the lower tail # # shadenorm(above=1.5) ## Sets upper endpoint at 1.5 # # shadenorm(above=1.96, justabove=TRUE) # # shadenorm(above=1.5, justabove=TRUE) ## Similar to below # # shadenorm(below=-1.5,above=1.5) ## Plots "outside" of -1.5 and 1.5 # # shadenorm(outside = c(2.5, -2.5)) ## Alternative "outside" plotter # # shadenorm(outside = c(1, 2, -1, -0.2, 2.4)) ## works by taking min and max # # shadenorm(between = c(2.5, -2.5)) ## Plots between specified points # # shadenorm(between = c(-1.75, 0, 2, 0.5, -1)) ## Plots between specified points # # ## Can specify different means and standard deviations with mu and sig arguments # # shadenorm(mu=2, sig=10) # # shadenorm(mu=2, sig=10, between=c(-2, 14)) ## works with between and outside # # shadenorm(mu=2, sig=10, outside=c(-5, 14)) # # ## Can Apply different colors and densities... color and dens # # shadenorm(mu=2, sig=10, outside=c(-3, 12), col="blue") # # shadenorm(mu=2, sig=10, outside=c(-3, 12), col="blue",dens=15) ## Dens default is 40 # # shadenorm(mu=2, sig=10, outside=c(-3, 12), col="blue",dens=150) # # ## Can plot one and then another ontop of it using lines = TRUE # # shadenorm(mu=2, sig=10, outside=c(-3, 12), dens=15) # # shadenorm(mu=2, sig=15, between=c(-3, 12),lines=TRUE, col="blue",dens=15) # # ## Example: Plotting a Hypothesis Test for the mean # # ## Truth: mu.true = 8 # # ## Hypothesis: mu.ho = 6 # # ## Generate Data Under Truth # # mu.true = 5 ## Alternative Mean # # mu.ho = 6 # # sig = 8 # # N = 250 ## Sample Size # # # # std.err = sig/sqrt(N) # # crits = qnorm(c(0.025,0.975),mean=mu.ho, sd = std.err) # # shadenorm(outside = crits, mu = mu.ho, sig = std.err,dens=15) # # shadenorm(between = crits, mu = mu.true, sig = std.err, lines=TRUE, color="green",dens=15) <p style="text-align: left;"> |