Home > Mobile >  R Shiny how to render a list without displaying c(...)
R Shiny how to render a list without displaying c(...)

Time:02-22

I'm trying to display a list with the top three users based on a user-selected variable (see below). I have created a function that filters my table based on the selection of the Agency via the dropdown and retrieves the top 3 users in a column. I then transfromed the column into a string to render it in the app, but the results are being displayed in between c(...):

Top 3 Users

I'm okay with the format of the names separated by a comma, but I cannot find a way to eliminate the c(...).

This is the code for my function:

Top3UsersbyAgency <- function(filteredbyAgencyPool) {
                                  filteredbyAgencyPool %>%                       
                                        arrange(desc(MTD_Domestic)) %>% 
                                           group_by(userDisplayName) %>% 
                                              head(3) %>% 
                                                 select(userDisplayName) %>% 
                                                    na.exclude() %>% 
                                                      na_if("") %>% 
                                                        na.omit() %>% 
                                                          toString()
  
}

And this is the result:

> Top3UsersbyAgency(filteredbyAgencyPool)
[1] "c(\"Payal Malhotra\", \"Swati Parmar\", \"Unassigned\")"

In the app, I simply used textOutput in the ui and renderText in the server function. I tried to also use renderTable to display the results in the column, but it honestly looks ugly with the title of the column in the middle, so I'd rather display the information just as a list of names in plain text. Any suggestion on how to clean this string up?

Thanks in advance!

CodePudding user response:

Try to transform data.frame column to vector with %>% .[[1]]:

op3UsersbyAgency <- function(filteredbyAgencyPool) {
  filteredbyAgencyPool %>%                       
    arrange(desc(MTD_Domestic)) %>% 
    group_by(userDisplayName) %>% 
    head(3) %>% 
    select(userDisplayName) %>% 
    na.exclude() %>% 
    na_if("") %>% 
    na.omit() %>% 
    .[[1]] %>%
    toString()
}

As an illustration :

library(magrittr)
data.frame(c('a','b','c')) %>% toString()
#> [1] "c(\"a\", \"b\", \"c\")"

data.frame(c('a','b','c')) %>% .[[1]] %>% toString()
#> [1] "a, b, c"
  • Related