What is the difference between executing and sourcing scripts in R - r

A basic question as I am starting out R.
What is the main difference when I am sourcing a R script vs executing it?
I am trying to get ggplot2 example scripts running.
library("ggplot2")
d = data.frame(x1=c(1,3,1,5,4), x2=c(2,4,3,6,6), y1=c(1,1,4,1,3), y2=c(2,2,5,3,5), t=c('a','a','a','b','b'), r=c(1,2,3,4,5))
ggplot() +
scale_x_continuous(name="x") +
scale_y_continuous(name="y") +
geom_rect(data=d, mapping=aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill=t),color="black",alpha=0.5) +
geom_text(data=d, aes(x1+(x2-x1)/2,y=y1+(y2-y1)/2, label=r), size=4) +
opts(title="geom_rect", plot.title=theme_text(size=40, vjust=1.5))
When I source this script, no plots appear. I understand this has to do with lack of explicit print statement in my code. I've read a discussion that when you execute a command in the interactive shell, the print statement is implicit.
My question is this - When I execute a script vs source it, what is the basic difference?
When would I do one over another? Thanks!

This seems likely to be related to The R-FAQ in section 7 relating to why grid based graphics do not get plotted. Try using explicit print or plot command.
Reading the first sentence of "Details" in the help page for source to you:
`Details
Note that running code via source differs in a few respects from entering it at the R command line. Since expressions are not executed at the top level, auto-printing is not done.` (And I'm glad to see that you did read the rest of that section.)

Related

'structure(NULL, *)' deprecated in R 3.5.* when using ggplot2

I'm writing a program which has a step involving ggplot2 in R. The plot's relevant steps are:
ggplot() +
geom_violin(data=x, mapping=aes(x=1, y=Mean_cov)) +
geom_jitter(data=x, mapping=aes(x=3, y=Mean_cov, col=Frac_pos)) +
scale_x_discrete(breaks=c(1,3), labels=c("","")) +
scale_y_continuous(limits=c(0, 50)) +
scale_color_gradient2( breaks=seq(0,100,20),
limits=c(0,100),
low="green3", high="darkorchid4",
midpoint=50, name="% covered") +
coord_flip()
Mean_cov and Frac_pos are colnames() of my data.frame(). 1 and 3 are unelegant solutions I found to obtain a plot that separates the geom_jitter() and the geom_violin() by a sufficient space.
This worked in R/3.3.3 but since our sysadmin updated the default R to R/3.5.2 we are experiencing plotting issues (i.e. the plot gets generated but the axes are overlapping the panel margin, instead of being contained within it). I know I could just point it to R/3.3.3 but I would prefer to make my program compatible with newer R versions, since I want to publish it.
The warning I'm getting is quite popular in R-related error threads:
3: In structure(NULL, class = "waiver") :
Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
Consider 'structure(list(), *)' instead.
I am aware of other stackoverflow issues similar to mine, like this one:
R c.Date() raises warning Calling 'structure(NULL, *)' is deprecated
However, I couldn't solve the problem following other issues so I decided to open a new one since it seems to be a ggplot2 issue.
Perhaps the error is raised because of a way ggplot2 calls some function in the background?
Here are 10 lines from my data frame:
Frac_pos Mean_cov
99.78591232934406 42.783034429509
99.98996910484291 24.89770493118806
98.9460737536315 19.48427592934241
99.80868782354266 45.07804411433716
99.48891662209545 88.17287692399121
99.89221021636827 30.86539422141599
99.56238183247297 25.512161961209184
99.00406672752926 32.80431986056934
99.22226325737003 29.72798766048967
This is how it should look like (as with R/3.3.3):
This is how it looks like with newer R versions (see that it is broader? Since I have millions of those dots, many of them fall out of the panel):

How to prevent program exits in R?

I am new to R and have a problem on running a ggplot command. Below is my code. The program exits immediately without showing the diagram. Is there anything wrong with my code?
#!/usr/bin/env Rscript
library(ggplot2)
data <- read.csv(file="./data/assignment-02-data.csv",head=TRUE,sep=",")
head(data)
nrow(data)
print(ggplot(data, aes(longitude, latitude)) + geom_point())
From this post: Generate multiple graphics from within an R function. It says that I need to print the value but it is not working.
I am running the program from shell ./myr.r.

Expression() command just prints itself

apologies for the trivial nature of this question. I have searched for other instances of this and can't find any.
I am trying to combine the expression() function with paste(), for example to do: expression(paste("TNF", alpha))
But everytime I enter a command using expression() I just get my command returned to me, so the above command just prints:
expression(paste("TNF", alpha))
What am I doing wrong?
Ah ok I had misinterpreted the function ofexpression() and it seems it's interpreted the way I wanted it to be within the scale_x_discreet() call when building my plot in ggplot2.
In case it's useful to anyone else, my code became (where a is a ggplot object):
a + scale_x_discrete(labels=c("Vehicle", expression(paste("TNF", alpha))))
Which then produced a group label of "TNFα".

Debugging: search packages for a specific line of code

I've been investigating this issue to find out which package throws the following error:
Error in if (dif < 0 && any(overlap)) { : missing value where
TRUE/FALSE needed
Long story short, I was not able to locate the bug via traceback, debug or any other debugging tool, so I downloaded the source of the package itself and its dependencies, unpacked them and ran
grep any\(overlap\) */R/*
which resulted in
directlabels/R/utility.function.R: if(dif<0&&any(overlap)){
and that's where the error is thrown, so the task is complete.
I have two major questions here, actually.
Is there a simpler way to get to the same result? Imagine I had ten depends, downloading all these sources would have become a pain. Say, I'd love to see something like
search_packages_source <- function(list_of_packages, string), is there anything like that available?
Why is traceback/debug failing to point at the function which throws an error? (traceback shows a trace for the plot rendering, with debug I have to guess whether to step into or to step over, so I gave up quickly, see the edit below for more detail) I have also tried Rstudio's debugging tools, none of them helped to locate the error.
Debugging in R is a rare task for me, so I'm probably missing something very simple. Would be delighted to hear your tips & tricks!
EDIT: here's the shortcut to the code that can be reproduced and a bit of clarification:
p1 <- ggplot(mtcars, aes(disp, mpg, col=cyl)) + geom_line()
p2 <- ggplot(subset(mtcars, cyl==4), aes(disp, mpg, col=cyl)) + geom_line()
# OK
direct.label(p1, "last.bumpup")
# error
direct.label(p2, "last.bumpup")
Now, if I traceback(), I get a long output with no indication which utility function failed. If I debug(), I get an even longer output, which also leads nowhere. So the problem is not only my debugging techniques are lacking, but also because of the cross-dependencies between the packages.

How to call plot.xts when using RScript

UPDATE: Thanks to Joshua's comment I realized the problem wasn't being inside a function, but inside a script. So I've edited the question and also provided my own answer.
When I use plot.xts() interactively it pops up a graphics window. I just tried it from inside a function (I'm troubleshooting a unit test and wanted some visual help) but nothing appeared. Aha, says I, I know the trick, just use print.
But print(plot.xts(x)) still shows no chart and instead prints my xts object! I.e. it does exactly the same as print(x).
The script I use to run unit tests is:
#!/usr/bin/Rscript --slave
library('RUnit')
options(warn=2) #Turn warnings into errors
#By naming the files runit.*.R, and naming the functions test*(), we can use
# all the defaults to defineTestSuite().
#NOTE: they have a weird default random number generator, so changed here
# to match the R defaults instead.
test.suite=defineTestSuite('tests',dirs=file.path('tests'),
rngKind = "Mersenne-Twister", rngNormalKind = "Inversion")
test.result <- runTestSuite(test.suite)
printTextProtocol(test.result)
The script below does two things:
plot to a device file, as you would in headless setting such as a webserver,
plot a screen device, I use x11() but you could use win().
There is no limitation imposed by Rscript. And this has nothing to do with xts as you could just as easily plot an xts object.
#!/usr/bin/Rscript
set.seed(42)
x <- cumsum(rnorm(100))
png("/tmp/darren.png")
plot(x)
dev.off()
x11()
plot(x)
Sys.sleep(3) # could wait for key pressed or ...
You cannot use graphics (or input functions like readline) when using RScript. However an RScript is still just R, so when you want to add something interactive (e.g. for troubleshooting) start R, then type:
source('run_tests.R')
When run this way, a line like this shows the chart:
plot(x$High);cat("Press a key");readline()
When run directly from the commandline with ./run_tests.R that line gets quietly ignored.

Resources