Last active
February 26, 2016 05:31
-
-
Save kylebaron/d3148e98bf09ee048657 to your computer and use it in GitHub Desktop.
Shiny + mclapply + mvrnorm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(MASS) | |
library(shiny) | |
library(mrgsolve) | |
library(dplyr) | |
library(magrittr) | |
library(parallel) | |
RNGkind("L'Ecuyer-CMRG") | |
set.seed(101) | |
mc.reset.stream() | |
##' UI ########################################### | |
ui<- fluidPage( | |
titlePanel("Shiny MC"), | |
sidebarLayout( | |
sidebarPanel( | |
sliderInput("OM1", "OMEGA 1",0,4,1,0.25), | |
sliderInput("OM2", "OMEGA 2",0,4,1,.25), | |
sliderInput("n", "N rep", 1,100,10,1), | |
sliderInput("N", "N subj", 1, 100,10,1), | |
sliderInput("mccores", "mc.cores", 1,4,2,1) | |
), | |
mainPanel(tableOutput("table") | |
) | |
) | |
) | |
##' Simulate | |
##' if(fix) the seed will be set based on replicate number | |
sim <- function(i,N,om1,om2) { | |
mvrnorm(N,c(ETA1=0,ETA2=0),dmat(om1,om2)) %>% as.data.frame | |
} | |
##' Summarize simulations | |
smry <- function(x) { | |
x %>% summarise(nETA1 = n_distinct(ETA1), | |
vETA1 = var(ETA1), | |
sETA1 = sum(ETA1), | |
nETA2 = n_distinct(ETA2), | |
vETA2 = var(ETA2), | |
sETA2 = sum(ETA2), | |
n=n()) | |
} | |
##' SERVER ########################################### | |
server<-function(input, output) { | |
output$table <- renderTable({ | |
mclapply(1:input$n, | |
mc.cores=input$mccores, | |
sim,input$N,input$OM1,input$OM2) %>% bind_rows %>% smry | |
}) | |
} | |
##' Run the shiny app | |
shinyApp(ui = ui, server = server) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment