r - Reactive dataframe with sortable columns order -
in shiny app, user can choose dataset, 1 displayed , user can sort order of columns (thanks shinyjqui
):
library(shiny) library(shinyjqui) library(googlevis) ui <- shinyui(fluidpage( sidebarlayout( sidebarpanel( selectinput("dataset", "select dataset", choices = c("iris", "mtcars")), uioutput("sortable") ), # mainpanel( htmloutput("gvtable") ) ) )) server <- shinyserver(function(input, output) { dat <- reactive({ switch(input$dataset, iris = iris, mtcars = mtcars) }) output$sortable <- renderui({ jqui_sortabled( do.call(function(...) tags$ul(id="list", ...), lapply(colnames(dat()), function(col) tags$li(col))) ) }) output$gvtable <- rendergvis({ validate(need(input$list_order, message=false)) if(all(input$list_order[["html"]] %in% names(dat()))){ colorder <- input$list_order[["html"]] }else{ colorder <- names(dat()) } gvistable(dat()[, colorder], options = list( gvis.editor="edit me!", page="enable") ) }) }) runapp(list(ui=ui, server=server))
this works fine when run app: iris
dataset displayed , can play columns order (the list of columns in sidebar sortable):
but when choose other dataset, list of columns not sortable. why , how fix that?
this works using different id sortable list:
randomid <- eventreactive(dat(), { paste0("list", rpois(1, 1000)) }) output$sortable <- renderui({ jqui_sortabled( do.call(function(...) tags$ul(id=randomid(), ...), lapply(colnames(dat()), function(col) tags$li(col))) ) }) output$gvtable <- rendergvis({ validate(need(input[[paste0(randomid(), "_order")]], message=false)) columns <- input[[paste0(randomid(), "_order")]][["html"]] if(all(columns %in% names(dat()))){ colorder <- columns }else{ colorder <- names(dat()) } gvistable(dat()[, colorder], options = list( gvis.editor="edit me!", page="enable") ) })
Comments
Post a Comment