R autoplot 在 运行 行时有效,但在获取 R 脚本时无效
R autoplot works when running line-by-line but not when source-ing R script
我观察到 R 的一个非常奇怪的行为。
当我从终端将以下代码逐行输入到 R 运行 的实例中时,以下代码有效。 (OS 是 Debian Linux。)
但是当我尝试 运行 source("script.R")
.
时它不起作用
它在 R Studio 中也不起作用。
具体来说,它无法使用 autoplot
生成图形输出。写入 pdf 文件不起作用,如果我删除 pdf()
和 dev.off()
行,则不会打开包含该图的 window。
这是我的脚本的副本...
library(lubridate)
library(ggplot2)
library(matrixStats)
library(forecast)
df_input <- read.csv("postprocessed.csv")
x <- df_input$time
y <- df_input$value
df <- data.frame(x, y)
x <- df$x
y <- df$y
holtmodel <- holt(y)
pdf("autoplot.pdf")
autoplot(holtmodel)
dev.off()
为了方便起见,这里有一个数据文件。
"","time","value"
"1",1,2.61066016308988
"2",2,3.41246054742996
"3",3,3.8608767964033
"4",4,4.28686048552237
"5",5,4.4923132964825
"6",6,4.50557049744317
"7",7,4.50944447661246
"8",8,4.51097373134893
"9",9,4.48788748823809
"10",10,4.34603985656981
"11",11,4.28677073671406
"12",12,4.20065901625172
"13",13,4.02514194962519
"14",14,3.91360194972916
"15",15,3.85865748409081
"16",16,3.81318053258601
"17",17,3.70380706527433
"18",18,3.61552922363713
"19",19,3.61405310598722
"20",20,3.64591327503384
"21",21,3.70234435835577
"22",22,3.73503970503372
"23",23,3.81003078640584
"24",24,3.88201196162666
"25",25,3.89872518158949
"26",26,3.97432743542362
"27",27,4.2523675144599
"28",28,4.34654855854847
"29",29,4.49276038902684
"30",30,4.67830892029687
"31",31,4.91896819673664
"32",32,5.04350767355202
"33",33,5.09073406942046
"34",34,5.18510849382162
"35",35,5.18353176529036
"36",36,5.2210776270173
"37",37,5.22643491929207
"38",38,5.11137006553725
"39",39,5.01052467981257
"40",40,5.0361056705898
"41",41,5.18149486951409
"42",42,5.36334869132276
"43",43,5.43053620818444
"44",44,5.60001072279525
很困惑,因为它看起来像一个微不足道的脚本!
改为:
print(autoplot(holtmodel))
当您单步执行代码时,您会在每个代码行上得到一个隐式的 print(...)
语句。当你 source() 你没有。 ggplot(和其他人!)使用 print() 来触发他们的绘图(这样你就可以方便地逐步构建绘图而不必等待闪烁的数字)
我观察到 R 的一个非常奇怪的行为。
当我从终端将以下代码逐行输入到 R 运行 的实例中时,以下代码有效。 (OS 是 Debian Linux。)
但是当我尝试 运行 source("script.R")
.
它在 R Studio 中也不起作用。
具体来说,它无法使用 autoplot
生成图形输出。写入 pdf 文件不起作用,如果我删除 pdf()
和 dev.off()
行,则不会打开包含该图的 window。
这是我的脚本的副本...
library(lubridate)
library(ggplot2)
library(matrixStats)
library(forecast)
df_input <- read.csv("postprocessed.csv")
x <- df_input$time
y <- df_input$value
df <- data.frame(x, y)
x <- df$x
y <- df$y
holtmodel <- holt(y)
pdf("autoplot.pdf")
autoplot(holtmodel)
dev.off()
为了方便起见,这里有一个数据文件。
"","time","value"
"1",1,2.61066016308988
"2",2,3.41246054742996
"3",3,3.8608767964033
"4",4,4.28686048552237
"5",5,4.4923132964825
"6",6,4.50557049744317
"7",7,4.50944447661246
"8",8,4.51097373134893
"9",9,4.48788748823809
"10",10,4.34603985656981
"11",11,4.28677073671406
"12",12,4.20065901625172
"13",13,4.02514194962519
"14",14,3.91360194972916
"15",15,3.85865748409081
"16",16,3.81318053258601
"17",17,3.70380706527433
"18",18,3.61552922363713
"19",19,3.61405310598722
"20",20,3.64591327503384
"21",21,3.70234435835577
"22",22,3.73503970503372
"23",23,3.81003078640584
"24",24,3.88201196162666
"25",25,3.89872518158949
"26",26,3.97432743542362
"27",27,4.2523675144599
"28",28,4.34654855854847
"29",29,4.49276038902684
"30",30,4.67830892029687
"31",31,4.91896819673664
"32",32,5.04350767355202
"33",33,5.09073406942046
"34",34,5.18510849382162
"35",35,5.18353176529036
"36",36,5.2210776270173
"37",37,5.22643491929207
"38",38,5.11137006553725
"39",39,5.01052467981257
"40",40,5.0361056705898
"41",41,5.18149486951409
"42",42,5.36334869132276
"43",43,5.43053620818444
"44",44,5.60001072279525
很困惑,因为它看起来像一个微不足道的脚本!
改为:
print(autoplot(holtmodel))
当您单步执行代码时,您会在每个代码行上得到一个隐式的 print(...)
语句。当你 source() 你没有。 ggplot(和其他人!)使用 print() 来触发他们的绘图(这样你就可以方便地逐步构建绘图而不必等待闪烁的数字)