Commissar Vasili Karlovic Commissar Vasili Karlovic - 2 months ago 9
R Question

cbind without a predetermined number of tables

I'm trying to create my first shiny app but I'm stuck on the following:


  1. The user will be able upload his table (so I don't know how many
    rows/columns the table will have)

  2. The app will use a function (modwt from 'wavelets' package) to
    calculate the coefficients

  3. The output will be a table that combines all the sets of
    coefficients.



Here's an example of how I can combine the coefficients of my test matrix:

output$mod <- renderTable({
if(is.null(data())){return ()}
file1 <- input$file
tbl <- read.csv(file = file1$datapath, header = input$header, stringsAsFactors = input$stringAsFactors)
wave <- modwt(tbl)
cbind(wave@W$W1, wave@W$W2, wave@W$W3)
})


In this case I have 3 tables of coefficients to combine (wave@W$W1, wave@W$W2, wave@W$W3) but it's not prefixed. Each coefficient table has the same number of rows and columns as the imported matrix.

How can I cbind i tables and name the columns of that table as "W1Var1", "W1Var2", ... , "W(i)Var(i)"?

Thank you in advance

Answer

I tried it out on this abalone data I had kept from some older project -

data <- read.csv("abalone.csv",stringsAsFactors = F)
data <- data[,sapply(data,is.numeric)]
data <- data[,1:3]
head(data)

#   Length Diameter Height
# 1  0.455    0.365  0.095
# 2  0.350    0.265  0.090
# 3  0.530    0.420  0.135
# 4  0.440    0.365  0.125
# 5  0.330    0.255  0.080
# 6  0.425    0.300  0.095

wave <- wavelets::modwt(data)

name_list <- expand.grid(W=names(wave@W),N=names(data))
name_list <- name_list[order(name_list$W),]
name_list <- unlist(apply(name_list,1,paste0,collapse=""))

output <- data.frame(do.call(cbind,wave@W))
names(output) <- name_list

head(output)

#       W1Length   W1Diameter    W1Height     W2Length   W2Diameter     W2Height
#1  0.0007141775 -0.015057604 -0.02741532  0.026445608  0.021973215  0.003796411
#2 -0.0178623259 -0.003055652  0.03147525  0.027294618  0.024811542  0.006805190
#3 -0.0042752443 -0.007179318 -0.02473044  0.003903755  0.000864748  0.007034940
#4  0.0969934351  0.074977230  0.03870881 -0.008999976 -0.011581076  0.003073201
#5 -0.0716497977 -0.048780057 -0.03227399 -0.006560299 -0.004023690 -0.004659585
#6 -0.0724520611 -0.068430864 -0.01265374 -0.009740781 -0.001975610 -0.006545703
#     W3Length  W3Diameter     W3Height     W4Length   W4Diameter      W4Height
#1 -0.08200325 -0.06957770 -0.022362161 -0.038224750 -0.032841068 -0.0163395399
#2 -0.01915867 -0.01572532 -0.004885967 -0.025496629 -0.024064880 -0.0127296303
#3  0.01767815  0.01757878  0.005162413 -0.009600510 -0.012067035 -0.0078526760
#4  0.03341336  0.03186063  0.009724175  0.006501488  0.000536822 -0.0028651762
#5  0.04755021  0.04179055  0.012794711  0.015687389  0.007644734  0.0002224431
#6  0.05795348  0.05000976  0.017513040  0.018876198  0.010426718  0.0014205476
#     W5Length  W5Diameter    W5Height    W6Length  W6Diameter     W6Height
#1 -0.07793182 -0.06638936 -0.02478733 -0.02199261 -0.01463714 -0.006825037
#2 -0.07783037 -0.06564205 -0.02440148 -0.02237454 -0.01516823 -0.006834632
#3 -0.07644738 -0.06354849 -0.02359830 -0.02247092 -0.01553270 -0.006772609
#4 -0.07385479 -0.06036354 -0.02243425 -0.02251433 -0.01591498 -0.006711645
#5 -0.06963612 -0.05580444 -0.02080034 -0.02253258 -0.01630504 -0.006589568
#6 -0.06407673 -0.05022518 -0.01878663 -0.02245154 -0.01659428 -0.006373257
#     W7Length  W7Diameter     W7Height    W8Length  W8Diameter    W8Height
#1 -0.01212583 -0.01158442 -0.001322562 0.003770865 0.004843111 0.004493407
#2 -0.01239135 -0.01177415 -0.001616514 0.003355641 0.004424366 0.004360637
#3 -0.01262050 -0.01192868 -0.001900928 0.002925821 0.003993220 0.004221592
#4 -0.01281899 -0.01205563 -0.002175976 0.002485530 0.003552588 0.004079251
#5 -0.01300265 -0.01216254 -0.002452028 0.002037058 0.003105960 0.003936552
#6 -0.01319736 -0.01227838 -0.002733520 0.001579256 0.002651809 0.003790449
#     W9Length  W9Diameter     W9Height
#1 -0.01668559 -0.01202336 -0.002490659
#2 -0.01669854 -0.01206783 -0.002526503
#3 -0.01670896 -0.01210992 -0.002562135
#4 -0.01671698 -0.01214994 -0.002597408
#5 -0.01672173 -0.01218722 -0.002631925
#6 -0.01672423 -0.01222268 -0.002666136