简体   繁体   English

R 中的开关错误:替换长度为零

[英]Switch Error in R: replacement has length zero

So I'm getting the error: Error in ans[p] <- switch(val[p], v1 = m[1], v2 = m[2], v3 = m[3], v4 = m[4], : replacement has length zero and I have no idea what could be the problem.所以我得到了错误: ans[p] <- switch(val[p], v1 = m[1], v2 = m[2], v3 = m[3], v4 = m[4] 中的错误, : 替换的长度为零,我不知道可能是什么问题。

The m values are strings that correspond to the names of six popular games, and I am trying to sort the names based on values from a normal distribution using a switch. m 值是对应于六种流行游戏名称的字符串,我正在尝试使用开关根据正态分布的值对名称进行排序。

Code:代码:

library("shiny")

ui <- fluidPage(
  
  titlePanel(h1(strong("Game Selector"))),
  
  sidebarLayout(
    
    sidebarPanel(
      selectInput("I1","Game You Want to Play Most", choices = c("Ark", "Divinity", "Dark_Souls_1", "Dark_Souls_3", "Pokemon", "Factorio"), selected = "ark"),
      selectInput("I2","Game You Want to Play Most", choices = c("Ark", "Divinity", "Dark_Souls_1", "Dark_Souls_3", "Pokemon", "Factorio"), selected = "Divinity"),
      selectInput("I3","Game You Want to Play Most", choices = c("Ark", "Divinity", "Dark_Souls_1", "Dark_Souls_3", "Pokemon", "Factorio"), selected = "Pokemon"),
      selectInput("I4","Game You Want to Play Most", choices = c("Ark", "Divinity", "Dark_Souls_1", "Dark_Souls_3", "Pokemon", "Factorio"), selected = "Dark_Souls_1"),
      selectInput("I5","Game You Want to Play Most", choices = c("Ark", "Divinity", "Dark_Souls_1", "Dark_Souls_3", "Pokemon", "Factorio"), selected = "Dark_Souls_3"),
      selectInput("I6","Game You Want to Play Most", choices = c("Ark", "Divinity", "Dark_Souls_1", "Dark_Souls_3", "Pokemon", "Factorio"), selected = "Factorio")
    ),
    
    mainPanel(
      textOutput("txtgame")
    )
  )
)

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

re = reactive({
  
  v1 = mean(rnorm(1,1000,100))
  v2 = mean(rnorm(1,1000,100))
  v3 = mean(rnorm(1,1000,100))
  v4 = mean(rnorm(1,1000,100))
  v5 = mean(rnorm(1,1000,100))
  v6 = mean(rnorm(1,1000,100))
  
  x = numeric(36)
  count = 0
  m = c(input$I1, input$I2, input$I3, input$I4, input$I5, input$I6)
  
  vals = c(v1,v2,v3,v4,v5,v6)
  val = sort(vals)
  
  q=0
  
  for(i in 1:6){
    
    for(j in 1:6){
      q = q + 1
      if(j == i){
        
        x[q] = 0
      }
      else{
        if(m[i] == m[j]){
          
          x[q] = 1
        }
        else{
          
          x[q] = 0
        }
      }
    }
  }
  
  for(k in 1:36){
    if(x[k] == 0){
      
      count = count + 1
    }
    
  }
  if(count == 36){
    chbx = TRUE
  }
  else(
    chbx = FALSE
  )
  
  if(chbx){
    ans = numeric(6)
    
  ans = numeric(6)
for(p in 1:6){

  ans[p] = switch (val[p],
                   v1 = m[1],
                   v2 = m[2],
                   v3 = m[3],
                   v4 = m[4],
                   v5 = m[5],
                   v6 = m[6]
  )
}

    return(ans)
  }
  else{
    ans2 = "You have selected the same game more than once"
    return(ans2)
  }
  
  
})
  
  output$txtgame = renderText(re())
}


shinyApp(ui, server)

So its a shiny app, that I'm developing for my friend and I but the switch part is giving me trouble.所以它是一个 shiny 应用程序,我正在为我和我的朋友开发,但开关部分给我带来了麻烦。

If the 'I1', 'I2', ..., 'I6' are objects in the global env如果 'I1', 'I2', ..., 'I6' 是全局环境中的对象

m <- paste0("I", 1:6)

ans <- numeric(6)
for(p in 1:6) {
        ans[p] <- switch(p, 
           `1` =  get(m[1]), 
           `2` = get(m[2]), 
           `3` = get(m[3]),
           `4` = get(m[4]),
           `5` = get(m[5]),
           `6` = get(m[6])
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM