• R语言
  • 【求助】将R图像输出成pdf格式,显示成“文件不含页面”,打不开~

现在的情况是:
如果直接运行那几行代码,是可以打开pdf的,但是如果直接保存运行我的脚本文件(在画图代码之前还有多行代码),则有pdf文件,但是打不开,显示“文件不含页面”。
我查看了一本书:《R数据可视化手册》,里面有如下的一段话:如果你使用某个脚本来创建图形,而在创建图形的过程中抛出了一个错误,则R可能无法执行到dev.off()这一步调用,并可能停留在PDF设备仍然开启的状态。当这种情况发生时,直到你去**手动*调用dev.off()之前,PDF文件将无法正常打开。

我觉得这是不能正常打开的原因,可是我想用脚本运行,不想每次都手动调用dev.off(),这怎么解决呢?

    Jonie_Y
    代码:

    ####plot the m0N
    pic_data <- data.frame(x = t, y = m0t, z = apply(m0N, 2, mean))
    filename3 <- paste("Pic_CL", fmodeli, cr, tr, "pdf", sep = ".")
    pdf(filename3, width = 5, height = 4)
    g <- ggplot(data = pic_data, aes(x = x, y = z))
    g + geom_abline(slope = D1, intercept = D2, color = "#56B4E9", size = 0.5) + geom_point(
      size = 0.5) + xlab("t") + ylab("m0(t)") + annotate(
        "text", x = 0.8, y = 0.45, label = "n = ") + annotate(
          "text", x = 0.86, y = 0.45, label = n) + annotate(
            "text", x = 0.84, y = 0.4, label = "cr = tr = ") + annotate(
              "text", x = 0.94, y = 0.4, label = cr)
    ggsave(filename3, width = 10, height = 6, units = "cm")
    dev.off()

    运行结果:

    Time difference of 12.59397 secs
    There were 50 or more warnings (use warnings() to see the first 50)

    这些警告信息是画图之前的代码产生的,如果单独运行贴出来的代码,是不报错的,pdf正常打开.
    如果直接全部运行所有代码,有pdf,但是打不开。显示“文件不含页面”.