Question
Please provide code creating a function sizeReport() in R language that can give following outputs. Create a custom function named sizeReport() in R PROGRAMMING LANGUAGE
Please provide code creating a function sizeReport() in R language that can give following outputs.
Create a custom function named sizeReport() in R PROGRAMMING LANGUAGE which has given arguments:
path : path to the directory (string),
patt: regular expression (regex, string) that filters the reported files / directories, by default = ".*",
dironly: logical value (boolean) that toggles whether all files or only directories are reported, by default = FALSE,
level: specify recursion depth (integer), default Inf.
A typical basic function call is shown below. In this call, we use only the path argument and specify the path to the directory in a relational way (you can also use direct path to a e.g. folder). The other arguments take default values so level = Inf which results in a complete search of the directory (all items in the directory, in all subdirectories, etc. are listed), patt = ".*" so all items are listed, and dironly = FALSE so all items are listed and not just directories.
Output of sizeReport() function should be like this:
sizeReport ( path ="/ ") options(width =200 ) sizeReport(path = "/Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniow sizeReport(path="/",level=0) 1path../size13019136 The following example shows off the level restrictions. The level is restricted only to the first level. Thus, the report contains only direct elements the initial directory. The initial directory is level 0 , so it is also included. sizeReport(path="/",level=1) Finally, we include recursion for two levels. This report is identical to the first one because there are only two levels for the sizeReport(path="./",level=2) Next, we restrict the report to directories only. The recursion level is not restricted, so all subdirectories are searched. Note that there is a single subdirectory with size 0 . It means that it is an empty directory. sizeReport(path="/",dironly=TRUE) The following example shows the use of regular expressions to filter reported items on names. In this example, we restrict listed items only to thes ending with "png". sizeReport ( path ="./ ", patt = "png\$") \( \begin{array}{llrr} & & \text { path } & \text { size } \\ 9 & \text {..//projekt-2/fig_1.png } & 24526 \\ 10 & \ldots / / \text { projekt-2/fig_2.png } & 35157 \\ 11 & \text {.//projekt-2/fig_3.png } & 128020 \\ 17 & \ldots / / \text { projekt-3/fig_1.png } & 32498 \\ 18 & \text {..//projekt-3/fig_2.png } & 72864 \\ 19 & \ldots / / \text { projekt-3/fig_3.png } & 90707\end{array} \) The following example restricts listed items only to the ones containing one of the strings "fig1", "fig2", "fig3", "fig4". sizeReport(path="./",patt="fig[1-4]") [1] path size rows (or -length row. names) The following example restricts listed items only to the ones containing the string "fig" not followed by 1 through 4. \[ \text { sizeReport }(\text { path }=" \ldots / \text {, patt }=\text { "fig[^1-4]") } \] sizeReport (path ="./ ", patt = "fig[^1-4]") \( \begin{array}{llrr} & & \text { path } & \text { size } \\ 9 & \ldots / / \text { projekt-2/fig_1.png } & 24526 \\ 10 & \ldots / / \text { projekt-2/fig_2.png } & 35157 \\ 11 & \text {..//projekt-2/fig_3.png } & 128020 \\ 17 & \ldots / / \text { projekt-3/fig_1.png } & 32498 \\ 18 & \ldots / / \text { projekt-3/fig_2.png } & 72864 \\ 19 & \ldots / / \text { projekt-3/fig_3.png } & 90707\end{array} \) The function allows for mixing all options. The following example shows such usage. We list only directories on the first level of recursion containing che string "projekt" or "project". sizeReport(path="/",patt="proje[ck]t",dironly=TRUE,level=1) 345path//projekt-1//projekt-2/projekt-3size32700211669238534
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started