Thursday 27 July 2017

GREAT Gene Ontology (GO) Source Code

GREAT Source Code

Build Software Forum

How to Build Software

Wednesday 26 July 2017

github push passwordless

1. Generate an SSH key and associate the SSH key with the remote repository

2. Set your remote URL to a form that supports SSH

If you have done the steps above and are still getting the password prompt, make sure your repo URL is in the form

git+ssh://git@github.com/username/reponame.git

as opposed to

https://github.com/username/reponame.git

To see your repo URL, run:

git remote show origin

You can change the URL with:

git remote set-url origin git+ssh://git@github.com/username/reponame.git

Reference
https://stackoverflow.com/questions/8588768/git-push-username-password-how-to-avoid

Thursday 20 July 2017

Functions are first class objects in R

Cited from The R Language

Functions in R are “first class objects”, which means that they can be treated much like any other R object. Importantly,
  • Functions can be passed as arguments to other functions.
  • Functions can be nested, so that you can define a function inside of another function.

R parlist, primitive functions, expression

Reading Notes of Metaprogramming in R

Cited from the book "Metaprogramming in R"

Metaprogramming is when you write programs that manipulate other programs; in other words, you treat code as data that you can generate, analyze, or modify.

R is a very high-level language where all operations are functions, and all functions are data that you can manipulate.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chapter 1 Anatomy of a function

There are three parts to a function: its formal parameters, its body, and the environment it is defined in. The functions formals(), body(), and environment() give you these.

formals() returns a list where element names are the parameter names and values are default parameters.

You cannot use the missing function to check for a missing value in a formals function (that function is useful only inside a function call, and in any case there is a difference between a missing parameter and one that doesn’t have a default value), but you can always check whether the value is the empty symbol.

The function body is an expression.
  • When a function is called, R sets up an environment for it to evaluate this expression in; this environment is called the evaluation environment for the function call.
  • The evaluation environment is first populated with values for the function’s formal parameters, either provided in the function call or given as default parameters, and then the body executes inside this environment.
  • Assignments will modify this local environment unless you use the <<- operator, and the result of the function is the last expression evaluated in the body. This is either the last expression in a sequence or an expression explicitly given to the return function.
eval(body(f), list(x = 2))

The eval function evaluates an expression and uses the second argument to look up parameters. You can give it an environment, and the expression will then be evaluated in it, or you can use a list.

When a function, f, is created, it gets associated with environment(f). This environment is the environment where f is defined. When f is invoked, R creates an evaluation environment for f; let’s call it evalenv. The parent of evalenv is set to environment(f). Since environment(f) is the environment where f is defined, having it as the parent of the evaluation environment means that the body of f can see its enclosing scope if f is a closure.

After the evaluation environment is created, the formals of f are added to it as promises. Default parameters will be promises that should be evaluated in the evaluation scope, evalenv. This means they can refer to other local variables or formal parameters. Since these will be put in evalenv and since evalenv’s parent is environment(f), these promises can also refer to variables in the scope where f was defined. Expressions given to f where it is called, however, will be stored as promises that should be called in the calling environment. Let’s call that callenv.

To set up the promises, the delayedAssign function takes two environments as arguments. The first is the environment where the promise should be evaluated, and the second is where it should be stored.

List doesn’t like empty values.

alist() function creates a pair-list, which is a data structure used internally in R for formal arguments. It is the only thing this data structure is used for, but if you start hacking around with modifying parameters of a function, it is the one to use.

Using alist, expressions are also automatically quoted.

Constructing Functions
as.function() takes an alist as input and interprets the last element in it as the new function’s body and the rest as the formal arguments.

If you give as.function a list, it interprets that as just an expression that then becomes the body of the new function.

The environment of the new function is by default the environment in which you call as.function. So to make a closure, you can just call as.function inside another function.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chapter 2 Inside a Function Call

Getting the Components of the Current Function
Functions do not have names as such; you give functions names when you assign them to variables, but that is a property of the environment where you have the name, not of the function itself.

To get hold of the current function, you can use the function sys.function. This function gives you the definition of the current function, which is what you need, not its name.

When you call any of formals, body, or environment, you don’t use a function name as the first parameter; you give each of them a reference to a function, and they get the function definition from that.

Accessing Actual Function Parameters
There is a difference between a function definition, a description of what a function should do when it is called, and a function instantiation (the actual running code). One such difference is the evaluating environment. Another is that a function instantiation has actual parameters, while a function definition has only formal parameters. The latter are part of the function definition; the former are provided by the caller of the function.

If you actually want the arguments passed to the current function in the form of the promises they are really represented as, you need to get hold of them without evaluating them. substitute() substitutes into an expression the values that variables refer to. This means that variables are replaced by the verbatim expressions; the expressions are not evaluated before they are substituted into an expression.

The substituted expression is not evaluated.

If you set up default parameters that depend on others, you just get them substituted with variable names; you do not get the value assigned to other variables.

You can call substitute with an expression instead of a single variable.

A common use for substitute is to get the expression provided to a function as a string.

deparse() takes an expression and translates it into its text representation.

The actual type of object returned by substitute depends on the expression you give the function and what the expression’s variables refer to. If the expression, after variables have been substituted, is a simple type, that is what substitute returns.

Inside a function if you give substitute a local variable you have assigned to, you also get a value back. This is not because substitute does anything special here. Local variables like these are not promises; you evaluated an expression when you assigned to one.

If we call substitute in the global environment, it considers variables as names and does not substitute them for their values.

A call object refers to an unevaluated function call.

You can translate a call into a list to get its components, and you can evaluate it to invoke the actual function call.

Unlike substitute inside a function, however, the arguments to call are evaluated when the call object is constructed. These are not lazy-evaluated.


You can also create call objects manually using the call function. The first argument to call is the name of the function to call, and any additional arguments are passed on to this function when the call object is evaluated.

From inside a function, you can get the call used to invoke it using the match.call function.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Accessing the Calling Scope 

If you want direct access to the calling environment, inside a function, you can get hold of it using the function parent.frame().

To keep an expression unevaluated, using expression().



Wednesday 19 July 2017

A Neural Algorithm of Artistic Style

A Neural Algorithm of Artistic Style

Reading Notes of R Packages

Cited from the Book "R Packages"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Package Structure

devtools::install() is effectively a wrapper for R CMD INSTALL.

devtools::build() is a wrapper for R CMD build that turns source packages into bundles.

devtools::install_github() downloads a source package from GitHub, runs build() to make vignettes, and then uses R CMD INSTALL to do the install. 

devtools::install_url(), devtools::install_gitorious(), devtools::install_bitbucket() work similarly for packages found elsewhere on the internet.

You can prevent files in the package bundle from being included in the installed package using .Rinstignore.

devtools::load_all() and RStudio’s “Build and reload”, which allows you to skip install and load a source package directly into memory.

A library is simply a directory containing installed packages. You can have multiple libraries on your computer. In fact, almost every one has at least two: one for packages you’ve installed, and one for the packages that come with every R installation (like base, stats, etc). Normally, the directories with user-installed packages vary based on the version of R that you’re using. That’s why it seems like you lose all of your packages when you reinstall R — they’re still on your hard drive, but R can’t find them.

The main difference between library() and require() is what happens if a package isn’t found. While library()throws an error, require() prints a warning message and returns FALSE.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
R code

The first principle of using a package is that all R code goes in R/.



Paper: Maternal H3K27me3 controls DNA methylation-independent imprinting

Maternal H3K27me3 controls DNA methylation-independent imprinting

Thursday 13 July 2017

R Cheetsheets

Cheatsheets

Reading Notes of R for Data Science

Cited from the book "R for Data Science"

To set an aesthetic manually, set the aesthetic by name as an argument of your geom function; i.e. it goes outside of aes().

To facet your plot by a single variable, use facet_wrap(). The first argument of facet_wrap()should be a formula, which you create with ~ followed by a variable name (here “formula” is the name of a data structure in R, not a synonym for “equation”). The variable that you pass to facet_wrap()should be discrete.

To facet your plot on the combination of two variables, add facet_grid() to your plot call. The first argument of facet_grid() is also a formula. This time the formula should contain two variable names separated by a ~.

The algorithm used to calculate new values for a graph is called a stat, short for statistical transformation.

To find the variables computed by the stat, look for the help section titled “computed variables”.

ggplot(data = <DATA>)
+
 <GEOM_FUNCTION>( mapping = aes(<MAPPINGS>), stat = <STAT>, position = <POSITION> ) + <COORDINATE_FUNCTION>
+ <FACET_FUNCTION>
  • Mutating joins, which add new variables to one data frame from matching observations in another.
  • Filtering joins, which filter observations from one data frame based on whether or not they match an observation in the other table.
  • Set operations, which treat observations as if they were set elements.
The variables used to connect each pair of tables are called keys.
  • A primary key uniquely identifies an observation in its own table.
  • A foreign key uniquely identifies an observation in another table.
A variable can be both a primary key and a foreign key.

An inner join matches pairs of observations whenever their keys are equal.

The most important property of an inner join is that unmatched rows are not included in the result.

An inner join keeps observations that appear in both tables. An outer join keeps observations that appear in at least one of the tables. There are three types of outer joins:
  • A left join keeps all observations in x.
  • A right join keeps all observations in y.
  • A full join keeps all observations in x and y.
Beware that the printed representation of a string is not the same as string itself, because the printed representation shows the escapes. To see the raw contents of the string, use writeLines().





R ggplot2 Book Layered Grammer

Layered Grammer

Create R Packages and Using Devtools

Writing an R package from scratch

Step by Step Tutorial to creating R Packages

Making Packages in R Using Devtools

Pandoc: a Universal Document Converter

Pandoc

Bookdown

Bookdown

R structure Function to Create a New Object

Cited from the book "R for Data Science"

> dput(mtcars)
structure(list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3,
24.4, 22.8, 19.2, 17.8, 16.4, 17.3, 15.2, 10.4, 10.4, 14.7, 32.4,
30.4, 33.9, 21.5, 15.5, 15.2, 13.3, 19.2, 27.3, 26, 30.4, 15.8,
19.7, 15, 21.4), cyl = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8,
8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 6, 8, 4),
    disp = c(160, 160, 108, 258, 360, 225, 360, 146.7, 140.8,
    167.6, 167.6, 275.8, 275.8, 275.8, 472, 460, 440, 78.7, 75.7,
    71.1, 120.1, 318, 304, 350, 400, 79, 120.3, 95.1, 351, 145,
    301, 121), hp = c(110, 110, 93, 110, 175, 105, 245, 62, 95,
    123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, 97, 150,
    150, 245, 175, 66, 91, 113, 264, 175, 335, 109), drat = c(3.9,
    3.9, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,
    3.07, 3.07, 3.07, 2.93, 3, 3.23, 4.08, 4.93, 4.22, 3.7, 2.76,
    3.15, 3.73, 3.08, 4.08, 4.43, 3.77, 4.22, 3.62, 3.54, 4.11
    ), wt = c(2.62, 2.875, 2.32, 3.215, 3.44, 3.46, 3.57, 3.19,
    3.15, 3.44, 3.44, 4.07, 3.73, 3.78, 5.25, 5.424, 5.345, 2.2,
    1.615, 1.835, 2.465, 3.52, 3.435, 3.84, 3.845, 1.935, 2.14,
    1.513, 3.17, 2.77, 3.57, 2.78), qsec = c(16.46, 17.02, 18.61,
    19.44, 17.02, 20.22, 15.84, 20, 22.9, 18.3, 18.9, 17.4, 17.6,
    18, 17.98, 17.82, 17.42, 19.47, 18.52, 19.9, 20.01, 16.87,
    17.3, 15.41, 17.05, 18.9, 16.7, 16.9, 14.5, 15.5, 14.6, 18.6
    ), vs = c(0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1), am = c(1,
    1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
    0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1), gear = c(4, 4, 4, 3,
    3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3, 3,
    3, 3, 4, 5, 5, 5, 5, 5, 4), carb = c(4, 4, 1, 1, 2, 1, 4,
    2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2, 2, 4, 2, 1,
    2, 2, 4, 6, 8, 2)), .Names = c("mpg", "cyl", "disp", "hp",
"drat", "wt", "qsec", "vs", "am", "gear", "carb"), row.names = c("Mazda RX4",
"Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout",
"Valiant", "Duster 360", "Merc 240D", "Merc 230", "Merc 280",
"Merc 280C", "Merc 450SE", "Merc 450SL", "Merc 450SLC", "Cadillac Fleetwood",
"Lincoln Continental", "Chrysler Imperial", "Fiat 128", "Honda Civic",
"Toyota Corolla", "Toyota Corona", "Dodge Challenger", "AMC Javelin",
"Camaro Z28", "Pontiac Firebird", "Fiat X1-9", "Porsche 914-2",
"Lotus Europa", "Ford Pantera L", "Ferrari Dino", "Maserati Bora",
"Volvo 142E"), class = "data.frame")

Set Language Option in R

Set 'English' as the default language.

Sys.setenv(LANGUAGE = "en")

Upgrade tidyverse

tidyverse_update()

List of New Tools

Hadoop or Spark
sparklyr, rhipe, and ddr.

R for Data Science: Import, Tidy, Transform, Visualize, and Model Data

R for Data Science: Import, Tidy, Transform, Visualize, and Model Data

Frontend vs. Backend

Frontend vs. Backend