Home > Mobile >  Ordering characters values alphabetically returns numeric values to me in a pickerInput()
Ordering characters values alphabetically returns numeric values to me in a pickerInput()

Time:01-07

I'm trying to alphabetically order the values of the pickerInput() shown below, but I get the numbers instead of characters even though I have used

stringsAsFactors = F 

in my dataframe.

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyWidgets)

df<-structure(list(Year = c(1951, 1951, 1951, 1951, 1951, 1951, 1954, 
                        1954, 1955, 1955), Country = c("Belgium", "France", "Germany", 
                                                       "Italy", "Luxembourg", "Netherlands", "Canada", "Portugal", "France", 
                                                       "Tunisia"), Scope = c(4, 4, 4, 4, 4, 4, 3, 3, 5, 5)), class = c("grouped_df", 
                                                                                                                       "tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(
                                                                                                                         Year = c(1951, 1954, 1955), .rows = structure(list(1:6, 7:8, 
                                                                                                                                                                            9:10), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                 "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                 ), row.names = c(NA, -3L), .drop = TRUE))
df<-data.frame(df,stringsAsFactors = F)
ui <- dashboardPage(
  dashboardHeader(title = "Scope Dashboard"),
  dashboardSidebar(
    ),
  dashboardBody(
    
    pickerInput(
      inputId = "cou",
      label = "Country", 
      choices = order(unique(df$Country)),
      selected = order(unique(df$Country))[1],
      multiple = F,
      
    )
  )
)

server <- function(input, output) {
  
  
}

shinyApp(ui, server)

CodePudding user response:

Use sort() instead of order()

See ?order

order returns a permutation which rearranges its first argument into ascending or descending order

versus ?sort

Sort (or order) a vector or factor (partially) into ascending or descending order.

i.e. order() returns a vector of ordered positions, essentially the index of the order vectored after ordering but not the ordered vector itself. sort() returns an ordered vector. However, sort() is only for vectors. For reordering dataframes, you'd use

x[order(x)]

Replacing sort() gives the app below:

enter image description here

Code:

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyWidgets)

df<-structure(list(Year = c(1951, 1951, 1951, 1951, 1951, 1951, 1954, 
                            1954, 1955, 1955), Country = c("Belgium", "France", "Germany", 
                                                           "Italy", "Luxembourg", "Netherlands", "Canada", "Portugal", "France", 
                                                           "Tunisia"), Scope = c(4, 4, 4, 4, 4, 4, 3, 3, 5, 5)), class = c("grouped_df", 
                                                                                                                           "tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(
                                                                                                                             Year = c(1951, 1954, 1955), .rows = structure(list(1:6, 7:8, 
                                                                                                                                                                                9:10), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                     "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                     ), row.names = c(NA, -3L), .drop = TRUE))

df<-data.frame(df,stringsAsFactors = F)
ui <- dashboardPage(
  dashboardHeader(title = "Scope Dashboard"),
  dashboardSidebar(
  ),
  dashboardBody(
    
    pickerInput(
      inputId = "cou",
      label = "Country", 
      choices = sort(unique(df$Country)),
      selected = sort(unique(df$Country))[1],
      multiple = F,
      
    )
  )
)

server <- function(input, output) {
  
  
}

shinyApp(ui, server)
  •  Tags:  
  • Related