I previously created a linear regression model shiny app, attached to a map for my class. I am now working on creating one with a different data set (variables are Sex, Race, Occupation, Income), and I am having some trouble modifying my own code.
What I want is to have users choose a variable - say, race, sex, etc. and get an income/see how income changes based on what they select. But since the below code doesn't work, I am wondering if a regression model is even the appropriate way to go about this. To be clear, I don't get an error - it just comes up blank, the screen.
Some assistance would be great - even if it's just telling me to use a different type of script. So, to be clear: I am looking for some guidance on whether or not a regression model is appropriate for the question I am asking, and two, if I am on the right track in modifying the code I already created.
mydata<-read.csv("usa_00004.csv")
server<-function(input, output) {
#creating a reactive regression forumula that uses inputs from the check list
#as independent variables to predict the variable Income - *I need this due to checklist formula in the UI below
regFormula<- reactive({
as.formula(paste("Income"," ~ ",paste(input$iv1,collapse="+")))
})
# then, put that formula into the svyglm() from survey package which outputs
# a weighted regression
model <- reactive({
lm(regFormula(), data = mydata)
})
# Creating pretty labels for the stargazer table
# Here, we are using if statements to build a vector, covars, that is dependent on the inputs
#from the beck list. - *I should be able to use this as is, I think, *
covar.label <- reactive({
covars<-character()
if ('SEX' %in% input$iv1){
covars <- c(covars,"SEX")
}
if ('RACE' %in% input$iv1){
covars <- c(covars,"RACE")
}
if ('OCC' %in% input$iv1){
covars <- c(covars,"OCC")
}
if ('INCWAGE' %in% input$iv1){
covars <- c(covars,"INCWAGE")
}
return(covars)
})
#Create nice regression table output
#stargazer() comes from the stargazer package
output$regTab <- renderText({
covars <- covar.label()
stargazer(model(),type="html",dep.var.labels ="Income Prediction",covariate.labels = covars, omit.stat = c("f","ser","aic","adj.rsq"))
})
}
#-----------------------------------#
# UI #
# Navigation Bar II #
# #
#-----------------------------------#
# UI
ui <- fluidPage(theme = shinytheme("flatly"),
navbarPage(
"Income Prediction App",
#this is the title for our navigation bar
tabPanel(
"Welcome",
#tabPanel() creates our first tab
tags$head(tags$style(
"h2 {color: #04B4AE; }
h1 {color: #04B4AE}; }
"
)),
# this is setting the color palette for our tab headers 1 and 2
headerPanel("Welcome!"),
br(),
h2("How to Use This App"),
h4(tags$ul(
tags$li(
"To be edited"
),
#bullet point
tags$li(
"To be edited"
) #bullet point
)),
h4(
"Filler language"
),
h2("The Data"),
h4(
"This app uses data from...."
),
h2("Methodology"),
h4(
"This app uses logistic regression to predict income"
)
),
# First Navigation bar tab
# this information will appear accross the inner-tabs, called panels
tabPanel(
"Analyzing Income",
headerPanel("Income Prediction Model"),
# The sidebar contains the option for the end user to select
# multiple independent variables
sidebarLayout(
position = "right",
sidebarPanel(
h2("Build your model"),
br(),
checkboxGroupInput(
"iv1",
label = "Select a factor. You can change your selection at any time.",
list(
"SEX" = "SEX",
"RACE" = "RACE",
"OCC" = "OCC",
"INCWAGE" = "INCWAGE",
),
selected = "INCWAGE"
)
),
mainPanel(br(),
#create a 1st tab panel
tabsetPanel(
type = "tabs",
#first panel shows regression table
tabPanel(
"Regression Table",
h3("Table of Regression Coefficients"),
HTML('</br>'),
tableOutput("regTab"),
HTML('</br>'),
helpText(
"Explains what table does"
)
)
))
)
)))
shinyApp(ui = ui, server = server)