微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rscript 无法从命令行绘制 xts

如何解决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 举报,一经查实,本站将立刻删除。