Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 中的if-else语句工作不正常_R_Shiny_Dt_Shiny Reactivity_Shinyapps - Fatal编程技术网

R 中的if-else语句工作不正常

R 中的if-else语句工作不正常,r,shiny,dt,shiny-reactivity,shinyapps,R,Shiny,Dt,Shiny Reactivity,Shinyapps,下面是我一直在编写的脚本。您会注意到,被动语句,table_data(),是进行重载的地方。当你运行应用程序时,你会看到所有三个过滤器都是空的。假设它们为空,但呈现的表应该生成一个表,该表表示最终else if语句中的过滤输出: res%groupby(level1)%%>%summary(total=sum(sales)),因此当应用程序加载时,您首先应该看到以下内容: 但它并没有产生这样的效果。它生成一个空表。我正在努力理解我做错了什么。以下是脚本: level1 <- c('1',

下面是我一直在编写的脚本。您会注意到,被动语句,
table_data()
,是进行重载的地方。当你运行应用程序时,你会看到所有三个过滤器都是空的。假设它们为空,但呈现的表应该生成一个表,该表表示最终
else if
语句中的过滤输出:

res%groupby(level1)%%>%summary(total=sum(sales))
,因此当应用程序加载时,您首先应该看到以下内容:

但它并没有产生这样的效果。它生成一个空表。我正在努力理解我做错了什么。以下是脚本:

level1 <- c('1','1','1','1','1','1','2','2','2','2','2','2','3','3','3','3','3','3')
level2 <- c('1A','1A','1B','1B','1C','1C','2A','2A','2B','2B','2C','2C','3A','3A','3B','3B','3C','3C')
level3 <- c('Jones','Smith','Johnson','James','Evans','Long','Roth','Goldberg','Ramos','Winston','Levin','Jackson','Fishman','Drake','Quinn','Lackey','Scott','McGuire')
sales <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)

df <- cbind(level1,level2,level3,sales) %>% as.data.frame() %>% mutate(level1 = as.character(level1), level2 = as.character(level2), level3 = as.character(level3), sales = as.integer(sales))

ui <- fluidPage(
  fluidRow(
    column(8,
           width = 10, offset = 1,
           tags$h3("Select Area"),
           panel(
            selectInput('lev1', label = 'Level 1', c(unique(sort(df$level1))), multiple = TRUE),
            uiOutput("level2"),
            uiOutput("level3")
           ),
           DT::dataTableOutput(outputId = "test")
    )
  )
)

server <- function(input, output, session) {

  output$level2 <- renderUI({
    selectizeInput("lev2", "Level 2", multiple = TRUE, c(unique(sort(df$level2[df$level1 ==input$lev1]))))
  }) 

  output$level3 <- renderUI({
    selectizeInput("lev3", "Level 3", multiple = TRUE, c(unique(sort(df$level3[df$level2 ==input$lev2]))))
  })


table_data <- reactive({

  if(!is.null(input$lev1) & is.null(input$lev2) & is.null(input$lev3)) #When lev1 is not null, but the other two are
    res <- df %>% filter(level1 %in% input$lev1) %>% group_by(level1,level2) %>% summarise(total=sum(sales))
  else
    if(is.null(input$lev1) | !is.null(input$lev1) & !is.null(input$lev2) & is.null(input$lev3)) #When lev2 is not null, lev3 is null, and lev1 can be either/or
      res <- df %>% filter(level2 %in% input$lev2) %>% group_by(level2,level3) %>% summarise(total=sum(sales))
    else
      if(is.null(input$lev1) | !is.null(input$lev1) & is.null(input$lev2) | !is.null(input$lev2) & !is.null(input$lev3)) #When lev3 is not null, lev1 can be either/or, and lev2 can be either/or
        res <- df %>% filter(level3 %in% input$lev3) %>% group_by(level1,level2,level3) %>% summarise(total=sum(sales))
      else
        if(is.null(input$lev1) & is.null(input$lev2) & is.null(input$lev3)) #When lev1 is null, lev2 is null, and lev3 is null (THIS ONE IS NOT WORKING CORRECTLY)
      res <- df %>% group_by(level1) %>% summarise(total=sum(sales))

})

output$test <- DT::renderDataTable({
  table_data()
})

}

shinyApp(ui,server)

level1只需使用
all(is.null(x,y,z))
或者使用大括号(对于嵌套在else中的最后一个if语句)?@NelsonGon-你能告诉我你的意思吗?我将最后一个更改为
else{all(is.null(input$lev1,input$lev2,input$lev3))res%groupby(level1)%%>%summary(total=sum(sales))}
但现在没有luckOn手机。如果我没弄错的话,is.null接受一个参数,所以您可能需要三个参数。另外,您不应该返回res而不是将其存储在变量中(不确定它是否与shiny不同)?!在我看来,您的第二个
if
条件在启动时为真:
为.null(输入$lev1)|!is.null(输入$lev1)&!is.null(输入$lev2)和..
。因此您得到了
df%>%过滤器(level2%在%input$lev2中)%>%…
这是一个0行TIBLE,因为
input$lev2
为空。@Stéphanelant那么最终的输出会是什么样子呢?