【问题标题】:Sorting a named list using actionButton Shiny使用 actionButton Shiny 对命名列表进行排序
【发布时间】:2017-06-11 13:42:31
【问题描述】:

我有一个命名列表如下:

vegshop <- list(
    "FRUITS" = c("MANGO", "JACKFRUIT", "BANANA"),
    'VEGETABLES' = c("OKRA", "BEANS", "CABBAGE")
)

我正在尝试根据名称对列表进行排序,这很好用。

vegshop[order(names(vegshop), decreasing = F)]

但是,当我尝试使用 actionButton() 时,我收到以下错误:

the condition has `length > 1` and only the first element will be used

Warning: Error in order: unimplemented type 'list' in 'orderVector1'

一个可行的例子如下:

vegshop <- list(
    "FRUITS" = c("MANGO", "JACKFRUIT", "BANANA"),
    'VEGETABLES' = c("OKRA", "BEANS", "CABBAGE")
)
grocer <- list(
    "GROCERY" = c("CEREALS", "PULSES", "TOILETRIES"),
    "CLEANERS" = c("DETERGENTS", "FLOOR CLEANERS", "WIPES")
)

library(shiny)

ui <- shinyUI(
    fluidPage(
    actionButton(style = "font-size: 10px;",inputId = "a2z", label = "Sort-A-Z", icon = icon("sort-alpha-asc")),
    radioButtons(inputId = "shopsel", label = "SELECT SHOP", choices = c("SHOPS","SUPERMARKETS"), selected = "SHOPS", inline = TRUE),
    uiOutput("shoplist")))

server <- function(session,input, output) {
    output$shoplist <- renderUI({
        if(input$shopsel == "SHOPS") {
         selectInput(inputId = "vegShopList", label = "SHOPLIST", choices = vegshop, selected = c('MANGO', 'JACKFRUIT', 'BANANA'), multiple = TRUE, selectize = FALSE)   
        } else if(input$shopsel == "SUPERMARKETS") {
        selectInput(inputId = "smList", label = "SUPERMARKET", choices = grocer, selected = c('CEREALS', 'PULSES', 'TOILETRIES'), multiple = TRUE, selectize = FALSE)    
        }
    })

    observeEvent(input$a2z, {
        if(input$shopsel == "SHOPS") {
            updateSelectInput(session, inputId = "vegShopList", choices = vegshop[order(vegshop), decreasing = F], selected = NULL)
        } else if(input$shopsel == "SUPERMARKETS") {
            updateSelectInput(session, inputId = "smList", choices = grocer[order(grocer), decreasing = F], selected = NULL)
        }
        })
}

shinyApp(ui = ui, server = server)

如何使用actionButton() 获取按名称排序的列表。

【问题讨论】:

    标签: r list sorting shiny action-button


    【解决方案1】:

    你有一个错字: 外面闪亮你写:

    vegshop[order(names(vegshop), decreasing = F)]
    

    在闪亮之内:

    vegshop[order(vegshop), decreasing = F]
    

    以下闪亮的代码 sn-p 可能也是如此:

    grocer[order(grocer), decreasing = F]
    

    【讨论】:

      猜你喜欢
      • 2015-01-28
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      相关资源
      最近更新 更多