如何解决使用 ggplot2 在 Rshiny 中未显示绘图,但是没有错误
大家好,
我正在尝试使用 Rshiny 创建一个网页。它应该使用 ui 中的输入变量显示交互式绘图。当我尝试运行该应用程序时,它不会出现任何错误,但它也不会向我显示情节。你们有谁知道为什么它似乎没有显示情节?我相信我确实在 ui 和服务器中都指定了输出..我相信故障出在数据过滤(服务器)的某个地方。起初它过滤了我的数据,后来它突然不再起作用了。我真的不知道如何解决这个问题。
非常感谢任何帮助。
library(shiny)
library(dplyr)
library(ggplot2)
df <- read.csv("data_merged.csv",header = TRUE,sep = ";")
summarySE <- function(data=NULL,measurevar,groupvars=NULL,na.rm=TRUE,conf.interval=.95,.drop=TRUE) {
# New version of length which can handle NA's: if na.rm==T,don't count them
length2 <- function (x,na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else length(x)
}
# This is does the summary; it's not easy to understand...
datac <- ddply(data,groupvars,.drop=.drop,.fun= function(xx,col,na.rm) {
c( N = length2(xx[,col],na.rm=na.rm),mean = mean (xx[,sd = sd (xx[,na.rm=na.rm)
)
},na.rm
)
# Rename the "mean" column
datac <- rename(datac,c("mean"=measurevar))
datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean
# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval:
# e.g.,if conf.interval is .95,use .975 (above/below),and use df=N-1
ciMult <- qt(conf.interval/2 + .5,datac$N-1)
datac$ci <- datac$se * ciMult
return(datac)
}
normDataWithin <- function(data=NULL,idvar,betweenvars=NULL,na.rm=FALSE,.drop=TRUE) {
# Measure var on left,idvar + between vars on right of formula.
data.subjMean <- ddply(data,c(idvar,betweenvars),.fun = function(xx,na.rm) {
c(subjMean = mean(xx[,na.rm=na.rm))
},na.rm
)
# Put the subject means with original data
data <- merge(data,data.subjMean)
# Get the normalized data in a new column
measurenormedVar <- paste(measurevar,"normed",sep="")
data[,measurenormedVar] <- data[,measurevar] - data[,"subjMean"] +
mean(data[,measurevar],na.rm=na.rm)
# Remove this subject mean column
data$subjMean <- NULL
return(data)
}
summarySEwithin <- function(data=NULL,withinvars=NULL,idvar=NULL,.drop=TRUE) {
# Ensure that the betweenvars and withinvars are factors
factorvars <- sapply(data[,c(betweenvars,withinvars),drop=FALSE],FUN=is.factor)
if (!all(factorvars)) {
nonfactorvars <- names(factorvars)[!factorvars]
message("Automatically converting the following non-factors to factors: ",paste(nonfactorvars,collapse = ","))
data[nonfactorvars] <- lapply(data[nonfactorvars],factor)
}
# norm each subject's data
data <- normDataWithin(data,betweenvars,na.rm,.drop=.drop)
# This is the name of the new column
measurenormedVar <- paste(measurevar,sep="")
# Replace the original data column with the normed one
data[,measurevar] <- data[,measurenormedVar]
# Collapse the normed data - Now we can treat between and within vars the same
datac <- summarySE(data,groupvars=c(betweenvars,na.rm=na.rm,conf.interval=conf.interval,.drop=.drop)
# Apply correction from Morey (2008) to the standard error and confidence interval
# Get the product of the number of conditions of within-S variables
nWithinGroups <- prod(sapply(datac[,withinvars,FUN=nlevels))
correctionFactor <- sqrt( nWithinGroups / (nWithinGroups-1) )
# Apply the correction factor
datac$sd <- datac$sd * correctionFactor
datac$se <- datac$se * correctionFactor
datac$ci <- datac$ci * correctionFactor
return(datac)
}
ui <- fluidPage(
titlePanel(" "),sidebarPanel(
checkBoxGroupInput("gender","Gender",choices = list("Man" = "Man","Vrouw" = "Vrouw","Beiden" = "Beiden"),selected = "Beiden"),sliderInput("age","Leeftijd",min = 0,max = 100,value = c(18,100)),selectInput("province",h3("Provincie"),choices = list("Utrecht","Brabant","Drenthe","Flevoland","Friesland","Gelderland","Groningen","Limburg","Noord-Holland","Overijssel","Zeeland","Zuid-Holland","Allemaal"),selected = "Allemaal")),mainPanel(
h1("Gewoonteverandering in Nederland ten tijden van de SARS-Covid-19 pandemie",align = "center"),p("hier komt informatie over het onderzoek",column(3,selectInput("v1","Selecteer een gewoonte",choices = list("handen wassen bij thuiskomst" = 1,"Handen wassen voor het eten" = 2,"Handen wassen na een toiletbezoek" = 3,"Handen wassen in het algemeen" = 4,"Afstand houden met gasten thuis" = 5,"Afstand houden met vrienden buiten" = 6,"Afstand houden met vreemden in de supermarkt" = 7,"Afstand houden in het algemeen" = 8),selected = 1),column(9,plotOutput("plot1")
)
)
)
)
server <- function(input,output,session){
s <- reactive({
age <- as.numeric(input$age)
df2 <- df %>% filter(between(Age,age[1],age[2]))
if (input$gender != "Beiden")
df2 <- filter(df2,Gender == input$gender)
if (input$province != "Allemaal")
df2 <- filter(df2,Province %in% input$province)
df2
})
renderPlot(
if(input$v1 == 1){
df_compliance <- rbind(data.frame(ppn=df2$ppn,wave=df2$wave,compliance=df2$compliance_h1),data.frame(ppn=df2$ppn,compliance=df2$compliance_h1))
df_compliance$behavior <- c(rep("hand-washing (returning home)",nrow(df2)),rep("hand-washing (eating)",rep("hand-washing (toilet)",rep("1.5m distance (home)",rep("1.5m distance (outside)",rep("1.5m distance (shopping)",nrow(df2)))
df_compliance2 <- summarySE(df_compliance,measurevar="compliance",groupvars=c("wave","behavior"))
output$plot1 <- renderPlot(
ggplot(df_compliance2,aes(x = wave,y = compliance_h1)) + geom_point(size=3) + geom_line() + geom_errorbar(aes(ymin=compliance_h1-1.96*se,ymax=compliance_h1+1.96*se),size=1,width=0.3) + theme_light() + ylim(1,9) + scale_x_continuous(limits=c(0.5,10.5),breaks=seq(1,10,1)) +
scale_linetype_manual(values=c(rep("solid",3),rep("dashed",3))) +
theme(axis.title=element_text(size=18)) + theme(legend.position="bottom") +
theme(legend.text=element_text(size=18)) +
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size = 18)) +
theme(axis.text.y = element_text(size = 18)) +
xlab("Wave") + ylab("Behavior (rated compliance)"))
}else if (input$v1 == 2){
df_compliance <- rbind(data.frame(ppn=df2$ppn,compliance=df2$compliance_h2),compliance=df2$compliance_h2))
df_compliance$behavior <- c(rep("hand-washing (returning home)",3))) +
theme(axis.title=element_text(size=18)) + theme(legend.position="bottom") +
theme(legend.text=element_text(size=18)) +
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size = 18)) +
theme(axis.text.y = element_text(size = 18)) +
xlab("Wave") + ylab("Behavior (rated compliance)"))
}else if(input$v1 == 3){
df_compliance <- rbind(data.frame(ppn=df2$ppn,compliance=df2$compliance_h3),compliance=df2$compliance_h3))
df_compliance$behavior <- c(rep("hand-washing (returning home)",nrow(df2),nrow(df2))))
df_compliance2 <- summarySE(df_compliance,y = compliance_h3)) + geom_point(size=3) + geom_line() + geom_errorbar(aes(ymin=compliance_h3-1.96*se,ymax=compliance_h3+1.96*se),3))) +
theme(axis.title=element_text(size=18)) + theme(legend.position="bottom") +
theme(legend.text=element_text(size=18)) +
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size = 18)) +
theme(axis.text.y = element_text(size = 18)) +
xlab("Wave") + ylab("Behavior (rated compliance)"))
}else if(input$v1 == 4){
df_compliance <- rbind(data.frame(ppn=df2$ppn,compliance=df2$norm_h),compliance=df2$norm_h))
df_compliance$behavior <- c(rep("hand-washing (returning home)",y = norm_h)) + geom_point(size=3) + geom_line() + geom_errorbar(aes(ymin=norm_h-1.96*se,ymax=norm_h+1.96*se),3))) +
theme(axis.title=element_text(size=18)) + theme(legend.position="bottom") +
theme(legend.text=element_text(size=18)) +
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size = 18)) +
theme(axis.text.y = element_text(size = 18)) +
xlab("Wave") + ylab("Behavior (rated compliance)"))
}else if (input$v1 == 5){
df_compliance <- rbind(data.frame(ppn=df2$ppn,compliance=df2$compliance_d1),compliance=df2$compliance_d1))
df_compliance$behavior <- c(rep("hand-washing (returning home)",y = compliance_d1)) + geom_point(size=3) + geom_line() + geom_errorbar(aes(ymin=compliance_d1-1.96*se,ymax=compliance_d1+1.96*se),3))) +
theme(axis.title=element_text(size=18)) + theme(legend.position="bottom") +
theme(legend.text=element_text(size=18)) +
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size = 18)) +
theme(axis.text.y = element_text(size = 18)) +
xlab("Wave") + ylab("Behavior (rated compliance)"))
}else if(input$v1 == 6){
df_compliance <- rbind(data.frame(ppn=df2$ppn,compliance=df2$compliance_d2),compliance=df2$compliance_d2))
df_compliance$behavior <- c(rep("hand-washing (returning home)",y = compliance_d2)) + geom_point(size=3) + geom_line() + geom_errorbar(aes(ymin=compliance_d2-1.96*se,ymax=compliance_d2+1.96*se),3))) +
theme(axis.title=element_text(size=18)) + theme(legend.position="bottom") +
theme(legend.text=element_text(size=18)) +
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size = 18)) +
theme(axis.text.y = element_text(size = 18)) +
xlab("Wave") + ylab("Behavior (rated compliance)"))
}else if(input$v1 == 7){
df_compliance <- rbind(data.frame(ppn=df2$ppn,compliance=df2$compliance_d3),compliance=df2$compliance_d3))
df_compliance$behavior <- c(rep("hand-washing (returning home)",y = compliance_d3)) + geom_point(size=3) + geom_line() + geom_errorbar(aes(ymin=compliance_d3-1.96*se,ymax=compliance_d3+1.96*se),3))) +
theme(axis.title=element_text(size=18)) + theme(legend.position="bottom") +
theme(legend.text=element_text(size=18)) +
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size = 18)) +
theme(axis.text.y = element_text(size = 18)) +
xlab("Wave") + ylab("Behavior (rated compliance)"))
}else if(input$v1 == 8){
df_compliance <- rbind(data.frame(ppn=df2$ppn,compliance=df2$norm_d),compliance=df2$norm_d))
df_compliance$behavior <- c(rep("hand-washing (returning home)",nrow(df2)))
df_compliance2 <- summarySE(df_compliance,"behavior"))
output$plot1 <- renderPlot(
ggplot(df_compliance2,y = norm_d)) + geom_point(size=3) + geom_line() + geom_errorbar(aes(ymin=norm_d-1.96*se,ymax=norm_d+1.96*se),1)) +
scale_linetype_manual(values=c(rep("solid",3))) +
theme(axis.title=element_text(size=18)) + theme(legend.position="bottom") +
theme(legend.text=element_text(size=18)) +
theme(legend.title=element_blank()) +
theme(axis.text.x = element_text(size = 18)) +
theme(axis.text.y = element_text(size = 18)) +
xlab("Wave") + ylab("Behavior (rated compliance)"))
}
)
}
shinyApp(ui,server)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。