Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you write that's project Accordin to R. PROGRAM The task is to code a function sizeReport(). The function should accept the following arguments: path:

Could you write that's project Accordin to R. PROGRAM

The task is to code a function sizeReport(). The function should accept the following arguments:

path: a path to a directory, both absolute and relative, should be accepted (string);

patt: a regular expression for filtering items in the resulting report (string) with the default value of .*;

dironly: a switch for including only directories in the final report (logical) with the default value of FALSE;

level: an integer deciding the level of recursion (integer) with the default value of Inf.

The functions task is to take a path to a directory and create a report (as a data frame) of subelements sizes in bytes. The final report should contain information on files and subdirectories. The size of a directory is meant as a sum of the sizes of all its elements. The resulting report, as a data frame, should contain two columns. The first one must contain paths to all elements, and the second column must contain the sizes. The following description contains several examples that should be included in your script.

A typical call to the function is shown below. In the call, only the argument path is used. A relative path is passed to the function. The other arguments are used with the default values. In particular level = Inf resulting in listing all subdirectories, patt = .* resulting in no filtering on names of items, and finally, dironly = FALSE resulting in listing all items, not only directories.

 path size 1 ../ 13019136 2 ..//cars.csv 12536233 3 ..//projekt-1 32700 6 ..//projekt-1/project.html 24713 7 ..//projekt-1/project.org 7987 8 ..//projekt-1/test 0 4 ..//projekt-2 211669 9 ..//projekt-2/fig_1.png 24526 10 ..//projekt-2/fig_2.png 35157 11 ..//projekt-2/fig_3.png 128020 12 ..//projekt-2/maze_0.map 84 13 ..//projekt-2/maze_1.map 576 14 ..//projekt-2/project.html 13719 15 ..//projekt-2/project.org 9587 5 ..//projekt-3 238534 16 ..//projekt-3/analiza.nb 1780 17 ..//projekt-3/fig_1.png 32498 18 ..//projekt-3/fig_2.png 72864 19 ..//projekt-3/fig_3.png 90707 20 ..//projekt-3/project.html 14314 21 ..//projekt-3/project.html~ 14313 22 ..//projekt-3/project.org 6648 23 ..//projekt-3/project.org~ 5410

The following example shows different level restrictions. The level is restricted only to the zero level. Thus, the report contains only the initial directory.

sizeReport(path = "../", level = 0)

The following example only differs from the previous one in that the absolute path is given. Note that the width option is changed to accommodate the width of the report. In all the following examples, only the relative paths are used for clarity purposes.

 path size 1 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe 483684 2 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-1 33789 5 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-1/project.html 25164 6 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-1/project.org 8625 7 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-1/test 0 3 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-2 211361 8 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-2/fig_1.png 24526 9 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-2/fig_2.png 35157 10 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-2/fig_3.png 128020 11 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-2/maze_0.map 84 12 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-2/maze_1.map 576 13 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-2/project.html 13641 14 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-2/project.org 9357 4 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3 238534 15 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3/analiza.nb 1780 16 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3/fig_1.png 32498 17 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3/fig_2.png 72864 18 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3/fig_3.png 90707 19 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3/project.html 14314 20 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3/project.html~ 14313 21 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3/project.org 6648 22 /Users/michael/Documents/sync/lectures/sgh/2-programowanie-w-R-podstawy/2022-11-09-zadania-zaliczeniowe/projekt-3/project.org~ 5410

The following example shows off the level restrictions. The level is restricted only to the first level. Thus, the report contains only direct elements of the initial directory. The initial directory is level 00, so it is also included.

sizeReport(path = "../", level = 1)
 path size 1 ../ 13019136 2 ..//cars.csv 12536233 3 ..//projekt-1 32700 4 ..//projekt-2 211669 5 ..//projekt-3 238534

Finally, we include recursion for two levels. This report is identical to the first one because there are only two levels for the initial directory.

sizeReport(path = "../", level = 2)

 path size 1 ../ 13019136 2 ..//cars.csv 12536233 3 ..//projekt-1 32700 6 ..//projekt-1/project.html 24713 7 ..//projekt-1/project.org 7987 8 ..//projekt-1/test 0 4 ..//projekt-2 211669 9 ..//projekt-2/fig_1.png 24526 10 ..//projekt-2/fig_2.png 35157 11 ..//projekt-2/fig_3.png 128020 12 ..//projekt-2/maze_0.map 84 13 ..//projekt-2/maze_1.map 576 14 ..//projekt-2/project.html 13719 15 ..//projekt-2/project.org 9587 5 ..//projekt-3 238534 16 ..//projekt-3/analiza.nb 1780 17 ..//projekt-3/fig_1.png 32498 18 ..//projekt-3/fig_2.png 72864 19 ..//projekt-3/fig_3.png 90707 20 ..//projekt-3/project.html 14314 21 ..//projekt-3/project.html~ 14313 22 ..//projekt-3/project.org 6648 23 ..//projekt-3/project.org~ 5410

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 00. It means that it is an empty directory.

sizeReport(path = "../", dironly = TRUE) 
 path size 1 ../ 13019136 3 ..//projekt-1 32700 8 ..//projekt-1/test 0 4 ..//projekt-2 211669 5 ..//projekt-3 238534

The following example shows the use of regular expressions to filter reported items on names. In this example, we restrict listed items only to these ending with "png".

sizeReport(path = "../", patt = "png$") 
 path size 9 ..//projekt-2/fig_1.png 24526 10 ..//projekt-2/fig_2.png 35157 11 ..//projekt-2/fig_3.png 128020 17 ..//projekt-3/fig_1.png 32498 18 ..//projekt-3/fig_2.png 72864 19 ..//projekt-3/fig_3.png 90707 

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 <0 rows> (or 0-length row.names) 

The following example restricts listed items only to the ones containing the string "fig" not followed by 1 through 4.

sizeReport(path = "../", patt = "fig[^1-4]") 
 path size 9 ..//projekt-2/fig_1.png 24526 10 ..//projekt-2/fig_2.png 35157 11 ..//projekt-2/fig_3.png 128020 17 ..//projekt-3/fig_1.png 32498 18 ..//projekt-3/fig_2.png 72864 19 ..//projekt-3/fig_3.png 90707 

The function allows for mixing all options. The following example shows such usage. We list only directories on the first level of recursion containing the string "projekt" or "project".

sizeReport(path = "../", patt = "proje[ck]t", dironly = TRUE, level = 1) 
 path size 3 ..//projekt-1 32700 4 ..//projekt-2 211669 5 ..//projekt-3 238534

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

Students also viewed these Databases questions