[R] ggplot inside function doesn't plot

Duncan Murdoch murdoch.duncan at gmail.com
Thu Nov 2 18:39:05 CET 2017


On 02/11/2017 1:03 PM, Ed Siefker wrote:
> I don't really understand. I mean, I understand the solution is
> print(ggplot(...)).  But why is that required in a function and not at
> the console?
> 
> Shouldn't I be able to rely on what I do at the console working in a
> script?  Is this inconsistent behavior by design?

Yes, it's by design.  At top level, values are normally printed after 
being computed (but this can be turned off).  So

x <- 5
x + 1

will print 6 after the second line.  If you have the line "x + 1" in a 
function, nothing will print.  So you can have a function

addOne <- function(x) {
   x + 1
}

and you can call it lots of times from other functions without printing 
a line each time.

Duncan Murdoch

> 
> 
> 
> On Thu, Nov 2, 2017 at 11:54 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>>
>>> On Nov 2, 2017, at 9:27 AM, Ed Siefker <ebs15242 at gmail.com> wrote:
>>>
>>> I have a function:
>>>
>>> myplot <- function (X) {
>>>     d <- plotCounts(dds2, gene=X, intgroup="condition", returnData=TRUE)
>>>     png(paste("img/", X, ".png", sep=""))
>>>     ggplot(d, aes(x=condition, y=count, color=condition)) +
>>>         geom_point(position=position_jitter(w=0.1,h=0)) +
>>>         scale_y_log10(breaks=c(25,100,400)) +
>>>         ggtitle(X) +
>>>         theme(plot.title = element_text(hjust = 0.5))
>>>
>>>     dev.off()
>>>     }
>>>
>>> 'd' is a dataframe
>>>
>>>                              count      condition
>>> E11.5 F20HET BA40_quant   955.9788   E11.5 F20HET
>>> E11.5 F20HET BA45_quant   796.2863   E11.5 F20HET
>>> E11.5 F20HET BB84_quant   745.0340   E11.5 F20HET
>>> E11.5 F9.20DKO YEH3_quant 334.2994 E11.5 F9.20DKO
>>> E11.5 F9.20DKO fkm1_quant 313.7307 E11.5 F9.20DKO
>>> E11.5 F9.20DKO zzE2_quant 349.3313 E11.5 F9.20DKO
>>>
>>> If I set X="Etv5" and paste the contents of the function into R, I get
>>> 'img/Etv5.png'
>>> If I run myplot(X), I get nothing.
>>>
>>>
>>>> X
>>> [1] "Etv5"
>>>> list.files("img")
>>> character(0)
>>>> myplot(X)
>>> null device
>>>           1
>>>> list.files("img")
>>> character(0)
>>>> d <- plotCounts(dds2, gene=X, intgroup="condition", returnData=TRUE)
>>>> png(paste("img/", X, ".png", sep=""))
>>>> ggplot(d, aes(x=condition, y=count, color=condition)) +
>>> + geom_point(position=position_jitter(w=0.1,h=0)) +
>>> + scale_y_log10(breaks=c(25,100,400)) +
>>> + ggtitle(X) +
>>> + theme(plot.title = element_text(hjust = 0.5))
>>>> dev.off()
>>> null device
>>>           1
>>>> list.files("img")
>>> [1] "Etv5.png"
>>>
>>> Why doesn't my function work?
>>
>> `ggplot` creates an object. You need to print it when used inside a function. Inside a function (in a more restricted environment) there is no parse-eval-print-loop.
>>
>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> David Winsemius
>> Alameda, CA, USA
>>
>> 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law
>>
>>
>>
>>
>>
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list