动态分配值使用循环的列表

发布于 2025-01-31 16:20:42 字数 1620 浏览 4 评论 0原文

这篇文章是A 先前的帖子< /a>提供的答案非常合适。但是,我想通过动态分配列表项名称,以使用unique(iris $ tox)中的值而不是硬编码命名列表中的项目来使应用程序更加灵活。我试图通过通过a for loop分配命名列表中的项目来做到这一点:for(y in temp_list){paste0(“物种_”,m)= toxt_table [[m]]}> 。但是,当我运行该应用程序时,我会遇到一个错误,说“按范围订阅”。我包括以下完整的可复制示例:

data(iris)

ui <- fluidPage(
  
  mainPanel(
    fluidRow(selectInput("portfolio", label = "Select Species", choices = unique(iris$Species))),
    fluidRow(tableOutput("result"))
  )
  
)

server <- function(input, output) {
  
  species_list <- unique(iris$Species)
  
  species_function <- reactive({
    
    species_table <- list()
    for (i in reactiveValuesToList(species_list)){
      local({
        j <- i
        species_table[[j]] <<- iris[iris$Species==j,]
      })
    }
    
    return(list(
      
      for (m in species_list){
        paste0("species_",m) = species_table[[m]]      
      }
      
    ))
    
  })
  
  output$result <- renderTable({
    input$portfolio
    species_table2 <- list()
    
    for (p in reactiveValuesToList(species_list)){
      local({
        q <- p
        species_table2[[reactive({q})()]] <<- species_function()[[paste0("species_",reactive({q})())]][1:5, c("Sepal.Length", "Sepal.Width")]
      })
    }
    
    print(species_table2[[input$portfolio]])
    return(species_table2[[input$portfolio]])
    
  })
  
}

shinyApp(ui = ui, server = server)```

This post is an extension of a previous post for which the answer provided was perfectly suitable. However, I'd like to make the app more flexible by assigning the list item names dynamically, using the values in unique(iris$Species) instead of hard coding the items in the named list. I've tried to do this by assigning the items in the named list via a for loop: for (m in species_list){ paste0("species_",m) = species_table[[m]]}. However, when I run the app I get a error saying "subscript out of bounds". I've included the complete replicable example below:

data(iris)

ui <- fluidPage(
  
  mainPanel(
    fluidRow(selectInput("portfolio", label = "Select Species", choices = unique(iris$Species))),
    fluidRow(tableOutput("result"))
  )
  
)

server <- function(input, output) {
  
  species_list <- unique(iris$Species)
  
  species_function <- reactive({
    
    species_table <- list()
    for (i in reactiveValuesToList(species_list)){
      local({
        j <- i
        species_table[[j]] <<- iris[iris$Species==j,]
      })
    }
    
    return(list(
      
      for (m in species_list){
        paste0("species_",m) = species_table[[m]]      
      }
      
    ))
    
  })
  
  output$result <- renderTable({
    input$portfolio
    species_table2 <- list()
    
    for (p in reactiveValuesToList(species_list)){
      local({
        q <- p
        species_table2[[reactive({q})()]] <<- species_function()[[paste0("species_",reactive({q})())]][1:5, c("Sepal.Length", "Sepal.Width")]
      })
    }
    
    print(species_table2[[input$portfolio]])
    return(species_table2[[input$portfolio]])
    
  })
  
}

shinyApp(ui = ui, server = server)```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿! 2025-02-07 16:20:42

以下修改的代码,将@mrflick的反馈和将物种_list指定为反应性值,符合预期的功能。

library(shiny)
data(iris)


ui <- fluidPage(
  
  mainPanel(
    fluidRow(selectInput("portfolio", label = "Select Species", choices = unique(iris$Species))),
    fluidRow(tableOutput("result"))
  )
  
)

server <- function(input, output) {
  
  species_names <- as.character(unique(iris$Species))
  species_list <- reactiveValues()
  
  for(i in 1:length(species_names)) {
    
    species_list[[paste0("species_", i)]] <- species_names[[i]]
    
  }
  
  species_function <- reactive({
    
    species_table <- list()
    for (i in reactiveValuesToList(species_list)){
      local({
        j <- i
        species_table[[j]] <<- iris[iris$Species==j,]
      })
    }
    
    setNames(species_table, species_names)
    
  })
  
  output$result <- renderTable({
    input$portfolio
    species_table2 <- list()
    
    for (p in reactiveValuesToList(species_list)){
      local({
        q <- p
        species_table2[[reactive({q})()]] <<- species_function()[[reactive({q})()]][1:5, c("Sepal.Length", "Sepal.Width")]
      })
    }
    
    return(species_table2[[input$portfolio]])
    
  })
  
}

shinyApp(ui = ui, server = server)

The following modified code, incorporating feedback from @MrFlick and specifying species_list as a reactive value, functions as expected.

library(shiny)
data(iris)


ui <- fluidPage(
  
  mainPanel(
    fluidRow(selectInput("portfolio", label = "Select Species", choices = unique(iris$Species))),
    fluidRow(tableOutput("result"))
  )
  
)

server <- function(input, output) {
  
  species_names <- as.character(unique(iris$Species))
  species_list <- reactiveValues()
  
  for(i in 1:length(species_names)) {
    
    species_list[[paste0("species_", i)]] <- species_names[[i]]
    
  }
  
  species_function <- reactive({
    
    species_table <- list()
    for (i in reactiveValuesToList(species_list)){
      local({
        j <- i
        species_table[[j]] <<- iris[iris$Species==j,]
      })
    }
    
    setNames(species_table, species_names)
    
  })
  
  output$result <- renderTable({
    input$portfolio
    species_table2 <- list()
    
    for (p in reactiveValuesToList(species_list)){
      local({
        q <- p
        species_table2[[reactive({q})()]] <<- species_function()[[reactive({q})()]][1:5, c("Sepal.Length", "Sepal.Width")]
      })
    }
    
    return(species_table2[[input$portfolio]])
    
  })
  
}

shinyApp(ui = ui, server = server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文