background preloader


Facebook Twitter

FindFn (sos package) FindFn(string, maxPages = 20) Do a search for functions, which opens an interactive HTML page of results. The results may be sorted in a variety of ways and also link to help files for each function. The results page will automatically open in whatever application has been designated to open .html files (usually a browser is the default). string – A character string to search.maxPages – Maximum number of pages to return, i.e. number of links is 20*maxPages. Example. I perform two searches below, one for stocks and one for point pattern. > > > library(sos)Loading required package: brew Attaching package: ‘sos’ The following object(s) are masked from ‘package:utils’: ?

If the HTML file opens in anything but a browser, e.g. a text editor, you’ll need to change the settings on your computer so that HTML files open in a browser to really make use of the findFn function. Find hidden code for functions. Retrieve an R Object, Including from a Namespace. Description These functions locate all objects with name matching their argument, whether visible on the search path, registered as an S3 method or in a namespace but not exported. getAnywhere() returns the objects and argsAnywhere() returns the arguments of any objects that are functions. Usage getAnywhere(x) argsAnywhere(x) Arguments Details These functions look at all loaded namespaces, whether or not they are associated with a package on the search list. They do not search literally “anywhere”: for example, local evaluation frames and namespaces that are not loaded will not be searched.

Where functions are found as registered S3 methods, an attempt is made to find which namespace registered them. Value For getAnywhere() an object of class "getAnywhere". In computing whether objects are identical, their environments are ignored. Normally the structure will be hidden by the print method. For argsAnywhere() one or more argument lists as returned by args. See Also get, getFromNamespace, args. Finding the code for a ‘hidden’ function in R. Finding the code for a ‘hidden’ function in R If you want to understand what a certain function is doing, or you want to adapt it, you can type in the function without bracket on the command line. However, sometimes functions are ‘hidden’.

You can use methods(function), which will give you the methods for that function (see ? Methods for more detailed information). Take for example the kruskal.test. function (x, ...) Next, if we type: [1] kruskal.test.default* kruskal.test.formula* Non-visible functions are asterisked Thus, there are two methods, kruskal.test.default and kruskal.test.formula. This gives the underlying code for the kruskal.test.default: I am just copying first lines of the code, that leaves something for you to find out ;-). About pvanb I am a tropical forest ecologist with a focus on spatial and temporal patterns and processes at population and ecosystem level. Get. Magic empty bracket. Print_fs: Print File Structure. We at STATWORX work a lot with R and we often use the same little helper functions within our projects. These functions ease our daily work life by reducing repetitive code parts or by creating overviews of our projects.

At first, there was no plan to make a package, but soon I realised, that it will be much easier to share and improve those functions, if they are within a package. Up till the 24th December I will present one function each day from helfRlein. So, on the 14th day of Christmas my true love gave to me… What can it do? This little helper returns the folder structure of a given path. With this, one can for example add a nice overview to the documentation of a project or within a git.

How to use it? If we take a look at the same example we used for the get_network function on day 5, we get the following: print_fs("~/flowchart/", depth = 4) With depth we can adjust how deep we want to traverse through our folders. Overview Have a merry advent season! Über den Autor Jakob Gepp. Symmetric set differences in R. My .Rprofile contains a collection of convenience functions and function abbreviations.

These are either functions I use dozens of times a day and prefer not to type in full: ## my abbreviation of head() h <- function(x, n=10) head(x, n) ## and summary() ss <- summary Or problems that I'd rather figure out once, and only once: ## example: ## between( 1:10, 5.5, 6.5 ) between <- function(x, low, high, ineq=F) { ## like SQL between, return logical index if (ineq) { x >= low & x <= high } else { x > low & x < high } } One of these "problems" that's been rattling around in my head is the fact that setdiff(x, y) is asymmetric, and has no options to modify this.

With some regularity, I want to know if two sets are equal, and if not, what are the differing elements. setequal(x, y) gives me a boolean answer to the first question. > setdiff(1:5, 1:6) integer(0) symdiff <- function( x, y) { setdiff( union(x, y), intersect(x, y))} > symdiff(1:5, 1:6) == symdiff(1:6, 1:5) [1] TRUE Tada! Boolean operators && and || Key R Operators. Only recently have I discovered the true power of some the operators in R. Here are some tips on some underused operators in R: The %in% operator This funny looking operator is very handy. It’s short for testing if several values appear in an object. For instance x = c(2, 6, 4, 4, 6, 8, 10, 14, 2) To grab all the values where x is 2, 4 or 14 we could do x[x == 2 | x == 4 | x == 14] ## [1] 2 4 4 14 2 or we could use %in% … x[x %in% c(2, 4, 14)] ## [1] 2 4 4 14 2 This is something I use all the time for filtering data.

Library("dplyr") library("sf") library("sp") data(world, package = "spData") # drop the geometry column because we don't need it world = world %>% st_drop_geometry() Your colleague sends you a list of 50 countries (I’m going to randomly sample the names from the data) and says that they want the average life expectency for each continent group within these 50 countries. Did you know? You can make your own %% operators! %add% = function(a, b) a + b 2 %add% 3 ## [1] 5 x = c(2, 4, 6, 8) Skimr for useful and tidy summary statistics. Like every R user who uses summary statistics (so, everyone), our team has to rely on some combination of summary functions beyond summary() and str(). But we found them all lacking in some way because they can be generic, they don't always provide easy-to-operate-on data structures, and they are not pipeable. What we wanted was a frictionless approach for quickly skimming useful and tidy summary statistics as part of a pipeline.

And so at rOpenSci #unconf17, we developed skimr. In a nutshell, skimr will create a skim_df object that can be further operated upon or that provides a human-readable printout in the console. It presents reasonable default summary statistics for numerics, factors, etc, and lists counts, and missing and unique values. Backstory The idea for skimr as a project for the #unconf17 was proposed by Amelia McNamara following discussions on Twitter and an initial package by Hadley Wickham.

Here's what we liked and disliked, in Amelia's words: Introducing skimr. Bricks not monoliths. Chapter 32 of Tao Te Programming advises you to make bricks instead of monoliths. Here is an example. The example is written with the syntax of R and is a data analysis, but the principle is valid no matter what language you use or what your task is. Monolith Here is an outline of a function reminiscent of many novice attempts: monolith <- function (data, col="blue", pch=21) { # transform data # fit model to data # plot data, uses 'col' and 'pch' # get desired model results # return desired model results } Each of these comment lines may be many lines of code so that the whole function runs to pages.

On the positive side, this is a step towards abstraction from merely having a script of commands and changing values by hand within the script. Brickwall It is not that I’m against having one function that does everything. Brickwall <- function (data, ...) { tranData <- myDataTransform(data) dataMod <- myModelFit(tranData) myPlot(dataMod, ...) myModelOutput(dataMod) # this value is returned } 3 dots construct in R. There is a mechanism that allows variability in the arguments given to R functions. Technically it is ellipsis, but more commonly called “…”, dots, dot-dot-dot or three-dots. Basics The three-dots allows: an arbitrary number and variety of argumentspassing arguments on to other functions Arbitrary arguments The two prime cases are the c and list functions: > c function (..., recursive = FALSE) .Primitive("c") > list function (...) .Primitive("list") Both of these allow you to give them as many arguments as you like, and you can name those arguments (which end up as names in the resulting object).

> c(a=42, 73.9, c=NA) a c 42.0 73.9 NA > names(.Last.value) [1] "a" "" "c" Passing arguments on An example is the apply function. > args(apply) function (X, MARGIN, FUN, ...) This has the three-dots as an argument. MyMat <- array(rnorm(15), c(5,3)) myMat[5] <- NA # treated as a vector not a matrix We can apply mean to this matrix with and without the additional argument that says to ignore missing values: Ellipsis. Lexical scope and function closures in R. Introduction R is different to many “easy to use” statistical software packages – it expects to be given commands at the R command prompt.

This can be intimidating for new users, but is at the heart of its power. Most powerful software tools have an underlying scripting language. This is because scriptable tools are typically more flexible, and easier to automate, script, program, etc. In fact, even software packages like Excel or Minitab have a macro programming language behind the scenes available for “power users” to exploit. Programming from the ground up It is natural to want to automate (repetitive) tasks on a computer, to automate a “work flow”. Next, one can add in simple control structures, to support looping, branching and conditional execution. Although scripting is a simple form of programming, it isn’t “real” programming, or software engineering. Functions and procedures Variable scope Dynamic scope Lexical scope Function closures Function closures for scientific computing. Tidysearch. Geocoding function. This is a very simple function to perform geocoding using the Google Maps API: Essentially, users need to get an API key from google and then use as an input (string) for the function.

The function itself is very simple, and it is an adaptation of some code I found on-line (unfortunately I did not write down where I found the original version so I do not have a way to reference the source, sorry!!). GeoCodes <- getGeoCode(gcStr="11 via del piano, empoli", key) To use the function we simply need to include an address, and it will return its coordinates in WGS84. It can be used in a mutate call within dplyr and it is reasonably fast. The repository is here: Checkdir. We at STATWORX work a lot with R and we often use the same little helper functions within our projects. These functions ease our daily work life by reducing repetitive code parts or by creating overviews of our projects. At first, there was no plan to make a package, but soon I realised, that it will be much easier to share and improve those functions, if they are within a package.

Up till the 24th December I will present one function each day from helfRlein. So, on the 1st day of Christmas my true love gave to me… What can it do? This little helper checks a given folder path for existence and creates it if needed. checkdir(path = "testfolder/subfolder") Internally, there is just a simple if statement which combines the base R functions file.exists() and dir.create(). Overview To see all the other functions you can either check out our GitHub or you can read about them here. Have a merry advent season! Über den Autor Jakob Gepp Sign Up Now! Don't forget the "utils" package in R. With thousands of powerful packages, it’s easy to glaze over the libraries that come preinstalled with R. Thus, this post will talk about some of the cool functions in the utils package, which comes with a standard installation of R.

While utils comes with several familiar functions, like read.csv, write.csv, and help, it also contains over 200 other functions. readClipboard and writeClipboard One of my favorite duo of functions from utils is readCLipboard and writeClipboard. For example, let’s copy a column of cells from Excel. We can now run readClipboard() in R. Similarly, if we want to write a vector of elements to the clipboard, we can the writeClipboard command: Now, the vector test has been copied to the clipboard. Combn The combn function is useful for getting the possible combinations of an input vector. In general, the first parameter of combn is the vector of elements you want to get possible combinations from.

FileSnapshot size ==> size of fileisdir ==> is file a directory? Helpers for 'OOP' in R • sloop. Primitive Functions List. Trace() Modify package function. Find Index of an Element in a Vector. Example 1: Find Index Value of R Vector Element Using match() # create two strings vowel_letters <- c("a", "e", "i", "o", "u") # find index value of "i"match("i", vowel_letters) # 3 # find index value of "u"match("u", vowel_letters) # 5 Output In the above example, we have used the match() function to find the index of an element in the vector named . Here, "i" is present in at the 3rd index, so the method returns 3 "u" is present in at the 5th index, so the method returns 5 Example 2: Find Index Value of R Vector Element Using which() # create two strings languages <- c("R", "Swift", "Java", "Python") # find index value of "Swift" using which()which(languages == "Swift") # 2 # find index value of "Python" using which()which(languages == "Python") # 4 Here, we have used the which() function to find the index value of an element.

Since, "Swift" is present in at the 2nd index, so the method returns 2 "Python" is present in at the 4th index, so the method returns 4. Round_preserve_sum: Round values while preserve their rounded sum. After an embarrassing teleconference in which I presented a series of percentages that did not sum to 100 (as they should have), I found some R code on to help me to avoid this in the future. In general, the sum of rounded numbers (e.g., using the base::round function) is not the same as their rounded sum.

For example: > sum(c(0.333, 0.333, 0.334)) [1] 1 > sum(round(c(0.333, 0.333, 0.334), 2)) [1] 0.99 The stackoverflow solution applies the following algorithm Round down to the specified number of decimal placesOrder numbers by their remainder valuesIncrement the specified decimal place of values with 'k' largest remainders, where 'k' is the number of values that must be incremented to preserve their rounded sum Here's the corresponding R function: round_preserve_sum <- function(x, digits = 0) { up <- 10 ^ digits x <- x * up y <- floor(x) indices <- tail(order(x-y), round(sum(x)) - sum(y)) y[indices] <- y[indices] + 1 y / up } Continuing with the example: Practicing static typing in R: Prime directive on trusting our functions with object oriented programming. Debugging with RStudio. Debugging in R: How to Easily and Efficiently Conquer Errors in Your Code.

General suggestions for debugging R. How to write and debug an R function. Tracking down errors in R. Catching errors in R and trying something else. Translating Weird R Errors. Easy data validation with the validate package | Mark van der Loo. R: Bug Reporting in R. Debugging techniques in RStudio. 4 ways to not just read the code, but delve into it step-by-step. Handling R errors the rlang way. Example of unit testing R code with testthat. Mocking is catching.