rshinysummarytools

Variable names disappear when using summarytools::descr() with by() in Shiny app


The descr() function from R-package summarytools generates common central tendency statistics and measures of dispersion for numerical data in R.

When I use descr() with by() in a Shiny app, names of variable (features) contained in the data disappear and not displayed. Instead, the names are replaced by Var1, Var2, Var3 etc.

I do not really understand why the names disappear when I implement these code in the Shiny app (see below). Any idea?

# Install packages
source("https://bioconductor.org/biocLite.R")
biocLite("ALL")
biocLite("Biobase")
install.packages('devtools')
devtools::install_github('dcomtois/summarytools')

# Load packages
library(summarytools)
library(Biobase)
library(ALL) 

# Shiny Server
server <- function(input, output, session) {
  output$summaryTable <- renderUI({
    #-- Load the ALL data
    data(ALL)  
    #-- Subset
    eset_object <- ALL [1:3,] # choose only 3 variables 
    #-- The group of interest 
    eset_groups <-"BT"
    # print(rownames (eset_object)) # print variable names
    ALL_stats_by_BT <- by(data = as.data.frame(t(exprs(eset_object))), 
                          INDICES = (pData(eset_object)[,eset_groups]), 
                          FUN = descr, stats ="all", 
                          transpose = TRUE)

    view(ALL_stats_by_BT,
         method = 'render',
         omit.headings = FALSE,
         bootstrap.css = FALSE)
  })
}

# Shiny UI
ui <- fluidPage(theme = "dfSummary.css",
                fluidRow(
                  uiOutput("summaryTable")
                )
)

# Lauch
shinyApp(ui, server)

Solution

  • The headings cause problems, so you need to use:

    view(ALL_stats_by_BT,
            method = 'render',
            omit.headings = TRUE, # not FALSE
            bootstrap.css = FALSE)
    

    Also, install the latest version from gitHub (commited today) to have all the groups displayed (that was an issue with descr() in versions < 0.8.7)

    devtools::install_github("dcomtois/summarytools")

    EDIT

    I looked into it a bit more, and found that while this will work (I'm using a sample data frame for simplification)...

    server <- function(input, output, session) {
      library(summarytools)
    
      output$summaryTable <- renderUI({
    
        data(exams)
    
        stats_by_gender <- by(data = exams[,3:4],
                              INDICES = exams$gender, 
                              FUN = descr, stats ="all", 
                              transpose = TRUE)
    
        view(stats_by_gender,
             method = 'render',
             bootstrap.css = FALSE)
    
      })
    }
    

    ... This doesn't (meaning the variable names are lost):

    server <- function(input, output, session) {
      library(summarytools)
    
      output$summaryTable <- renderUI({
    
        data(exams)
    
        # this time using a temporary subsetted object     
        dat <- exams[,3:4]    
    
        stats_by_gender <- by(data = dat,
                              INDICES = exams$gender, 
                              FUN = descr, stats ="all", 
                              transpose = TRUE)
    
        view(stats_by_gender,
             method = 'render',
             bootstrap.css = FALSE)
    
      })
    }
    

    It has to do with the way summarytools retrieves object names, I'll need to look further into it.