0

I am writing a shiny app that takes user uploaded data and calculates summary statistics based on whatever variables are in their data. If you use the iris data set as an example, the user could upload that data set and calculate summary statistics for the steple length by species. I have code that works to create one summary table for each statistic, but I would like to be able to create a table that includes all of the summary statistics in one space. A section of the code I am using is below. You will see code for calculating the mean and median, but it presents as two tables. Is there a way to tweak this so that I can have multiple expressions in one table?

server <- function(input, output, session) {
    
    data <- reactive({
        file1 <- input$csvFile
        if (is.null(file1)) { 
            return() 
        } 
        data = read.csv(file=file1$datapath)
        data
    })

    output$var_dem <- renderUI({
        selectInput("dem", "Select a demographic variable.", choices= names(data()))
    })
        
    output$var_ui <- renderUI({
        selectInput("var", "Select an outcome variable.", choices= names(data()))
    })
    
    output$mean <- renderTable(
        tapply(data()[,input$var], data()[,input$dem], mean), 
        rownames = TRUE, 
        colnames = FALSE
    )
    
    output$median <- renderTable(
        tapply(data()[,input$var], data()[,input$dem], median), 
        rownames = TRUE, 
        colnames = FALSE
    )
  • 1
    For others to help you, please post a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – YBS Aug 04 '20 at 14:18

1 Answers1

0

In the case of mean & median you can create a dataframe column for each and pass it to renderTable:

library(datasets)

data.frame(mean   = tapply(iris[,"Sepal.Length"], iris[,"Species"], mean),
           median = tapply(iris[,"Sepal.Length"], iris[,"Species"], median))
eporetsky
  • 11
  • 3