首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R中的开关错误:替换长度为零

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

Stack Overflow用户
提问于 2020-12-08 19:22:31
回答 1查看 74关注 0票数 0

所以我得到了错误: ansp <- switch中的错误(valp,v1 = m1,v2 = m2,v3 = m3,v4 = m4 ),:替换的长度为零,我不知道会有什么问题。

M值是对应六个流行游戏名称的字符串,我试图使用开关根据正态分布中的值对名称进行排序。

代码:

代码语言:javascript
运行
复制
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)

所以,这是一个闪亮的应用程序,我正在为我的朋友和我开发,但开关部分给我带来了麻烦。

EN

回答 1

Stack Overflow用户

发布于 2020-12-08 19:32:48

如果'I1','I2',.,'I6‘是全局env中的对象

代码语言:javascript
运行
复制
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])
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65205537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档