The below example code produces a stratification table of raw data, whereby the user can specify the grouping ranges of the table. See more complete example of flexible stratification table in post 
Below is the revised output$strat_data... code (server section) reflecting solution proposed by oskjerv (the 2 added rows are flagged):
output$strat_data <- renderTable({
breaks <- seq(min(data[,5], na.rm=TRUE),
max(data[,5], na.rm=TRUE),
by=input$strat_gap)
if(max(breaks) < max(data[,5], na.rm=TRUE)){breaks <- c(breaks, max(breaks) input$strat_gap)}
data <- data %>%
mutate(sumvar = cut(!!sym("Values"), breaks=breaks, include.lowest=TRUE)) %>%
group_by(sumvar) %>%
summarise(Count = n(), Values = sum(!!sym("Values"))) %>%
complete(sumvar, fill = list(Count = 0, Values = 0)) %>%
ungroup %>%
mutate(Count_pct = sprintf("%.1f%%", (Count/sum(Count))*100),
Values_pct = sprintf("%.1f%%", (Values/sum(Values))*100)) %>%
dplyr::select(everything(), Count, Count_pct, Values, Values_pct) %>%
mutate( sumvar = str_replace(sumvar, "^\\(", "[")) %>% # ADDED
mutate( sumvar = str_replace(sumvar, "\\,", " to ")) # ADDED
names(data)[1] <- "Ranges"
data
})
CodePudding user response:
You should probably play around with str_replace and regular expressions.
https://regexr.com/ is a great place to start.
For instance, the code below replaces an opening parentheses at the start of the string with [.
^ is an anchor, matching at the start of the string. \\ is to escape the parentheses, since ( has other functions when using regexpr.
str_replace("(-0,5]", "^\\(", "[")
I am sure there are other answers here on SO that also will be of help.
