Я хочу видеть процент людей, в зависимости от их расы, в возрасте от 20 до 35 лет, сообщивших о своем образовательном статусе. На следующем этапе я делаю блестящее приложение. Однако я получил эту ошибку. ! [введите описание изображения здесь] [1] Пожалуйста, помогите мне, как связать этот код с блестящим приложением.
Мой вопрос заключается в том, как я могу изменить sliderInput с 20 до 35 лет, в каждом возрасте знает, как у многих людей есть средняя школа, высшее образование и степень бакалавра в зависимости от их расы.
Ниже вы можете увидеть кодирование возраста, образования и расы.
# rename education
nlsy97$educstat1997<-Recode(nlsy97$R1205700, recodes="0='None';1:2='Hischool';3='college';4='bachelor';5:7='mastermore' ;-5=NA;-3=NA;-4=NA", as.factor=T)
#rename ages
nlsy97$age1<-Recode(nlsy97$R1194100, recodes="12=12;13=13;14=14;15=15;16=16=17=17;18=18;19=19;-5=NA",as.factor=F)
# recode race
nlsy97$race<-Recode(nlsy97$R1482600 , recodes="1='black';2='hispanic' ;4='white';else=NA", as.factor=T)
table(nlsy97$race)
На следующем шаге я сделал переходы.
myvars1<-c( "R0000100","R0536300", "R0536402","R1489700","R1489800", "gender","race","age1","age2","age3","age4","age5","age6","age7","age8","age9","age10","age11","age12","age13","age14","age15","age16","age17","educstat1997","educstat1998","educstat1999","educstat2000","educstat2001","educstat2002","educstat2003","educstat2004","educstat2005","educstat2006","educstat2007","educstat2008","educstat2009","educstat2010","educstat2011","educstat2013","educstat2015")
which(myvars1 %in% names(nlsy97))
sub<-nlsy97[,myvars1]
sub<-subset(sub,is.na(sub$age1)==F&is.na(sub$age2)==F&is.na(sub$age3)==F&is.na(sub$age4)==F&is.na(sub$age5)==F&is.na(sub$age6)==F&is.na(sub$age7)==F&is.na(sub$age8)==F&is.na(sub$age9)==F&is.na(sub$age10)==F&is.na(sub$age11)==F&is.na(sub$age12)==F&is.na(sub$age13)==F&is.na(sub$age14)==F&is.na(sub$age15)==F&is.na(sub$age16)==F&is.na(sub$age17)==F)
head(sub, n=5)
x.vertical<-reshape(sub, idvar="R0000100", varying=list(age1=c("age1", "age2", "age3","age4","age5","age6","age7","age8","age9","age10","age11","age12","age13","age14","age15","age16"), age2=c("age2", "age3","age4","age5","age6","age7","age8","age9","age10","age11","age12","age13","age14","age15","age16","age17"),educstat1=c("educstat1997","educstat1998","educstat1999","educstat2000","educstat2001","educstat2002","educstat2003","educstat2004","educstat2005","educstat2006","educstat2007","educstat2008","educstat2009","educstat2010","educstat2011","educstat2013"),educstat2=c("educstat1998","educstat1999","educstat2000","educstat2001","educstat2002","educstat2003","educstat2004","educstat2005","educstat2006","educstat2007","educstat2008","educstat2009","educstat2010","educstat2011","educstat2013","educstat2015")), times=1:16, direction="long", v.names=c( "agestart", "ageend","educstat1","educstat2") )
x.vertical<-x.vertical[order(x.vertical$R0000100, x.vertical$time),]
Последний Шаг делает блестящую приборную панель.
# STEP 1: copy an example Shiny app into app.R (or ui.R and server.R)
library(shiny)
library(tidyverse)
library(gapminder)
# User Interface
ui <- basicPage(
# STEP 3: Create an input widget here (e.g. sliderInput)
sliderInput("age", "Select Age:", animate = TRUE, # STEP 4: add animate = TRUE here
min = 20, max = 35, value = 25,
step = 1,
sep="" # so thousands are not separated with a comma (without this defaults to 1,952 - 2,007)
), #note this comma here - different to our usual R code
tabPanel("Plot", plotOutput(outputId = "myplot"))
)
x.vertical2<-x.vertical[complete.cases(x.vertical[, c("race","educstat2")]),]
sums<-as.data.frame(xtabs(~educstat2+race, x.vertical2))
# Server
server <- function(input, output) {
output$myplot <- renderPlot({
# STEP 2: copy your plot code here
x.vertical2 %>%
filter(agestart==input$agestart)
mutate(educstat2 = fct_relevel(educstat2,
"None", "Hischool", "c",
"college")) %>%
filter(is.na(educstat2)==F)%>%
group_by(race, educstat2)%>%
summarise(n = n())%>%
mutate(freq= n /sum(n))%>%
ggplot(aes(x = factor(educstat2),y=freq, fill= race)) +
geom_bar( stat="identity",position = "dodge") +theme_bw()
})
}
shinyApp(ui, server)
dput(head(nlsy97, 10))
structure(list(R0000100 = 1:10, R0536300 = c(2L, 1L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 1L), R0536401 = c(9L, 7L, 9L, 2L, 10L, 1L,
4L, 6L, 10L, 3L), R0536402 = c(1981L, 1982L, 1983L, 1981L, 1982L,
1982L, 1983L, 1981L, 1982L, 1984L), R1194100 = c(15L, 14L, 13L,
15L, 15L, 15L, 14L, 16L, 15L, 14L), R1205700 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), R1235800 = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), R1482600 = c(4L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 4L, 4L), R2553500 = c(17L, 16L, 15L, 17L, 16L, 16L, 15L,
17L, 16L, 14L), R2564101 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), R3876300 = c(18L, 17L, 16L, 18L, 17L, 17L, 16L, 18L,
17L, 15L), R3885701 = c(2L, 0L, 0L, 2L, 0L, 0L, 0L, 2L, 0L, 0L
), R5453700 = c(19L, 18L, 17L, 19L, 18L, 19L, 17L, 19L, 18L,
16L), R5464901 = c(2L, 2L, 0L, 2L, 2L, 2L, 0L, 2L, 2L, 0L), R7216000 = c(20L,
19L, 18L, 20L, 19L, 20L, 18L, 20L, 19L, 17L), R7228601 = c(2L,
2L, 2L, 2L, 2L, 2L, 0L, 2L, 2L, 0L), S1531400 = c(21L, 20L, 19L,
21L, 20L, 20L, 19L, 21L, 20L, 18L), S1542501 = c(2L, 2L, 2L,
2L, 2L, 2L, 0L, 2L, 2L, 2L), S2001000 = c(22L, 21L, 20L, 22L,
21L, 22L, -5L, 22L, 21L, 19L), S2012301 = c(4L, 2L, 3L, 2L, 2L,
2L, -5L, 4L, 2L, 2L), S3801100 = c(23L, 22L, 21L, 23L, 22L, 23L,
-5L, 23L, 22L, 20L), S3813801 = c(4L, 2L, 3L, 2L, 2L, 2L, -5L,
4L, 2L, 2L), S5401000 = c(24L, 23L, -5L, 24L, 23L, 24L, 22L,
24L, 23L, 21L), S5413400 = c(4L, 2L, -5L, 2L, 2L, 2L, 0L, 4L,
4L, 2L), S7501200 = c(25L, -5L, -5L, 25L, 24L, 25L, 23L, 25L,
24L, 22L), S7514300 = c(4L, -5L, -5L, 2L, 2L, 2L, 0L, 4L, 4L,
4L), T0008500 = c(26L, -5L, -5L, 26L, 25L, 25L, -5L, 26L, 25L,
23L), T0014700 = c(4L, -5L, -5L, 2L, 2L, 2L, -5L, 4L, 4L, 4L),
T2011100 = c(27L, 26L, -5L, 27L, 26L, 26L, -5L, -5L, 26L,
24L), T2016800 = c(4L, 2L, -5L, 2L, 2L, 2L, -5L, -5L, 4L,
4L), T3601500 = c(28L, 27L, 26L, 28L, 26L, 27L, 26L, 28L,
27L, 25L), T3607100 = c(4L, 2L, 3L, 2L, 2L, 2L, 1L, 5L, 4L,
4L), T5201400 = c(29L, 28L, -5L, 29L, 28L, 28L, 27L, -5L,
28L, -5L), T5207400 = c(4L, 2L, -5L, 2L, 2L, 2L, 1L, -5L,
4L, -5L), T5207500 = c(4L, 2L, -5L, 2L, 2L, 2L, 1L, -5L,
4L, -5L), T6651300 = c(29L, 29L, 28L, 30L, 29L, 29L, 28L,
30L, 29L, -5L), T6657200 = c(4L, 2L, 3L, 2L, 2L, 2L, 1L,
5L, 5L, -5L), T6657300 = c(4L, 2L, 3L, 2L, 2L, 2L, 1L, 5L,
5L, -5L), T8123600 = c(32L, 31L, 30L, 32L, 31L, 31L, -5L,
-5L, 31L, -5L), T8129600 = c(4L, 2L, 3L, 2L, 2L, 2L, -5L,
-5L, 5L, -5L), T8129700 = c(4L, 2L, 3L, 2L, 2L, 2L, -5L,
-5L, 5L, -5L), U0001800 = c(34L, 33L, -5L, 34L, 33L, 34L,
32L, 34L, 33L, -5L), U0009400 = c(4L, 2L, -5L, 2L, 2L, 2L,
1L, 5L, 5L, -5L), U1838500 = c(-5L, 35L, 34L, 36L, 35L, 35L,
-5L, -5L, 35L, 34L), weight = c(607550L, 0L, 0L, 261156L,
450091L, 367309L, 0L, 0L, 618091L, 0L)), row.names = c(NA,
10L), class = "data.frame")