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:
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
Perhaps you can use labels (only for data.frames as far as I know)..
You can write a method for objects of class
"matrix"
. This method can abbreviate the column names and then call the usualprint.default
.Created on 2023-11-01 with reprex v2.0.2