skip to Main Content

I am working with multiple interactive matrixes that are consolidated into a list for organizational and Shiny-rendering purposes. Because of the number of matrixes and number of matrix columns that are being generated, the unique matrix column names become very long making the matrixes hard to view in R studio. I need to see interim matrixes that are generated by the function for testing.

How do R pro’s handle this sort of issue, truncating matrix column names for visualization purposes only while leaving the long column names intact for processing purposes? Or instead, do R pro’s create a key index of matrix names and column names, and assign matrix names like mat1, mat2, etc., and column names mat1Col1, mat1Col2, etc.? The latter seems difficult to me, it’s easier for matrix column names to be somewhat self-explanatory even if long.

The code posted below is a redaction. The matrix column names in the actual code get much longer than the names shown in this example. You can see that the matrix is embedded into a list, with a portion of the long matrix column name extracted and inserted into the list name for embedded matrix identification purposes. Running the function where the matrix is output results in the top half of the image below, and running the function where the list with embedded matrix is output results in the bottom half of the image below:

enter image description here

Code:

create_Tmp1 <- function(element, variable) {

  col_1 <- paste('Begin_testing_element', element, sep = "_")
  col_2 <- paste('Run_tests_element', element, sep = "_")
  col_3 <- paste('End_testing_element', element, sep = "_")
  
  matTmp1 <- matrix(0, nrow = 3, ncol = 3)
  colnames(matTmp1) <- c(col_1,col_2,col_3)
  
  
  # For-loop to generate running table
  for(i in seq(nrow(matTmp1))){
    if(i > 1){matTmp1[i,col_1] = matTmp1[i-1,col_3]}
    matTmp1[i,col_2] = variable
    matTmp1[i,col_3] = matTmp1[i,col_1] + matTmp1[i,col_2]
  }
  
  matTmp1_to_list <- matTmp1
  new_colnames <- sub("_element.*$", "", colnames(matTmp1))
  colnames(matTmp1_to_list) <- new_colnames
  
  listTmp1 <- list(matTmp1 = list())
  listTmp1$matTmp1[[element]] <- matTmp1_to_list
  
  return(listTmp1)
  # return(matTmp1)
}

create_Tmp1("once_upon_a_time",5)

2

Answers


  1. Perhaps you can use labels (only for data.frames as far as I know)..

    # sample data
    mymatrix <- matrix(rep(1:3, 3), ncol = 3)
    colnames(mymatrix) <- paste0("My_Long_Colname", 1:3)
    
    
    library(sjlabelled)
    library(labelled)
    # labels only wotk on vectors or data.frames, 
    #  so create a data.frame from the mar
    mydf <- as.data.frame(mymatrix)
    # set labels to each colum (here: a,b,c are used)
    mydf <- labelled::set_variable_labels(mydf, .labels = letters[1:3])
    # print with labels as colnames
    sjlabelled::label_to_colnames(mydf)
    #   a b c
    # 1 1 1 1
    # 2 2 2 2
    # 3 3 3 3
    
    # proof that the actual conames are still intact:
    mydf
    #   My_Long_Colname1 My_Long_Colname2 My_Long_Colname3
    # 1                1                1                1
    # 2                2                2                2
    # 3                3                3                3
    
    Login or Signup to reply.
  2. You can write a method for objects of class "matrix". This method can abbreviate the column names and then call the usual print.default.

    print.matrix <- function(x, ..., size = 4L) {
      cn <- colnames(x)
      if(any(nchar(cn) > size)) {
        cn <- abbreviate(cn, minlength = size, strict = TRUE, method = "both")
        colnames(x) <- cn
      }
      NextMethod()
    }
    
    m <- matrix(1:12, 3, dimnames = list(NULL, state.name[1:4]))
    
    print.default(m)
    #>      Alabama Alaska Arizona Arkansas
    #> [1,]       1      4       7       10
    #> [2,]       2      5       8       11
    #> [3,]       3      6       9       12
    
    # default is size = 4L
    print(m)
    #>      Albm Alsk Arzn Arkn
    #> [1,]    1    4    7   10
    #> [2,]    2    5    8   11
    #> [3,]    3    6    9   12
    
    # size is too small, printed names are equal,
    # original names do not change
    print(m, size = 2L)
    #>      Al Al Ar Ar
    #> [1,]  1  4  7 10
    #> [2,]  2  5  8 11
    #> [3,]  3  6  9 12
    
    m
    #>      Albm Alsk Arzn Arkn
    #> [1,]    1    4    7   10
    #> [2,]    2    5    8   11
    #> [3,]    3    6    9   12
    

    Created on 2023-11-01 with reprex v2.0.2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search