Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny gvisTable with columns selected by user in defined order

Tags:

r

shiny

I have an R shiny webpage where I currently use gvisTable to show a selection of columns from a data.frame. The rows are dynamically selected by the user with the sidebarPanel, but right now the columns are hard-coded inside the gvisTable call.

I would like to allow the user to dynamically select the columns from a drop-down menu (see snapshot of a similar system from a non-shiny webpage). The key feature I want is to allow resorting of the columns.

enter image description here

Any ideas how to pass this sorted selection of columns in shiny?

I don't mind using something else instead of gvisTable if it does the job.

EDIT: Thanks for showing a solution using the sortable answer. It works both for my old and new versions of shiny. Yet, this does not seem to remember the order upon hitting "Refresh", which would be really nice to have.

So, can it save the last chosen order as a browser cookie or a similar way? The server is authenticated, and I've been told I could put the variable order in a list with the user id as the key. An example of that would be great.

like image 748
719016 Avatar asked Jan 28 '14 11:01

719016


1 Answers

In Shiny you would have to use multiple selectInput's. However, you could install ShinySky by ZJ (https://github.com/AnalytixWare/ShinySky) and use his select2 binding which allows sorting. Alternatively, you could modify the sortable binding at https://github.com/mostly-harmless/sortable.

Edit: I don't know about cookies. I use sortable in a larger app. There I have an action button to save the order selected by the user. See Data > Transform > Reorder columns. In the app data is stored in a reactiveValue. To save the data order I use values[[input$datasets]] <- values[[input$datasets]][,input$tr_reorder_cols] where input$datasets is the active dataset, input$tr_reorder_cols is the users selected variable ordering, and values is the reactiveValue that contains the data.

The source for the app is on Github: https://github.com/mostly-harmless/radiant

As an alternative, you could also save the order of the variables in a reactiveValue. See the Shiny documentation for details.

Edit:

In global.R define a reactiveValue:

savedOrder <- reactiveValues()

When a user changes the order (this assumes you have userid available as a variable in R):

if(!is.null(input$sortable)) {
    savedOrder[[userid]] <- input$sortable
}

Also, you could pass the id-value to returnOrder in case of a refresh:

if(!is.null(savedOrder[[userid]])) {
    returnOrder("sortable",savedOrder[[userid]])
} else {
    returnOrder("sortable",colnames(dat))
}
like image 143
Vincent Avatar answered Nov 12 '22 21:11

Vincent