我基本上打算做的是根据用户选择一个子集'输入并做一些操作,比如计算每个数据集的百分比并绘制ggplot。但是当我写这个闪亮的应用程序服务器时,它一直给我错误:
Listening on http://127.0.0.1:5884
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
48: .getReactiveEnvironment()$currentContext
47: .subset2(x, "impl")$get
46: $.reactivevalues
45: $
44: eval
43: eval
42: subset.data.frame
41: subset
40: server [/Users/Qiner/Downloads/ME Analyst Assessment Files/server.R#7]
1: runApp
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
我已尝试按现有答案建议的reactive({})
功能,但错误仍然存在。有人知道答案吗?非常感谢!这是我的代码:
server <- function(input, output) {
library(shiny)
library(radarchart)
library(ggplot2)
library(reshape2)
top_version<-read.csv("top_version.csv",header = T)
#plot1 data
ds<-subset(top_version,top_version$ageInd==input$select1)
labs<-c('familiarity','favorability','consideration')
scores<-reactive({
scores<-list('Women'=c(nrow(ds[ds$familiarity==1 & ds$gender==2,])/nrow(ds[ds$gender==2,]),nrow(ds[ds$favorability==1 & ds$gender==2,])/nrow(ds[ds$gender==2,]),nrow(ds[ds$consideration==1 & ds$gender==2,])/nrow(ds[ds$gender==2,])),
'Men'=c(nrow(ds[ds$familiarity==1 & ds$gender==1,])/nrow(ds[ds$gender==1,]),nrow(ds[ds$favorability==1 & ds$gender==1,])/nrow(ds[ds$gender==1,]),nrow(ds[ds$consideration==1 & ds$gender==1,])/nrow(ds[ds$gender==1,])))
})
#plot2 data
scores2<-list("Age 18-24"=c(nrow(top_version[top_version$familiarity==1 & top_version$ageInd=="Age 18-24",])/nrow(top_version[top_version$ageInd=="Age 18-24",]),nrow(top_version[top_version$favorability==1 & top_version$ageInd=="Age 18-24",])/nrow(top_version[top_version$ageInd=="Age 18-24",]),nrow(top_version[top_version$consideration==1 & top_version$ageInd=="Age 18-24",])/nrow(top_version[top_version$ageInd=="Age 18-24",])),
"Age 25-44"=c(nrow(top_version[top_version$familiarity==1 & top_version$ageInd=="Age 25-44",])/nrow(top_version[top_version$ageInd=="Age 25-44",]),nrow(top_version[top_version$favorability==1 & top_version$ageInd=="Age 25-44",])/nrow(top_version[top_version$ageInd=="Age 25-44",]),nrow(top_version[top_version$consideration==1 & top_version$ageInd=="Age 25-44",])/nrow(top_version[top_version$ageInd=="Age 25-44",])),
"Age 45-64"=c(nrow(top_version[top_version$familiarity==1 & top_version$ageInd=="Age 45-64",])/nrow(top_version[top_version$ageInd=="Age 45-64",]),nrow(top_version[top_version$favorability==1 & top_version$ageInd=="Age 45-64",])/nrow(top_version[top_version$ageInd=="Age 45-64",]),nrow(top_version[top_version$consideration==1 & top_version$ageInd=="Age 45-64",])/nrow(top_version[top_version$ageInd=="Age 45-64",])),
"Age 65+"=c(nrow(top_version[top_version$familiarity==1 & top_version$ageInd=="Age 65+",])/nrow(top_version[top_version$ageInd=="Age 65+",]),nrow(top_version[top_version$favorability==1 & top_version$ageInd=="Age 65+",])/nrow(top_version[top_version$ageInd=="Age 65+",]),nrow(top_version[top_version$consideration==1 & top_version$ageInd=="Age 65+",])/nrow(top_version[top_version$ageInd=="Age 65+",])))
#plot3 data
com_1<-subset(top_version,familiarity==as.integer(input$select2) & favorability==as.integer(input$select3) & as.integer(input$select4))
com_2<-subset(top_version,familiarity==as.integer(input$select5) & favorability==as.integer(input$select6) & as.integer(input$select7))
abc_0<-reactive({
de_1<-rep(nrow(com_1),19)
sum_0<-apply(com_1[,1:19],2,sum)
portion_0<-sum_0/de_1
#return(portion_0)
})
abc_1<-reactive({
de_2<-rep(nrow(com_2),19)
sum_1<-apply(com_2[,1:19],2,sum)
portion_1<-sum_1/de_1
#return(portion_1)
})
df<-reactive({
df<-melt(data.frame(i=names(top_version)[1:19],name1=abc_0(),name2=abc_1()),id="i")
})
#begin plot
output$plot1 <- renderPlot({
chartJSRadar(scores=scores(), labs=labs,showToolTipLabel = T)
})
output$plot2 <- renderPlot({
chartJSRadar(scores=scores2, labs=labs,showToolTipLabel = T)
})
output$plot3 <- renderPlot({
ggplot(df(), aes(x=i,y=value,colour=variable,group=variable)) +
geom_line()+theme(axis.text.x = element_text(angle = 90, hjust = 1))
})
}