I have a section in my Shiny app that generates a list.
names of the list are column names of the dataframe
we will calculate on,
list items contain the calculations we want
Looking to do this:
apply
to all list
names:
for listname
(column
) x calculate function
n,m,o
over df column
x
and name the resulting column 'x.n
' i.e. 'cyl.mean'
, 'mpg.sum
'
to get a dataframe
of summary statistics PER GROUP (mtcars$cyl) in this case as example
It is linked to this question, but there the example data
used a separate list
of column
names, and apply
the same functions
to all those columns
from another list
. I'm looking to move forward to apply
unique
sets of functions
to different columns
The list my app spits out looks like this:
mylist
$disp
[1] "sum" "mean"
$hp
[1] "sd"
$drat
[1] "sum" "mean"
$wt
[1] "max"
expected output:
cyl disp.sum hp.sd drat.sum drat.mean wt.max
4 x ....
6 x ....
8 x ....
The little Shiny app to create the list:
library(shiny)
library(data.table)
library(shinyjs)
Channels <- names(mtcars)[3:8]
ui <- fluidPage(
shinyjs::useShinyjs(),
h5('Channels', style = 'font-weight:bold'),
uiOutput('ChannelCheckboxes'),
h5('Statistics', style = 'font-weight:bold'),
uiOutput('CalculationCheckboxes')
)
server <- function(input, output, session) {
values <- reactiveValues(Statisticlist = list())
## build observer to deselect all sub category checkboxes if channel is deselected
lapply(Channels, function(x) {
observeEvent(input[[paste('Channel', x, sep = '')]], {
if(!input[[paste('Channel', x, sep = '')]]) {
shinyjs::disable(paste("Calculations", x, sep = ''))
updateCheckboxGroupInput(session, inputId = paste("Calculations", x, sep = ''), selected=character(0))
} else {
shinyjs::enable(paste("Calculations", x, sep = ''))
}
})
})
output$ChannelCheckboxes <- renderUI({
fluidRow(
lapply(Channels, function(x) {
column(2,
checkboxInput(inputId = paste('Channel', x, sep = ''), label = x)
)
})
)
})
output$CalculationCheckboxes <- renderUI({
fluidRow(
lapply(Channels, function(x) {
column(2,
checkboxGroupInput(inputId = paste("Calculations", x, sep = ''), label = NULL, c('sum', 'mean', 'length', 'max', 'min', 'sd')) ) })
)
})
lapply(Channels, function(x) {
observe({
req(input[[paste('Channel', x, sep = '')]])
if(input[[paste('Channel', x, sep = '')]] & !is.null(input[[paste("Calculations", x, sep = '')]])){
values$Statisticlist[[paste(x)]] <- input[[paste("Calculations", x, sep = "")]]
}
})
})
observeEvent(values$Statisticlist, { print(values$Statisticlist)
mylist <<- values$Statisticlist
})
}
shinyApp(ui, server)