The Big R-Book. Philippe J. S. De Brouwer

Чтение книги онлайн.

Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 39

The Big R-Book - Philippe J. S. De Brouwer

Скачать книгу

target="_blank" rel="nofollow" href="#fb3_img_img_4f161572-7b14-5d26-8ba1-1939e23b0c05.jpg" alt="image"/> Hint

      The edit() function uses the vi editor when using the CLI on Linux. This editor is not so popular any more and you might not immediately know how to close it. To get out of it: press [esc], then type :q and press [enter].

       vi

      4.6.5 Creating Function with Default Arguments

      Assigning a default value to the argument of a function means that this argument will get the default value, unless another value is supplied – in other words: if nothing is supplied then the default is used.

      It is quite handy to have the possibility to assign a default value to a function. It allows to save a lot of typing work and makes code more readable, but it allows also to add a variable to an existing function and make it compatible with all previous code where that argument was not defined.

       paste()

      Example

      The function paste() collates the arguments provided and returns one string that is a concatenation of all strings supplied, separated by a separator. This separator is supplied in the function via the argument sep. What is the default separator used in paste()?

      Creating functions with a default value

      Example: default value for function

      c_surface <- function(radius = 2) { radius 2 * pi } c_surface(1) ## [1] 3.141593 c_surface() ## [1] 12.56637

      Unlike environments like spreadsheets, R is more like a programming language that is extremely flexible, modular, and customizable.

      4.7.1 Discovering Packages in R

      Additional functions come in “packages.” To use them one needs to install the package first with the function install.packages(); this will connect to a server, download the functions and prepare them for use. Once installed on our computer, they can be loaded in the active environment with the function library() or require()

       install.packages()

       library()

       require()

      Example: loading the package DiagrammeR

      # Download the package (only once): install.packages(‘DiagrammeR’) # Load it before we can use it (once per session): library(DiagrammeR)

      The number of packages availabe increases fast. At the time of writing there are about 15 thousand packages available (see the next “Further information” section). We can of course not explain each package in just one book. Below we provide a small selection as illustration and in the rest of the book we will use a selection of 60 packages (which contain a few hundred upstream packages). The choice of packages is rather opinionated and personal. R is free software and there are always many ways to achieve the same result.

      image Further information – Packages

      More information about the packages as well as the packages themselves can be found on the CRAN server https://cran.r-project.org/web/packages.

      Useful functions for packages

      Below we show some useful functions - note that the output is suppressed.

      # See the path where libraries are stored: .libPaths() # See the list of installed packages: library() # See the list of currently loaded packages: search()

      image Further information – All available packages

      # available.packages() gets a list: pkgs <- available.packages(filters = “duplicates”) colnames(pkgs) ## [1] “Package” “Version” ## [3] “Priority” “Depends” ## [5] “Imports” “LinkingTo” ## [7] “Suggests” “Enhances” ## [9] “License” “License_is_FOSS” ## [11] “License_restricts_use” “OS_type” ## [13] “Archs” “MD5sum” ## [15] “NeedsCompilation” “File” ## [17] “Repository” # We don't need all, just keep the name: pkgs <- pkgs[,‘Package’] # Show the results: print(paste(‘Today, there are’, length(pkgs), ‘packages for R.’)) ## [1] “Today, there are 15477 packages for R.” available.packages()

      image Further information – All installed packages

      We can use the function library() to get a list of all packages that are installed on our machine.

      # Get the list (only names): my_pkgs <- library()$results[,1] ## Warning in library(): library ‘/usr/local/lib/R/site-library’ contains no packages # Show the results: print(paste(‘I have’, length(my_pkgs), ‘packages for R.’)) ## [1] “I have 282 packages for R.”

      Alternatively, you can use the function installed.packages()

       library()

       installed.packages()

      4.7.2 Managing Packages in R

      In the previous section, we learned how to install a package, and got a flavour of the available packages. It is also a good idea to keep the repository of packages stable during a big project, but from time to time update packages aswell as R.Not only there are bug fixes, but also new features.

      # See all installed packages: installed.packages()

Скачать книгу