0
votes

I'm new to Rshiny and I need help producing some summary statistics in Rshiny.

The aim of my task is to create a select input which allows a user to select one of 6 variables. After the user selects a variable, output is supposed to be generated to present summary statistics.

Here is my code:

library(shiny)
library(dplyr)
library(ggplot2)
library(markdown)
library(gtsummary)

#################################################################

ui <- fluidPage(
  navbarPage("",
    tabPanel("Data Exploration",
             sidebarLayout(
               sidebarPanel(
                 selectInput("variable",
                             "Variable",
                             colnames(data))
               ),
               mainPanel(
                 tableOutput("table")
               )
             )
    )
  )
)
                        
                


#################################################################


server <- function(input, output) {
  sum <- reactive({
      data <- data %>%
      select(input$variable) %>%
      summary()
  })
  
  output$table <- renderTable(sum())
}




#################################################################
shinyApp(ui, server)

Whenever I run the app, it generates this:

App summary statistics

App summary statistics

Basically, I'm wanting to create a really simple summary table where the column names just look like Min, Q1, Median, Mean, Q3, Max for numerical variables

and maybe just a simple table with Yes and No for column names for categorical variables.

I've also tried using the Summarise() function to manually create my summaries but this also leads to its own problems for me.

The actual statistics themselves aren't necessarily important to me right now as I just want to get the tables correct first. Can anyone help me fix my tables please?

1

1 Answers

1
votes

You can use separate and pivot_wider functions from package tidyr to sparate metric name and value, then move metrics as columns:

data <- data %>%
  select(input$variable) %>%
  summary() %>%
  as.data.frame() %>%
  tidyr::separate(Freq, c("Stat", "Value"), sep=":") %>%
  tidyr::pivot_wider(names_from =Stat, values_from = Value)