Custom Function, ggplot and return values

后端 未结 1 1534
[愿得一人]
[愿得一人] 2021-01-02 07:19

Today I noticed something strange. I wrote a function which should return a dataframe and a plot, a plot produced with ggplot2.

But if I run the function, either the

相关标签:
1条回答
  • 2021-01-02 07:59

    I'll answer but I know this is a repeated question and it may likely get closed:

    With ggplot you need to explicitally use print inside a function as in:

    dummyfunct<-function(){
        df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
               total_bill = c(14.89, 17.23))
        x <- ggplot(data=df, aes(x=time, y=total_bill)) + geom_bar(aes(fill=time))
        print(x)
        return(df)
    } 
    
    dummyfunct()
    
    0 讨论(0)
提交回复
热议问题