Rscript 无法从命令行绘制 xts

问题描述

我有一个 Rscript 为我做了一些工作,其中之一是绘制股票价格数据(可以是每日、每小时或逐笔报价数据)。我能够从终端发出命令来绘制 matrix,但是绘制 xts 不起作用。

下面我使用 docopt 包来实现一个过于简化的 R 命令行界面,足以演示所描述的问题。我相信这个问题更多的是 plotxts 因此 更改命令行接口实现应该是安全的(但您必须保持测试功能完整)。

当我从 CLI 运行 test.matrix() 时,会打开一个 png 文件显示我预期的绘图。 当我从 CLI 运行 test.xts() 时,弹出错误,说没有名为“test2.png”的文件;对于 xts 对象,该文件甚至不存在 开始。

为什么在这种情况下绘制 xts 不起作用?是否有从 CLI 绘制 xts解决方法

我正在运行基于 Debian 10 的 Linux 系统。

# code in test.R

library(dplyr)
library(xts)

test.matrix <- function(){
  data(sample_matrix)
  png("test.png")
  plot(x = sample_matrix,main = paste("Test","xts",sep=" "))
  dev.off()
  browseURL("test.png")
}
test.xts <- function(){
  data(sample_matrix)
  png("test2.png")
  sample.df <- sample_matrix %>% as.data.frame()
  test.data <- sample.df$Open
  test.date <- as.Date(rownames(sample.df))
  test <- xts(test.data,order.by=test.date)
  plot(x = test,sep=" "))
  dev.off()
  browseURL("test2.png")
}

# code in Rscript rf.R
# rf.R test
#!/usr/bin/Rscript --vanilla

# for demo purpose,the actual paths to r files are omitted.
# They are just the usual absolute paths to files under project directory.
suppressMessages(source('test.R'))

readr::read_file('rf_doc') -> doc
args <- docopt(doc,"test",version="1.0\n") 

if (args$test){
  test.xts()
 #test.matrix()
}

# rf_doc,a plain txt file (in Linux it has no file extension).
# the file dictates how the rf command is going to work.
# the pattern is used by `docopt` package.

Usage:
rf test
rf --help

Options:
--help  show this help page.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)