编辑:好吧,我做了一点解决问题,这个问题是不是在if(boolean)
声明,它实际上是在我filter()
声明if()
语句中:闪亮:如果与server.R输入变量声明可能是NULL
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
以及后续的数据操作是否有错误,但所有内容都显示出来。
请让我知道,如果你有任何想法'空'来自何处。
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')
}
})
})
谢谢您的时间。
莫非使用返回()retuns一个NULL到plotOutput( 'barplot')。 日志文件中是否有更多上下文? –
我很确定'return()只是返回一个空的'mainPanel()'。我把它放在那里,以防万一复选框被点击。 – beemyfriend
我也不知道如何读取日志文件,这是非常大的,我从来没有看过日志文件。唯一突出的信息是Java:Null不是对象错误 – beemyfriend