Sorting a named list using actionButton Shiny

Apricot

I have a named list as follows:

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

I am trying to order the list based on the names, and this works fine.

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

However when I try to do using an actionButton(), I am getting the following error:

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

or

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

A workable example is as follows:

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)

How could I get the list sorted by the names using the actionButton().

Tonio Liebrand

You have a typo: Outside shiny you write:

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

Within shiny:

vegshop[order(vegshop), decreasing = F]

The same probably holds for the following shiny code snippet:

grocer[order(grocer), decreasing = F]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I hide an actionbutton in Shiny using Javascript

From Dev

renderDataTable or Table in Shiny on actionButton

From Dev

Update label of actionButton in shiny

From Dev

Duplicate an icon in shiny actionButton

From Dev

renderDataTable or Table in Shiny on actionButton

From Dev

R shiny uncheck checkboxGroup with actionbutton

From Dev

R shiny: Add weblink to actionButton

From Dev

R Shiny error when using shinyBS to disable actionButton if right-side of chooserInput is empty

From Dev

shiny - actionButton #1 works exact same actionButton #2 doesnt

From Dev

Sorting a list using a regex in Python

From Dev

Sorting on List using Comparator in Java

From Dev

Sorting and the grouping the list using Lambda

From Dev

Sorting API list using filter

From Dev

Sorting a list<object> using an array

From Dev

Sorting a list using two keys

From Dev

Sorting and arranging a list using pandas

From Dev

Cannot Update SQL data in Shiny + RMySQL by actionButton()

From Dev

opening a new empty shiny ui through actionbutton

From Dev

Shiny Application actionButton click on page load

From Dev

Refresh input values in shiny app with `actionButton`

From Dev

shiny: different tasks with actionButton for each menuSubItems

From Dev

sorting a list of strings using another list of strings

From Dev

Replacing values in a column using a named list

From Dev

Sorting dict using tuple or list in Python

From Dev

Python: using a dict to speed sorting of a list of tuples

From Dev

Sorting a List<> using linq in C#

From Dev

c++11 sorting list using lambda

From Dev

dynamic list sorting without using sort array

From Dev

Sorting a List alphabetically using compareTo() method

Related Related

HotTag

Archive