编辑:好的,我做了更多的问题解决,问题不在if(boolean)
语句中,它实际上在filter()
语句中的if()
语句中:
bar_mcgun <- bar_mcgun %>% filter(region == input$area)
input$area
正在返回NULL
。 area
是inputid
变量,其中包含以下renderUI()
语句:
output$region <- renderUI({
selectInput(inputId = "area", label = "Choose a region:", choices = c("Midwest", "Northeast",
"South Central", "South Atlantic", "West"), selected = "Northeast")
})
input$area
似乎因某种原因被隐藏了。请帮忙。
当我尝试拨打input$location
时,我也收到以下错误:
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.)
过去三个小时我一直在试图运行应用程序而被赶出RStudio。我非常确信该错误与我的ui.R文件中的radioButtons()
和我的server.R文件中的if-statements
之间的连接有关。
我检查了我的日志文件,我看到'Null不是一个对象'。我隔离了代码以检查read.csv2
是否存在错误以及随后的数据操作,但一切都显示出来。
如果你知道'Null'的来源,请告诉我。
ui.R
shinyUI(fluidPage(
titlePanel("McGun"),
sidebarLayout(
sidebarPanel(
helpText("Look at number of Gundealers, McDonald's, hospitals, and/or Population of states by region:"),
radioButtons('view', 'View U.S.A data according to state or region:', c('states', 'regions'), 'regions'),
conditionalPanel(
"input.view == 'states'",
uiOutput('state')
),
conditionalPanel(
"input.view == 'regions'",
uiOutput('region')
),
checkboxGroupInput("data",
label = "Data of interest:",
c( 'population','gundealers', "mcdonalds", 'hospitals'),
c( 'population','gundealers', "mcdonalds", 'hospitals')),
selectInput('pop', 'Population by:', c('1000', '10000', '100000'), '10000')
),
mainPanel(
plotOutput('barplot')
)
)
))
server.R
mcgun <- read.csv2("data/mcgun.csv2")
library(ggplot2)
library(dplyr)
library(reshape2)
location <- state.abb
location[51] <- 'DC'
location <- sort(location)
shinyServer(function(input, output) {
output$state <- renderUI({
selectInput(inputId = 'location',label = 'Choose a state:', choices = location, selected = location[1])
})
output$region <- renderUI({
selectInput(inputId = "area", label = "Choose a region:", choices = c("Midwest", "Northeast",
"South Central", "South Atlantic", "West"), selected = "Northeast")
})
output$barplot <- renderPlot({
scale_mcgun <- mcgun %>% mutate(population = round(population/as.integer(input$pop)))
scale_mcgun <- melt(scale_mcgun, id.vars = c('state', 'region'))
scale_mcgun <- scale_mcgun %>% arrange(state, variable)
if(is.null(input$data)==T){return()}
bar_mcgun <- data.frame()
for(i in 1:length(input$data)){bar_mcgun <- rbind(bar_mcgun, filter(scale_mcgun, variable == input$data[i]))}
if(input$view == 'regions'){
bar_mcgun <- bar_mcgun %>% filter(region == input$area)
ggplot(bar_mcgun, aes(state, value, fill = as.factor(variable))) +
geom_bar(stat = 'identity', position = 'dodge')
}
if(input$view == 'states'){
bar_mcgun <- bar_mcgun %>% filter(state == input$location)
ggplot(bar_mcgun, aes(state, value, fill = as.factor(variable))) +
geom_bar(stat = 'identity', position = 'dodge')
}
})
})
感谢您的时间。
答案 0 :(得分:1)
好的,我的问题有两方面:我没有使用被动语句,我使用了dplyr的被动语句。
例如:
output$region <- renderUI({
selectInput(inputId = "area", label = "Choose a region:", choices = c("Midwest", "Northeast",
"South Central", "South Atlantic", "West"), selected = "Northeast")
})
因为selectInput()
函数在renderUI()
函数内,所以前者现在是一个反应函数。这种新的反应意味着我不能像往常一样打电话给input$area
。我现在必须创建一个单独的反应变量,然后将该变量作为函数调用:
area <- reactive({ input$area })
area() #will equal input$area
第二个问题是在dplyr filter()
函数中使用反应变量。显然,由于shiny
和dplyr
使用非标准格式,因此两者之间存在冲突。我认为有一种方法可以解决这个冲突,但是我直截了当地理解它并通过摆脱dplyr
并使用base
过滤方法来解决这个问题。 / p>
干杯。
ui.R
shinyUI(fluidPage(
titlePanel("McGun"),
sidebarLayout(
sidebarPanel(
helpText("Look at number of Gundealers, McDonald's, Hospitals, and/or Population of states either individually or by region:"),
radioButtons('view', 'View U.S.A data according to state or region:', c('states', 'regions'), 'regions'),
conditionalPanel(
"input.view == 'states'",
uiOutput('state')
),
conditionalPanel(
"input.view == 'regions'",
uiOutput('region')
),
checkboxGroupInput("data",
label = "Data of interest:",
c( 'population','gundealers', "mcdonalds", 'hospitals'),
c( 'population','gundealers', "mcdonalds", 'hospitals')),
selectInput('pop', 'Population by:', c('1000', '10000', '100000'), '10000')
),
mainPanel(
plotOutput('barplot')
)
)
))
server.R
mcgun <- read.csv2("data/mcgun.csv2")
library(ggplot2)
library(dplyr)
library(reshape2)
location <- sort(c(state.abb, 'DC'))
shinyServer(function(input, output) {
output$state <- renderUI({
selectInput(inputId = 'location',label = 'Choose a state:', choices = location, selected = location[1])
})
output$region <- renderUI({
selectInput(inputId = "area", label = "Choose a region:", choices = c("Midwest", "Northeast",
"South Central", "South Atlantic", "West"), selected = "Northeast")
})
output$barplot <- renderPlot({
scale_mcgun <- mcgun %>% mutate(population = round(population/as.integer(input$pop)))
scale_mcgun <- melt(scale_mcgun, id.vars = c('state', 'region'))
scale_mcgun <- scale_mcgun %>% arrange(state, variable)
if(is.null(input$data)==T){return()}
bar_mcgun <- data.frame()
for(i in 1:length(input$data)){bar_mcgun <- rbind(bar_mcgun, filter(scale_mcgun, variable == input$data[i]))}
stateorregion <- reactive({ input$view })
if( stateorregion() == 'regions' ){
area <- reactive({ input$area })
ismatch <- bar_mcgun[,2] == area()
bar_mcgun <- bar_mcgun[ismatch,]
}
if( stateorregion() == 'states' ){
location <- reactive({ input$location })
ismatch <- bar_mcgun[,1] == location()
bar_mcgun <- bar_mcgun[ismatch,]
}
ggplot(bar_mcgun, aes(state, value, fill = as.factor(variable))) +
geom_bar(stat = 'identity', position = 'dodge')
})
})