Skip to content

Commit 5705089

Browse files
committed
new function to streamline attribute totals
1 parent 079a58b commit 5705089

2 files changed

Lines changed: 25 additions & 54 deletions

File tree

inst/shiny/global.R

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,19 @@ stat.comparison <- function(statlist) {
149149
return(statvec)
150150
}
151151

152-
hist.info <- function(x, breaks) {
153-
out <- hist(x, breaks = breaks, plot = FALSE)
154-
barname <- paste(out$breaks[1:2], collapse = "-")
155-
for(i in seq(length(out$breaks) - 2)){
156-
barname <- append(barname, paste(out$breaks[i+1]+1, out$breaks[i+2], sep = "-"))
152+
attr.info <- function(df, colname, numattrs, breaks) {
153+
lvls <- length(unique(df[[colname]]))
154+
if(colname %in% numattrs & lvls > 9){
155+
tab <- hist(df[[colname]], breaks = breaks, plot = FALSE)
156+
barname <- paste(tab$breaks[1:2], collapse = "-")
157+
for(i in seq(length(tab$breaks) - 2)){
158+
barname <- append(barname, paste(tab$breaks[i+1]+1, tab$breaks[i+2], sep = "-"))
159+
}
160+
tab <- tab$counts
161+
names(tab) <- barname
162+
} else {
163+
tab <- table(df[[colname]])
157164
}
158-
159-
out$percent <- out$counts/sum(out$counts)
160-
names(out$counts) <- barname
161-
names(out$percent) <- barname
162-
return(out)
165+
return(tab)
163166
}
164167

165-
attrtab <- function(obj, breaks) {
166-
tab <- hist.info(obj, breaks = breaks)
167-
tab < - rbind(tab, tab/sum(tab))
168-
rownames(tab) <- c("count", "percent")
169-
}

inst/shiny/server.R

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,33 +1452,13 @@ output$attrtbl_sm <- renderPrint({
14521452
attrname <- input$attrcols
14531453
tbl_list <- list()
14541454
if(ntbl == 1){
1455-
lvls <- length(unique(nwdf()[[attrname]]))
1456-
if(attrname %in% numattr() & lvls > 9){
1457-
tab <- hist(nwdf()[[attrname]], breaks = 10, plot = FALSE)
1458-
barname <- paste(tab$breaks[1:2], collapse = "-")
1459-
for(i in seq(length(tab$breaks) - 2)){
1460-
barname <- append(barname, paste(tab$breaks[i+1]+1, tab$breaks[i+2], sep = "-"))
1461-
}
1462-
tab <- tab$counts
1463-
names(tab) <- barname
1464-
} else {
1465-
tab <- table(nwdf()[[attrname]])
1466-
}
1455+
tab <- attr.info(df = nwdf(), colname = attrname,
1456+
numattrs = numattr(), breaks = 10)
14671457
tbl_list[[attrname]] <- tab
14681458
} else {
14691459
for(a in attrname){
1470-
lvls <- length(unique(nwdf()[[a]]))
1471-
if(a %in% numattr() & lvls > 9){
1472-
tab <- hist(nwdf()[[a]], breaks = 10, plot = FALSE)
1473-
barname <- paste(tab$breaks[1:2], collapse = "-")
1474-
for(i in seq(length(tab$breaks) - 2)){
1475-
barname <- append(barname, paste(tab$breaks[i+1]+1, tab$breaks[i+2], sep = "-"))
1476-
}
1477-
tab <- tab$counts
1478-
names(tab) <- barname
1479-
} else {
1480-
tab <- table(nwdf()[[a]])
1481-
}
1460+
tab <- attr.info(df = nwdf(), colname = a,
1461+
numattrs = numattr(), breaks = 10)
14821462
tbl_list[[a]] <- tab
14831463
}
14841464
}
@@ -1496,12 +1476,8 @@ output$attrhist <- renderPlot({
14961476
plot(density(nwdf()[[attrname]]), main = attrname,
14971477
col = "#076EC3", lwd = 2)
14981478
} else {
1499-
if(attrname %in% numattr() & lvls > 9){
1500-
out <- hist.info(nwdf()[[attrname]], breaks = 10)
1501-
tab <- out$counts
1502-
} else {
1503-
tab <- table(nwdf()[[attrname]])
1504-
}
1479+
tab <- attr.info(df = nwdf(), colname = attrname,
1480+
numattrs = numattr(), breaks = 10)
15051481
if(input$attrhistaxis == "percent"){
15061482
tab <- tab/sum(tab)
15071483
}
@@ -1515,15 +1491,12 @@ output$attrhist <- renderPlot({
15151491
if(input$attrhistaxis == "density" & a %in% numattr() & lvls > 9){
15161492
plot(density(nwdf()[[a]]), main = a, col = "#076EC3", lwd = 2)
15171493
} else {
1518-
if(a %in% numattr() & lvls > 9){
1519-
tab <- hist.info(nwdf()[[a]], breaks = 10)
1520-
} else {
1521-
tab <- table(nwdf()[[a]])
1522-
}
1523-
if(input$attrhistaxis == "percent"){
1524-
tab <- tab/sum(tab)
1525-
}
1526-
barplot(tab, main = a, col = histblue)
1494+
tab <- attr.info(df = nwdf(), colname = a,
1495+
numattrs = numattr(), breaks = 10)
1496+
if(input$attrhistaxis == "percent"){
1497+
tab <- tab/sum(tab)
1498+
}
1499+
barplot(tab, main = a, col = histblue)
15271500
}
15281501
}
15291502
}

0 commit comments

Comments
 (0)