Using png function not working when called within a function

后端 未结 2 837
暖寄归人
暖寄归人 2021-01-03 19:37

I have a function that does stuff and then plots based on a condition:

f <- function(n) {
  rand <- rnorm(n)
  no   <- seq_len(n)
  df   <- data.         


        
2条回答
  •  旧巷少年郎
    2021-01-03 20:05

    I just learned from other website (link provided below). In a loop you have to explicitly use print function in order to make jpeg(), png() function to work. In the original post, you can just add a line of print(p).

      if (n > 10) {
            png("plot.png")
            p <- ggplot(df)
            p + geom_point(aes(x=no, y=rand))
            print(p)
            dev.off()
        }
    

    In the link below, it provides a good explanation for this https://stat545-ubc.github.io/block017_write-figure-to-file.html#despair-over-non-existent-or-empty-figures

提交回复
热议问题