簡體   English   中英

R 中的開關錯誤:替換長度為零

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

所以我得到了錯誤: ans[p] <- switch(val[p], v1 = m[1], v2 = m[2], v3 = m[3], v4 = m[4] 中的錯誤, : 替換的長度為零,我不知道可能是什么問題。

m 值是對應於六種流行游戲名稱的字符串,我正在嘗試使用開關根據正態分布的值對名稱進行排序。

代碼:

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)

所以它是一個 shiny 應用程序,我正在為我和我的朋友開發,但開關部分給我帶來了麻煩。

如果 '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