How to exit Debug Browser in R

This works if you have enabled debugging for a single command/function, and you are not aware how to exit the Browser prompt. To save you the trouble, popular Googles that ask you to f or Q or ESC or q() simply do not work. The behavior of debug command is identical in RStudio and the R prompt, at least in Windows 10 that i tested.

Here are two examples of enabling/disabling debug.

First we use a random sapply command:

> sapply(split(mtcars$mpg, mtcars$cyl), mean)
4 6 8
26.66364 19.74286 15.10000

Now we turn debugging on:

> debug(sapply)

and, as expected, nothing happens until we try to run sapply again:

> sapply(split(mtcars$mpg, mtcars$cyl), mean)
debugging in: sapply(split(mtcars$mpg, mtcars$cyl), mean)
debug: {
FUN <- match.fun(FUN)
answer <- lapply(X = X, FUN = FUN, ...)
if (USE.NAMES && is.character(X) && is.null(names(answer)))
names(answer) <- X if (!isFALSE(simplify) && length(answer)) simplify2array(answer, higher = (simplify == "array")) else answer } Browse[2]>

There are two things to type here. The first is the command undebug(). In this case will be sapply. Then, Q exits the Browser prompt, and we can run the function outside of debug mode again. Let’s see:

Browse[2]> undebug(sapply)
Browse[2]> Q
> sapply(split(mtcars$mpg, mtcars$cyl), mean)
4 6 8
26.66364 19.74286 15.10000

Identically with ls()

ls(mtcars)
[1] "am" "carb" "cyl" "disp" "drat" "gear" "hp" "mpg" "qsec" "vs"
[11] "wt"
> debug(ls)
> ls(mtcars)
debugging in: ls(mtcars)
debug: {
if (!missing(name)) {
pos <- tryCatch(name, error = function(e) e)
if (inherits(pos, "error")) {
name <- substitute(name)
if (!is.character(name))
name <- deparse(name)
warning(gettextf("%s converted to character string",
sQuote(name)), domain = NA)
pos <- name
}
}
all.names <- .Internal(ls(envir, all.names, sorted))
if (!missing(pattern)) {
if ((ll <- length(grep("[", pattern, fixed = TRUE))) &&
ll != length(grep("]", pattern, fixed = TRUE))) {
if (pattern == "[") {
pattern <- "\\["
warning("replaced regular expression pattern '[' by '\\\\['")
}
else if (length(grep("[^\\\\]\\[<-", pattern))) {
pattern <- sub("\\[<-", "\\\\\\[<-",
pattern)
warning("replaced '[<-' by '\\\\[<-' in regular expression pattern") } } grep(pattern, all.names, value = TRUE) } else all.names }

Browse[2]>undebug(ls)
Browse[2]> Q
> ls(mtcars)
[1] "am" "carb" "cyl" "disp" "drat" "gear" "hp" "mpg" "qsec" "vs"
[11] "wt"
>

Linear Algebra in R, create and invert matrices

This is a simple test case of creating a random 2×2 matrix, performing its inversion, and multiplying them. We will also use MS Excel to check our computations
Create a 2×2 matrix

> mNP <- matrix(rnorm(4),nrow=2,ncol=2)

This command used the matrix() function . This first argument, the datasets, uses rnorm() function to generate 4 random variates, and the next two arguments explain that those are to be ordered in a matrix of 2 rows by 2 columns. Now let’s display the matrix

> mNP
[,1] [,2]
[1,] 0.5644179 -0.4694577
[2,] 0.7707571 0.1500823

Next, we use solve() function to invert the matrix, and display the output

> mNP_inv <- solve(mNP)
> mNP_inv
[,1] [,2]
[1,] 0.3360953 1.051306
[2,] -1.7260381 1.263961

Finally, we use the %*% operator to to the algebraic multiplication of the matrices and check if we arrive to I

> mNP %*% mNP_inv
[,1] [,2]
[1,] 1 0
[2,] 0 1
>

If you would want to do the last operation in Excel, as a check, then let’s suppose that the first matrix sits in fields A1,B1,A2,B2 and the second one sits in fields D1,E1,D2,E2. Their multiplication product would be:
Top left, top right
=A1*D1+B1*D2 =A1*E1+B1*E2
Bottom left, bottom right
=A2*D1+B2*D2 =A2*E1+B2*E2

Use an online spreadsheet to feed data to R

Instead of creating the data frame programmatically, why not use an existing spreadsheet, available online. A simple HTTP file server, free and Open Source is HFS: http://rejetto.com/hfs/?f=
Once HFS is installed, uploading the spreadsheet is as simple as dragging and dropping an existing spreadsheet. Here is how it looks on my machine:

RFS

Once the file is in place, we can get its URL directly by right-clicking on the file, and selecting “Copy URL address – Ctrl+C”

The first time it will be needed to read the spreadsheet into R, some more software is needed

Download and install a PERL home on the machine. For Windows, Strawberry Perl can be used (link: http://strawberryperl.com/ )

Next, the package to read the spreadsheet needs to be installed into R. The package name is “gdata” so we perform

install.packages("gdata")
and
require("gdata")

at this point, we are ready to load directly the spreadsheet into a new data frame

dfXLBAN <- read.xls("http://192.168.0.107/BaRanas.xlsx")
trying URL 'http://192.168.0.107/BaRanas.xlsx'
Content type 'application/octet-stream' length 8786 bytes
downloaded 8786 bytes
lets display the data frame:

dfXLBAN
Date Name Bananas
1 1 Radka 2
2 2 Radka 3
3 3 Radka 4
4 4 Radka 5
5 5 Radka 1
6 6 Radka 1
7 7 Radka 7
8 1 Natalie 6
9 2 Natalie 2
10 3 Natalie 3
11 4 Natalie 4
12 5 Natalie 2
13 6 Natalie 1
14 7 Natalie 8

Since the spreadsheet contained one more row, we need (in order to have the dataframe exactly the same as our example with inline data) to discard the Date Column:
dfB2 <- data.frame(dfXLBAN$Name,dfXLBAN$Bananas)

dfB2
dfXLBAN.Name dfXLBAN.Bananas
1 Radka 2
2 Radka 3
3 Radka 4
4 Radka 5
5 Radka 1
6 Radka 1
7 Radka 7
8 Natalie 6
9 Natalie 2
10 Natalie 3
11 Natalie 4
12 Natalie 2
13 Natalie 1
14 Natalie 8

the plot function will be exactly the same as the previous post

R Language – from nothing to a plot of your own data

What is R?

R is a procedural programming language, with built in statistical functions and the ability to generate graphical plots of data

Let’s install some software (all software below is Open Source License)

Use CRAN (Comprehensive R Archive Network) to download and install R. Go to CRAN https://cran.r-project.org/mirrors.html, select the mirror suitable for your geo location, and choose your platform. This will install R language itself

Then, let’s install RStudio.

Go to https://www.rstudio.com/products/rstudio/download/ and choose “RStudio Desktop – Open Source License”

Create the Case Study

The one used here is how many bananas did 2 people eat, for one week (7 days). There is data for two people.

Launch RStudio

Screenshot of RStudio
Screenshot of RStudio, to explain the tabs outline

Observe the basic elements of the IDE: Script upper left, console below. Once the commands are executed, the values of the variables are displayed top-right. As seen in the screen to the left, there are 4 elements created, 2 vectors and 2 dataframes, and all assigned to variables. The two vectors contain our data (who ate how many bananas) and the dataframes are used for the plots. The first dataframe is used to determine the axis, the second to compute the statistical data.
Quick note, for those double-checking the numbers: the functions use N-1 instead of N as dividend, meaning they treat the data set as a sample, instead of a population. The mapping of the function names, between R and Excel for Variance is (EXCEL: VAR, R: VAR), and St.Dev (EXCEL:STDEV, R: SD)Excel, additionally, has functions VAR.P and STDEV.P where the dividend is N, providing a calculation that is considered to cover the entire dataset, and not a sample.

Now we need to install the graphics libraries to our installation and have them available for our code. For this, we need to type two commands. The first, connects to CRAN (so a Internet connection is required) to get the libraries.

Write our code and enter data

install.packages("ggplot2")
library(ggplot2)

Now let’s create some sample data. We will create a “data frame”, which will consist of two vectors, one holding the measurements (fruit consumed per day) and the second will contain the person linked to this measurement

cName <- c('Radka','Radka','Radka','Radka','Radka','Radka','Radka','Natalie','Natalie',

'Natalie','Natalie','Natalie','Natalie','Natalie')

BpD <- c(2, 3, 6, 0, 1, 5, 4,1, 4, 5, 7, 3, 9, 1)

dfNP <- data.frame(cName,BpD)

Now, we want to calculate the mean and Standard Deviation (see next post for explanation of terms) of our data, and put them in their own data frame:

dsTATNP <- plyr::ddply(dfNP, "cName", plyr::summarise, mean = mean(BpD), sd = sd(BpD))

And finally, perform the plot itself, using this command:

p <- ggplot() +
xlab("Participant Name")+
ylab("Bananas per Day")+
geom_point(data=dfNP,aes(cName,BpD))+
geom_point(data=dsTATNP,aes(cName,mean), colour = 'red', size = 3)+
geom_point(data=dsTATNP,aes(cName,sd), colour = 'green', size = 4)
p + labs(title = "R/ggplot demo, Nikolas Perdikis May 2018", subtitle = "Visit my starter Big Data blog: http://www.smalldeskbigdata.com")

It should look something like this:

R/ggplot demo
Black dots are daily data consumption of fruit,
Green dot is Standard Deviation,
Red dot is Mean value

Additional steps

Use the following commands as practice:

-Type the name of the variable in the Console, to display its value. in our case, type cName and BpD

– Use help for commands using help(). For the commands we have used, you can use help(c), help(ggplot)

See how the value of the variables is displayed in the top right window, while help,output and other elements is displayed in the bottom right window

Environment and Output
in the lower window, switch between the tabs to see relevant information