2016-07-16 54 views
0

编辑:好吧,我做了一点解决问题,这个问题是不是在if(boolean)声明,它实际上是在我filter()声明if()语句中:闪亮:如果与server.R输入变量声明可能是NULL

bar_mcgun <- bar_mcgun %>% filter(region == input$area)

input$area将返回NULLareainputid变量在以下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') 

    } 
    }) 

}) 

谢谢您的时间。

+0

莫非使用返回()retuns一个NULL到plotOutput( 'barplot')。 日志文件中是否有更多上下文? –

+0

我很确定'return()只是返回一个空的'mainPanel()'。我把它放在那里,以防万一复选框被点击。 – beemyfriend

+0

我也不知道如何读取日志文件,这是非常大的,我从来没有看过日志文件。唯一突出的信息是Java:Null不是对象错误 – beemyfriend

回答

0

好吧,我的问题是双重的:我没有使用反应性陈述,我用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()功能反应变量。显然,由于shinydplyr使用非标准格式,因此两者之间存在冲突。我认为有一种方法可以解决这个冲突,但我直接无法理解它,并通过摆脱dplyr和使用base过滤方法来取消懒惰的根。

干杯。

ui。ř

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') 

    }) 

})