Complete statistics in one table. first browse and select your CSV file, then as you check the variable on the left side, statistics will appear. hope it helps
##################################
library(shiny)
library(shinyWidgets)
library(DT) # for using %>% which works as a pipe in R code
library(dplyr) # select functions are covered in the library
library(Rmisc) # to calculate confidence interval
library(moments) # to calculate skewness,Kurtosis in r
library(kableExtra) # to handle scroll_box function
Create the function.
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
ui <- fluidPage(
column(style = "border: 4px double red;height: 500px;overflow-y: auto;",
width = 2,
br(),
fileInput("file",
label = "Select: ONLY csv",
multiple = FALSE,
accept = c("csv",
"comma-separated-values,text",
".csv")),
x <- uiOutput('radio_Btns')
),
column(style = "border: 4px double red;height: 500px;overflow-y: auto;",
width = 10,
htmlOutput('msummarytext')
)
)
server <- function(input, output, session) {
vmy <- reactiveValues(mydata=NULL,zzDevic=NULL)
observeEvent(input$file,{
ext <- tools::file_ext(input$file$name)
vmy$mydata <- as.data.frame(read.csv(input$file$datapath,header = T))
vmy$mydata <- dplyr::select_if(vmy$mydata,is.numeric)
fnCreatezzDevictbl()
})
output$msummarytext <- renderUI({
if (length(input$file)==0){
return()
}
n<- ncol(vmy$zzDevic)
options(knitr.table.format = "html")
HTML( kable(vmy$zzDevic[vmy$zzDevic$Variable %in% c(input$radioreply), ],
escape = F,caption ="Descriptive Statistics",row.names=FALSE,
table.attr = "style='width:30%;'")%>%
kable_styling(font_size = 14, position = "center", html_font = "Cambria",fixed_thead = T) %>%
kable_paper("striped", full_width = F) %>%
column_spec(c(1,n), background = "steelblue",color = "white")%>%
row_spec(0, angle = 360,bold=TRUE)%>%
column_spec((1:n), bold = T,border_left = TRUE,border_right = TRUE) %>%
kableExtra::scroll_box(width = "100%", height = "385px")
)
})
output$radio_Btns <- renderUI({
if (length(input$file)==0){
return()
}
options <- colnames(vmy$mydata) # The options are dynamically generated on the server
prettyCheckboxGroup(
inputId = "radioreply",
label = "Select variable to Report Stat",
choices = options,
selected = options[1],
inline = FALSE,
status = "danger"
)# radioGroupbtn closure
}) # renderUI closure
fnCreatezzDevictbl <- function(){
vmy$zzDevic<<- data.frame (Variable=character(),
Mean =double(),
Median =double(),
Mode = double(),
SD=double(),
U_CI = double(),
L_CI = double(),
Conf_Intrl = double(),
Std_Error =double(),
Coff_Var = double(),
Inter_Qtrl = double(),
First_Qtrl=double(),
Third_Qtrl =double(),
Minimum =double(),
Maximum =double(),
Skewness=double(),
Kurtosis=double(),
Variable2 = character()
)
for (i in names(vmy$mydata)){
x<- vmy$mydata[ ,i]
vmy$zzDevic<- vmy$zzDevic %>% add_row(
Variable = i,
Mean = round(mean(x,na.rm = TRUE),3),
Minimum = round(min(x,na.omit(x)),3),
First_Qtrl = round(quantile(x, prob=c(0.25)),3),
Median = round(median(x,na.rm = TRUE),3),
Third_Qtrl = round(quantile(x, prob=c(0.75)),3),
Maximum = round(max(x,na.omit(x)),3),
SD = round(sd(x, na.rm=TRUE),3),
U_CI = round(CI(x,ci = 0.95)[1],3),
L_CI = round(CI(x,ci = 0.95)[3],3),
Conf_Intrl = round(CI(x,ci = 0.95)[1] - mean(x,na.rm = TRUE),3),
Skewness = round(skewness(x,na.rm = TRUE),3),
Kurtosis = round(kurtosis(x,na.rm = TRUE),3),
Inter_Qtrl = round(IQR(x,na.rm = TRUE),3),
Std_Error = round(sd(x, na.rm =TRUE)/sqrt(length(na.omit(x))),3),
Coff_Var = round(sd(x, na.rm =TRUE)/mean(x,na.rm = TRUE),3),
Mode = round(getmode(x),3),
Variable2 = i
)
}
}
}
shinyApp(ui, server)