Skip to content

Instantly share code, notes, and snippets.

@marcionicolau
Last active December 10, 2015 12:28
Show Gist options
  • Save marcionicolau/4434061 to your computer and use it in GitHub Desktop.
Save marcionicolau/4434061 to your computer and use it in GitHub Desktop.
.Rproj.user
.Rhistory
.RData
*.Rproj
long-*
y x1 x2 x3 x4 x5 group
-6.139908396 -0.550032705 0.993611412 -0.131581026 0.203644534 -1.563591113 a
-1.537896952 -0.352910358 0.755836532 0.156990812 -0.285947146 -0.605456618 a
-9.867806781 -2.246062704 -0.164825664 -1.630023491 -1.267427975 0.332656721 a
-5.366534149 -1.90079101 0.453673142 -0.428995044 0.907142746 -1.197633431 a
-1.328486327 -1.379938644 0.693039648 -0.542828302 -1.99084673 1.270872676 a
13.27492317 0.629846228 -0.200375562 0.778708945 0.898276555 1.402740243 a
3.120933404 0.695263835 -0.071938747 -0.634645637 1.726859202 -0.164787851 a
-7.274417029 -2.470976374 0.759031882 -0.151586267 0.534159255 -1.57424832 a
8.92561924 0.058592499 0.286524313 -0.1457834 0.525562368 1.169929439 a
-7.763898364 0.588319492 0.1745487 0.403809317 -1.17342337 -0.968255235 a
3.358455154 -0.183659515 -1.136825837 1.303277716 0.159695072 0.565131776 a
-0.628075264 -1.814003128 -0.280803159 -1.000525258 -0.021591865 0.94780226 a
-10.63620741 -0.8248063 -1.394313706 0.756084982 -2.741811688 0.434879291 a
-4.063927475 1.220309459 0.94357521 -0.625912326 -1.570261068 -0.145874784 a
-10.19503115 -0.556932775 0.184675097 -0.693868251 -0.263124544 -1.603356602 a
14.73234326 -1.17253586 -0.589482911 0.380908544 1.236029313 2.204803103 a
-3.791769892 -1.550162883 -0.24128911 -1.372779841 0.805564465 -0.266331255 a
-3.802577613 1.888193672 -0.501839409 -1.047740838 -0.879007421 0.509710955 a
-11.96881224 -0.239300005 0.499618337 0.416272252 -1.689930512 -1.369412536 a
5.306325939 1.367993583 1.906662725 0.465897007 -0.438300214 -0.187727823 a
2.823474909 -1.036249943 -0.739763716 1.353273836 -0.378639669 0.315608502 b
-9.868850007 -1.696959415 0.37549746 -0.377483871 -0.987513976 -0.928038613 b
-10.8008745 -0.783203238 -0.387076512 -1.603618649 -1.022489362 -0.035794639 b
-1.363890081 1.33606701 0.789539374 0.315549529 -2.183340196 0.719246773 b
-0.627163191 0.112590888 -0.243247197 -0.922847585 0.320436396 0.583634841 b
-5.099836498 -0.602106826 -0.192106287 1.044032845 0.133002864 -1.665833416 b
0.833954733 -1.273769252 0.078508407 -1.196774257 0.33810222 0.571724481 b
-2.055378051 0.068363939 -0.166155066 -0.485299586 -1.705305711 1.315830618 b
-11.28851742 1.204520341 -0.496720115 -1.121656952 -1.130200418 -1.149452048 b
20.07949072 1.131021937 2.22953606 0.771416625 1.146537529 1.667171297 b
15.01389305 -0.308935118 1.294180615 1.908890334 -0.258035763 1.653856205 b
1.301392778 -1.674581486 -0.898181877 0.56548884 0.068733717 0.49824458 b
5.945500965 0.588185521 -0.852445694 0.958573651 -0.969394842 1.633865041 b
0.032582069 0.360223275 0.665551576 -0.115898555 -1.259569793 0.5933086 b
1.416281363 0.594063558 -1.018008253 0.523113555 -0.816096788 0.521959071 b
4.956004616 1.042484948 -0.181520709 -0.135136946 -0.193948291 0.978850393 b
-2.350145155 -1.080710581 -0.697594741 1.373407025 -0.480772687 -0.522436617 b
-4.548050476 2.12208699 -1.384240934 -1.302889172 0.790109135 -0.480878296 b
-7.730458452 -0.799350606 -0.219665252 0.418171293 -0.360532887 -1.111536293 b
17.73088849 0.602969161 -0.797339221 2.140729358 1.803081025 1.033939145 b
7.301434229 -0.240388315 1.552314269 -0.33345869 -0.014348357 0.997048918 b
-9.29490976 -0.508845027 -0.937299964 1.02754567 -1.876253586 -0.86049338 b
-3.617061255 1.120643939 1.634355749 0.391116867 0.023570367 -1.907342958 b
-0.921171509 -1.027508454 0.494764935 -0.560627689 0.956184321 -0.411767983 b
4.601148624 1.006554947 1.121070343 0.081132517 -1.019206686 1.166002481 b
-0.665868488 -1.019267017 0.760125705 -0.024346725 0.707472677 -0.719319115 b
5.009150664 -0.721268475 0.456587518 1.8421873 -0.174041471 0.069522972 b
-0.215965522 -0.637901245 -0.548877752 2.067402349 -1.07746857 -0.104808224 b
-2.75545953 0.454997022 -1.018171585 -0.526623847 -0.460272653 0.207627574 b
0.083976773 0.30033072 -1.022978756 -0.600318686 -0.404935493 1.148569364 b
library(shiny)
# shiny::runApp('~/rtut/damshiny')
library(car)
library(tools)
library(foreign)
library(ggplot2)
library(xlsx)
# avoid breaks in R-output print, don't show error messages in Rstudio
options(width = 200, show.error.messages = FALSE, warn = -1, shiny.maxRequestSize = -1)
# options(width = 200)
loaddata <- function(state) {
inFile <- state$upload
if (is.null(inFile))
return(NULL)
filename <- inFile$name
ext <- file_ext(filename)
file <- newdata <- sub(paste(".",ext,sep = ""),"",filename)
ext <- tolower(ext)
if(ext == 'rda' || ext == 'rdata') {
newdata <- load(inFile$datapath, envir = .GlobalEnv)
}
# by putting this here we get the name of the object inside the R data file
data_sets <<- unique(c(newdata,data_sets))
if(ext == 'sav') {
assign(file, read.spss(inFile$datapath), envir = .GlobalEnv)
} else if(ext == 'dta') {
assign(file, read.dta(inFile$datapath), envir = .GlobalEnv)
} else if(ext == 'csv') {
assign(file, read.csv(inFile$datapath, header = TRUE), envir = .GlobalEnv)
} else if(ext == 'xls' || ext == 'xlsx') {
assign(file, read.xlsx(inFile$datapath, 1), envir = .GlobalEnv)
}
}
# needed since dataView is one of the tools
summary.dataView <- plot.dataView <- extra.dataView <- function(state) {
NULL
}
# needed since vizualize is one of the tools
summary.vizualize <- function(state) {
if(is.null(state))
return(cat("Please select a Y-variable\n"))
return(cat("Plots are shown in the plot-tab\n"))
}
plot.vizualize <- function(state) {
if(is.null(state))
return(NULL)
dat <- get(state$dataset)
if(is.null(state$var2)) {
print(ggplot(dat, aes_string(x=state$var1)) + geom_histogram())
} else {
print(ggplot(dat, aes_string(x=state$var1, y=state$var2)) + geom_point() + geom_smooth(method = "loess", size = 1.5))
}
}
extra.vizualize <- function(state) {
NULL
}
summary.dataView <- extra.dataView <- plot.dataView <- function(state) {
NULL
}
main.regression <- function(state) {
formula <- paste(state$var1, "~", paste(state$var2, collapse = " + "))
lm(formula, data = get(state$dataset))
}
# summary.regression <- function(state) {
summary.regression <- function(state) {
if(is.null(state))
return(cat("Please select one or more independent variables\n"))
summary(state)
}
plot.regression <- function(state) {
if(is.null(state))
return(NULL)
par(mfrow = c(2,2))
plot(state, ask = FALSE)
}
extra.regression <- function(state) {
if(is.null(state))
return(NULL)
if(length(state$coefficients) > 2) {
cat("Variance Inflation Factors\n")
VIF <- sort(vif(state), decreasing = TRUE)
data.frame(VIF)
} else {
cat("Insufficient number of independent variables selected to calculate VIF scores\n")
}
}
summary.compareMeans <- function(state) {
if(is.null(state$var2) || is.null(state$var1))
return(cat("Please select one or more variables\n"))
formula <- as.formula(paste(state$var2, "~", paste(state$var1, collapse=" + ")))
summary(aov(formula, data = get(state$dataset)))
}
plot.compareMeans <- function(state) {
if(is.null(state$var2))
return(NULL)
dat <- get(state$dataset)
print(qplot(factor(dat[,state$var1]), dat[,state$var2], data = dat, xlab = state$var1, ylab = state$var2, geom = c("boxplot", "jitter")))
# print(ggplot(dat, aes_string(x=state$var1, y=state$var2)) + geom_boxplot()) # x must be specified as a factor --> doesn't work with aes_string
}
extra.compareMeans <- function(state) {
NULL
}
# initial list of files to play with
data_sets <- c("mtcars", "morley", "rock")
# Labels for variable selectors
labels1 <- c("X-variable", "Dependent variable","Dependent variable")
labels2 <- c("Y-variable", "Independent variables","Variables")
labtools <- c("vizualize", "regression", "compareMeans")
names(labels1) <- names(labels2) <- labtools
# Define server logic
shinyServer(function(input, output) {
varnames <- reactive(function() {
dat <- get(input$dataset)
colnames <- names(dat)
names(colnames) <- paste(colnames, " (", sapply(dat,class), ")", sep = "")
return(colnames)
})
output$dataloaded <- reactiveUI(function() {
# adding 'input$loaded' means the function gets called when input$upload changes
# by putting it in this function the list of data also gets updated
input$upload
loaddata(as.list(input))
# Drop-down selection of data set
selectInput(inputId = "dataset", label = "Data sets", choices = data_sets, selected = data_sets[1], multiple = FALSE)
})
output$rowsToShow <- reactiveUI(function() {
# number of observations to show in data view
nrRow <- dim(get(input$dataset))[1]
sliderInput("nrRows", "# of rows to show:", min = 1, max = nrRow, value = min(15,nrRow), step = 1)
})
# variable selection
output$var1 <- reactiveUI(function() {
selectInput(inputId = "var1", label = labels1[input$tool], choices = varnames(), selected = NULL, multiple = TRUE)
})
# variable selection
output$var2 <- reactiveUI(function() {
selectInput(inputId = "var2", label = labels2[input$tool], choices = varnames()[-which(varnames() == input$var1)], selected = NULL, multiple = TRUE)
})
output$choose_columns <- reactiveUI(function() {
# Get the data set with the appropriate name
# Create a group of checkboxes and select them all by default
checkboxGroupInput("columns", "Choose columns", choices = as.list(varnames()), selected = names(varnames()))
# selectInput("columns", "Choose columns", choices = colnames, selected = colnames, multiple = TRUE)
})
output$data <- reactiveTable(function() {
dat <- get(input$dataset)[, input$columns, drop = FALSE]
head(dat, input$nrRows)
})
# Analysis reactives
vizualize <- reactive(function() {
# calling a reactive several times is more efficient than call a regular function several time
# if(is.null(input$var2))
# return(NULL)
as.list(input)
})
regression <- reactive(function() {
# calling a reactive several times is more efficient than call a regular function several time
if(is.null(input$var2))
return(NULL)
main.regression(as.list(input))
})
compareMeans <- reactive(function() {
# calling a reactive several times is more efficient than call a regular function several time
if(is.null(input$var2))
return(NULL)
as.list(input)
})
# Generate output for the summary tab
output$summary <- reactivePrint(function() {
f <- get(paste("summary",input$tool,sep = '.'))
f(get(input$tool)())
})
output$plots <- reactivePlot(function() {
f <- get(paste("plot",input$tool,sep = '.'))
f(get(input$tool)())
}, width=600, height=600)
# Generate output for the correlation tab
output$extra <- reactivePrint(function() {
print(as.list(input))
f <- get(paste("extra",input$tool,sep = '.'))
f(get(input$tool)())
})
})
<script type="text/javascript">
var wasBusy = false;
var elapsedTimer = null;
var startTime = null;
function updateBusy() {
var isBusy = $('html').hasClass('shiny-busy');
if (isBusy && !wasBusy) {
startTime = new Date().getTime();
elapsedTimer = setInterval(function() {
var millisElapsed = new Date().getTime() - startTime;
$('.progress').text(Math.round(millisElapsed/1000) + ' seconds have elapsed');
}, 1000);
}
else if (!isBusy && wasBusy) {
clearInterval(elapsedTimer);
}
wasBusy = isBusy;
}
</script>
library(shiny)
toolChoices <- list("Data view" = "dataView",
"Visualize" = "vizualize",
"Regression" = "regression",
"Compare means" = "compareMeans")
# toolChoices <- list("Visualize" = "vizualize", "Data view" = "dataView", "Regression" = "regression", "Compare means" = "compareMeans")
# Define UI for Data Analysis Menu using Shiny
shinyUI(
pageWithSidebar(
# Application title
headerPanel("Data Analysis Menu in Shiny"),
sidebarPanel(
wellPanel(
selectInput(inputId = "tool", label = "Tool:", choices = toolChoices, selected = 'dataView'),
uiOutput("dataloaded")
),
conditionalPanel(condition = "input.tool == 'dataView'",
wellPanel(
list(
fileInput("upload", "Load data (Rdata, CSV, Spss, Stata or Microsoft Excel format)"),
conditionalPanel("updateBusy() || $('html').hasClass('shiny-busy')",
id='progressIndicator',
"HI I'M IN PROGRESS",
div(class='progress',includeHTML("timer.js"))
)
),
uiOutput("rowsToShow"),
uiOutput("choose_columns")
)
),
conditionalPanel(condition = "input.tool != 'dataView'",
wellPanel(uiOutput("var1")),
wellPanel(uiOutput("var2"))
)
),
mainPanel(
conditionalPanel(condition = "input.tool == 'dataView'", tableOutput("data")),
conditionalPanel(condition = "input.tool != 'dataView'",
tabsetPanel(
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Plots", plotOutput("plots", height = 1200)),
tabPanel("Extra", verbatimTextOutput("extra"))
)
),
tags$head(tags$style(type="text/css",
'#progressIndicator {',
' position: fixed; top: 8px; right: 8px; width: 200px; height: 50px;',
' padding: 8px; border: 1px solid #CCC; border-radius: 8px;',
'}'
))
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment