library(knitr)
source("123.r")
shinyServer(function(input, output) {
datasetInput <- reactive( {
inFile = input$file
if (!is.null(inFile))
read.table(inFile$datapath, header = T)
else
read.table('data/mtcars.txt', header = T)
}
)
# dependent variable
output$dv = renderUI({
selectInput('dv', h5('Dependent Variable'), choices = colnames(datasetInput()))
})
# independent variable
output$iv = renderUI({
selectInput('iv', h5('Independent Variable1'), choices = c(colnames(datasetInput()),"none"))
})
output$iv2 = renderUI({
selectInput('iv2', h5('Independent Variable2'), choices = c(colnames(datasetInput()),"none"))
})
output$iv3 = renderUI({
selectInput('iv3', h5('Independent Variable3'), choices = c(colnames(datasetInput()),"none"))
})
# regression formula
regFormula <- reactive({
temp = c(input$iv, input$iv2,input$iv3)
temp = temp[temp!="none"]
as.formula(paste(input$dv, '~', paste(temp, collapse = "+")))
})
# bivariate model
model <- reactive({
lm_eqn(regFormula(), datasetInput())
})
# create graphics
# data view
output$view = renderTable({
head(datasetInput(), n = 20)
})
#
# scatter plot
output$scatter <- renderPlot({
temp = c(input$iv, input$iv2,input$iv3)
temp = temp[temp!="none"]
pairs(datasetInput()[ , c(input$dv,temp)])
})
# model
output$model <- renderPrint({
summary(model())
})
# residuals
output$res_plot <- renderPlot({
par(mfrow = c(2,2))
plot(model(),1:4)
})
output$tbr <- renderTable({
tb = c(summary(model())$r.sq, coef(model()), summary(model())$coefficients[,4])
df = data.frame(Items = c("R2", names(coef(model())), paste(names(coef(model())),"pvalue")) , Values = tb )
df
})
output$cor <- renderTable({
temp = c(input$iv, input$iv2,input$iv3)
temp = temp[temp!="none"]
cor(datasetInput()[ , c(input$dv,temp)])
})
makepdf <- function() {
pdf(file = "temp/report.pdf")
temp = c(input$iv, input$iv2,input$iv3)
temp = temp[temp!="none"]
pairs(datasetInput()[ , c(input$dv,temp)])
par(mfrow = c(2,2))
plot(model(),1:4)
dev.off()
}
output$downloadReport <- downloadHandler(
filename = 'report.pdf',
content = function(file) {
makepdf()
file.rename("temp/report.pdf", file)
}
)
}
)
===============
ui
# Define UI for application
shinyUI(fluidPage(
# Application title
titlePanel("Regression Model Shiny App"),
# Sidebar
sidebarLayout(
sidebarPanel(
helpText("Upload your txt file here, make sure you have headers with at least 2 variables!"),
fileInput("file", label = h4("")),
uiOutput('dv'),
uiOutput('iv'),
uiOutput('iv2'),
uiOutput('iv3'),
radioButtons('format', h5('Document PDF Report'), c('PDF'), inline = TRUE),
downloadButton('downloadReport')),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Data",
tableOutput("view")),
tabPanel("Scatter Plot",
plotOutput("scatter")),
tabPanel("correlations",
tableOutput("cor")),
tabPanel("Model R squared,Coefficients,pvalues",
tableOutput("tbr")),
tabPanel("Model Estimate Details",
verbatimTextOutput("model")),
tabPanel("residuals plot",
plotOutput("res_plot"))
)
))
))
source("123.r")
shinyServer(function(input, output) {
datasetInput <- reactive( {
inFile = input$file
if (!is.null(inFile))
read.table(inFile$datapath, header = T)
else
read.table('data/mtcars.txt', header = T)
}
)
# dependent variable
output$dv = renderUI({
selectInput('dv', h5('Dependent Variable'), choices = colnames(datasetInput()))
})
# independent variable
output$iv = renderUI({
selectInput('iv', h5('Independent Variable1'), choices = c(colnames(datasetInput()),"none"))
})
output$iv2 = renderUI({
selectInput('iv2', h5('Independent Variable2'), choices = c(colnames(datasetInput()),"none"))
})
output$iv3 = renderUI({
selectInput('iv3', h5('Independent Variable3'), choices = c(colnames(datasetInput()),"none"))
})
# regression formula
regFormula <- reactive({
temp = c(input$iv, input$iv2,input$iv3)
temp = temp[temp!="none"]
as.formula(paste(input$dv, '~', paste(temp, collapse = "+")))
})
# bivariate model
model <- reactive({
lm_eqn(regFormula(), datasetInput())
})
# create graphics
# data view
output$view = renderTable({
head(datasetInput(), n = 20)
})
#
# scatter plot
output$scatter <- renderPlot({
temp = c(input$iv, input$iv2,input$iv3)
temp = temp[temp!="none"]
pairs(datasetInput()[ , c(input$dv,temp)])
})
# model
output$model <- renderPrint({
summary(model())
})
# residuals
output$res_plot <- renderPlot({
par(mfrow = c(2,2))
plot(model(),1:4)
})
output$tbr <- renderTable({
tb = c(summary(model())$r.sq, coef(model()), summary(model())$coefficients[,4])
df = data.frame(Items = c("R2", names(coef(model())), paste(names(coef(model())),"pvalue")) , Values = tb )
df
})
output$cor <- renderTable({
temp = c(input$iv, input$iv2,input$iv3)
temp = temp[temp!="none"]
cor(datasetInput()[ , c(input$dv,temp)])
})
makepdf <- function() {
pdf(file = "temp/report.pdf")
temp = c(input$iv, input$iv2,input$iv3)
temp = temp[temp!="none"]
pairs(datasetInput()[ , c(input$dv,temp)])
par(mfrow = c(2,2))
plot(model(),1:4)
dev.off()
}
output$downloadReport <- downloadHandler(
filename = 'report.pdf',
content = function(file) {
makepdf()
file.rename("temp/report.pdf", file)
}
)
}
)
===============
ui
# Define UI for application
shinyUI(fluidPage(
# Application title
titlePanel("Regression Model Shiny App"),
# Sidebar
sidebarLayout(
sidebarPanel(
helpText("Upload your txt file here, make sure you have headers with at least 2 variables!"),
fileInput("file", label = h4("")),
uiOutput('dv'),
uiOutput('iv'),
uiOutput('iv2'),
uiOutput('iv3'),
radioButtons('format', h5('Document PDF Report'), c('PDF'), inline = TRUE),
downloadButton('downloadReport')),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Data",
tableOutput("view")),
tabPanel("Scatter Plot",
plotOutput("scatter")),
tabPanel("correlations",
tableOutput("cor")),
tabPanel("Model R squared,Coefficients,pvalues",
tableOutput("tbr")),
tabPanel("Model Estimate Details",
verbatimTextOutput("model")),
tabPanel("residuals plot",
plotOutput("res_plot"))
)
))
))