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 plotgundataintentvalcovar,policevar using gun death data found on
github.com.fivethirtyeightgunsdata in file fulldata.csv It makes barplots of number of gun deaths of
intent intentval separated by variable covar. covar can take values sexageraceplace and
education For age its broken out by decade of life etc The function filters out results
where police column does not equal policevar, only shows incidents where police were involved if
policevar incidents where police were not involved if policevar and all incidents if
policevarc 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 if police involved in shooting and 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 vrakjshinyapps.iogundataapp
I need code for 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
decadefuncfunctionage
ifage return
ifage&age return
ifage&age return
ifage&age return
ifage&age return
ifage&age return
ifage&age return
ifage&age return
ifage&age return
ifage return
plotgundeathsfunctionintentvalcovar,policevalc
requiretidyverse
requiregridExtra
intentvalstrtotitleintentval #converts case of intentval to title case capitalized first
letter and the rest lower case
gundatareadcsvfulldata.csvnaNA
ifintentvalinuniquegundata$intent returnerror: invalid value of intent"
gundataffiltergundataintentintentval,policeinpoliceval #filter down to rows
with desired intent and police involvement
plotitleswitchintentvalHomicidepasteNumber of US gun homicide victims from
bycovarSuicidepasteNumber of US gun suicide victims from
bycovarAccidentalpasteNumber of US accidental gun death victims from
bycovarUndeterminedpasteNumber of US gun death victims with
undetermined intent from bycovar
#Makeplots depending on covar value
ifstrtolowercovar"sex"
gundataffiltergundatafisnasex
ggplotdatagundataf
geombaraessex
coordflip
xlabSex
ggtitleplotitle
else if strtolowercovar"race"
gundataffiltergundatafisnarace
ggplotdatagundataf
geombaraesrace
coordflip
xlabRace
ggtitleplotitle
else if strtolowercovar"age"
gundataffiltergundatafisnaage
gundatafmutategundatafdecadesapplygundataf$age,decadefunc
ggplotdatagundataf
geombaraesdecade
coordflip
xlabDecade of Life"
ggtitleplotitle
else if strtolowercovar"place"
gundataffiltergundatafisnaplace
ggplotdatagundataf
geombaraesplace
coordflip
xlablocation of death"
ggtitleplotitle
else if strtolowercovar"education"
gundataffiltergundatafisnaeducation
ggplotdatagundataf
geombaraeseducation
coordflip
xlabeducation level of victim"
ggtitleplotitle
elsereturnerror: invalid covariate"
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
