Pragyaditya Das Pragyaditya Das - 2 months ago 7
R Question

str() prints first no matter the order in R

I am writing a function that would give the dim() and str() of a given dataset.

JustfunFun <- function(.csv) {
csv <- read.csv(.csv)
dimVal <- dim(csv)
print("The dimension of the dataset is:")
strVal <- str(csv)
print("The structute of the dataset is:")
headVal <- head(csv)
return(list(dimVal, strVal, headVal))
}


Ideally, the output must have the dimension first, the structure second and then the head of dataset.

But the output is as follows:

> JustfunFun("tips.csv")
[1] "The dimension of the dataset is:"
'data.frame': 244 obs. of 8 variables:
$ obs : int 1 2 3 4 5 6 7 8 9 10 ...
$ totbill: num 17 10.3 21 23.7 24.6 ...
$ tip : num 1.01 1.66 3.5 3.31 3.61 4.71 2 3.12 1.96 3.23 ...
$ sex : Factor w/ 2 levels "F","M": 1 2 2 2 1 2 2 2 2 2 ...
$ smoker : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
$ day : Factor w/ 4 levels "Fri","Sat","Sun",..: 3 3 3 3 3 3 3 3 3 3 ...
$ time : Factor w/ 2 levels "Day","Night": 2 2 2 2 2 2 2 2 2 2 ...
$ size : int 2 3 3 2 4 4 2 4 2 2 ...
[1] "The structute of the dataset is:"
[1] "The head of the dataset is:"
[[1]]
[1] 244 8

[[2]]
NULL

[[3]]
obs totbill tip sex smoker day time size
1 1 16.99 1.01 F No Sun Night 2
2 2 10.34 1.66 M No Sun Night 3
3 3 21.01 3.50 M No Sun Night 3
4 4 23.68 3.31 M No Sun Night 2
5 5 24.59 3.61 F No Sun Night 4
6 6 25.29 4.71 M No Sun Night 4

>


How do I tackle this problem?

Answer

str, like print does not return anything. You can see the last line of utils:::str.default. The easiest way to see this is try to nest a str ( ie. str(str(mtcars)) ).

This function should print the way you want, AND store the data.

JustfunFun <- function(.csv) {
    csv <- read.csv(.csv)
    dimVal <- dim(csv)
    print("The dimension of the dataset is:")
    print(dimVal)
    print("The structute of the dataset is:")
    strVal <- utils:::capture.output(str(csv))
    print(strVal)
    print(head(csv))
    return(invisible(list(dimVal, strVal, head(csv))))
}

Example:

write.csv(mtcars, "mtcars.csv", row.names = FALSE)
a <- JustfunFun("mtcars.csv")

Result:

[1] "The dimension of the dataset is:"
[1] 32 11
[1] "The structute of the dataset is:"
[1] "'data.frame':\t32 obs. of  11 variables:"                          
[2] " $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ..."  
[3] " $ cyl : int  6 6 4 6 8 6 8 4 4 6 ..."                            
[4] " $ disp: num  160 160 108 258 360 ..."                            
[5] " $ hp  : int  110 110 93 110 175 105 245 62 95 123 ..."           
[6] " $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ..."
[7] " $ wt  : num  2.62 2.88 2.32 3.21 3.44 ..."                       
[8] " $ qsec: num  16.5 17 18.6 19.4 17 ..."                           
[9] " $ vs  : int  0 0 1 1 0 1 0 1 1 1 ..."                            
[10] " $ am  : int  1 1 1 0 0 0 0 0 0 0 ..."                            
[11] " $ gear: int  4 4 4 3 3 3 3 4 4 4 ..."                            
[12] " $ carb: int  4 4 1 1 2 1 4 2 2 4 ..."  
   mpg cyl disp  hp drat    wt  qsec vs am gear carb
1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

str(a)
 $ : int [1:2] 32 11
 $ : chr [1:12] "'data.frame':\t32 obs. of  11 variables:" " $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ..." " $ cyl : int  6 6 4 6 8 6 8 4 4 6 ..." " $ disp: num  160 160 108 258 360 ..." ...
 $ :'data.frame':   6 obs. of  11 variables:
  ..$ mpg : num [1:6] 21 21 22.8 21.4 18.7 18.1
  ..$ cyl : int [1:6] 6 6 4 6 8 6
  ..$ disp: num [1:6] 160 160 108 258 360 225
  ..$ hp  : int [1:6] 110 110 93 110 175 105
  ..$ drat: num [1:6] 3.9 3.9 3.85 3.08 3.15 2.76
  ..$ wt  : num [1:6] 2.62 2.88 2.32 3.21 3.44 ...
  ..$ qsec: num [1:6] 16.5 17 18.6 19.4 17 ...
  ..$ vs  : int [1:6] 0 0 1 1 0 1
  ..$ am  : int [1:6] 1 1 1 0 0 0
  ..$ gear: int [1:6] 4 4 4 3 3 3
  ..$ carb: int [1:6] 4 4 1 1 2 1
Comments