闪亮:server.R中带有输入变量的if语句可能为NULL

闪亮:server.R中带有输入变量的if语句可能为NULL,r,null,shiny,R,Null,Shiny,编辑:好的,我解决了更多的问题,问题不在if(boolean)语句中,而是在if()语句中的filter()语句中: bar\u mcgun%filter(region==input$area) input$area正在返回NULLarea是一个inputid变量,在下面的renderUI()语句中: output$region <- renderUI({ selectInput(inputId = "area", label = "Choose a region:", choi

编辑:好的,我解决了更多的问题,问题不在
if(boolean)
语句中,而是在
if()
语句中的
filter()
语句中:

bar\u 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")
})
服务器.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')

    }
  })

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

  })

})

mcgun好的,我的问题有两个:我没有使用被动语句,我在dplyr中使用被动语句

例如:

output$region <- renderUI({
    selectInput(inputId = "area", label =  "Choose a region:", choices =  c("Midwest", "Northeast",
        "South Central", "South Atlantic", "West"), selected =  "Northeast")
})
第二个问题是在
dplyr filter()
函数中使用反应变量。显然,两者之间存在冲突,因为
shinny
dplyr
使用非标准格式。我认为有一种方法可以解决这一冲突,但我还是无法理解它,只是通过去掉
dplyr
并使用
base
过滤方法来解决它

干杯

用户界面

服务器.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')

    }
  })

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

  })

})

mcgun可能是因为使用return()将NULL重新调用到plotOutput('barplot')。日志文件中是否有更多上下文?我很确定
return()
只返回一个空的
mainPanel()
。我把它放在那里,以防没有任何复选框被点击。我也不知道如何读取日志文件,它非常大,我以前从未看过日志文件。唯一突出的信息是Java:Null不是对象错误
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')

  })

})