Question: I ve written function plot _ gun _ data ( intentval , covar,policevar ) using gun death data found on github.com.fivethirtyeight / guns - data

Ive written function plot_gun_data(intentval,covar,policevar) using gun death data found on
github.com.fivethirtyeight/guns-data in file `full_data.csv`. It makes barplots of number of gun deaths of
intent intentval separated by variable covar. covar can take values sex,age,race,place, and
education. For age, its broken out by decade of life (0-9,10-19,etc). The function filters out results
where police column does not equal policevar, only shows incidents where police were involved if
policevar=1, incidents where police were not involved if policevar=0, and all incidents if
policevar=c(0,1). It works regardless of capitalization of intentval and covar and gives informative error
messages. Intentval specifies the type of gun death to plot (homicide, suicide, accidental,
undetermined.) Covar is the variable to plot gun deaths against (sex, age, race, place,
education). Policeval is 1 if police involved in shooting and 0 if not. Output is a barplot showing
number of gun deaths for different values of covar.
Turn the function into an R Shiny app which looks like the example vrakj2.shinyapps.io/gun_data_app/.
I need code for 2 separate files: one with app (UI, server) and one with function (based on code above). I
already have a separate R file to deploy app so that it can be published on shinyapps.io.
```{r}
#function converting age to decade of life
decadefunc<-function(age)
{ if(age<10) return("0-9")
if((age>=10)&(age<20)) return ("10-19")
if((age>=20)&(age<30)) return ("20-29")
if((age>=30)&(age<40)) return ("30-39")
if((age>=40)&(age<50)) return ("40-49")
if((age>=50)&(age<60)) return ("50-59")
if((age>=60)&(age<70)) return ("60-69")
if((age>=70)&(age<80)) return ("70-79")
if((age>=80)&(age<90)) return ("80-89")
if(age>=90) return ("90+")
}
plot_gun_deaths<-function(intentval,covar,policeval=c(0,1))
{
require(tidyverse)
require(gridExtra)
intentval=str_to_title(intentval) #converts case of intentval to title case (capitalized first
letter and the rest lower case).
gundata<-read_csv("full_data.csv",na="NA")
if(!intentval%in%unique(gundata$intent)) return("error: invalid value of intent")
gundata_f=filter(gundata,intent==intentval,police%in%policeval) #filter down to rows
with desired intent and police involvement
plotitle=switch(intentval,Homicide=paste("Number of US gun homicide victims from
2012-2014 by",covar),Suicide=paste("Number of US gun suicide victims from 2012-
2014 by",covar),Accidental=paste("Number of US accidental gun death victims from
2012-2014 by",covar),Undetermined=paste("Number of US gun death victims with
undetermined intent from 2012-2014 by",covar))
#Makeplots depending on covar value
if(str_to_lower(covar)=="sex")
{ gundata_f=filter(gundata_f,!is.na(sex))
ggplot(data=gundata_f)+
geom_bar(aes(sex))+
coord_flip()+
xlab("Sex")+
ggtitle(plotitle)
} else if (str_to_lower(covar)=="race")
{ gundata_f=filter(gundata_f,!is.na(race))
ggplot(data=gundata_f)+
geom_bar(aes(race))+
coord_flip()+
xlab("Race")+
ggtitle(plotitle)
} else if (str_to_lower(covar)=="age")
{ gundata_f=filter(gundata_f,!is.na(age))
gundata_f=mutate(gundata_f,decade=sapply(gundata_f$age,decadefunc))
ggplot(data=gundata_f)+
geom_bar(aes(decade))+
coord_flip()+
xlab("Decade of Life")+
ggtitle(plotitle)
} else if (str_to_lower(covar)=="place")
{ gundata_f=filter(gundata_f,!is.na(place))
ggplot(data=gundata_f)+
geom_bar(aes(place))+
coord_flip()+
xlab("location of death")+
ggtitle(plotitle)
} else if (str_to_lower(covar)=="education")
{ gundata_f=filter(gundata_f,!is.na(education))
ggplot(data=gundata_f)+
geom_bar(aes(education))+
coord_flip()+
xlab("education level of victim")+
ggtitle(plotitle)
} else(return("error: invalid covariate"))
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!