What is the difference between run and search scripts in R

The main question is when I start R.

What is the main difference when I get an R script against executing it? I am trying to run ggplot2 example scripts.

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 use this script, the graphs are not displayed. I understand that this is due to the lack of an explicit print statement in my code. I read the discussion that when executing a command in an interactive shell, the print statement is implicit.

My question is this: when I run the script vs source, what is the main difference? When will I do one on top of the other? Thanks!

+4
source share
1 answer

This is probably due to the R-FAQ in section 7 regarding why grid-based graphics cannot be constructed. Try using the explicit print or plot command.

Reading the first sentence "Details" on the help page for source for you:

`Details

Note that running the code through the source differs in several respects from entering it on the R command line. Since expressions are not executed at the top level, automatic printing does not work. `(And I'm glad to see you read the rest of this section.)

+4
source

Source: https://habr.com/ru/post/1489803/


All Articles