低延迟R提交

时间:2017-01-06 07:26:13

标签: r

我创建了一些R代码,接受csv并生成和输出,现在我称之为:

Rscript code.R input.csv

这里code.R是要执行的代码,input.csv是它用作输入的文件

问题:

脚本需要5秒或更长时间才能生成结果,这是因为从shell调用了R,库需要时间加载。

问题:

是否可以在后台运行R或作为服务加载所有库,我可以提交我的工作,只需要时间来计算?

完全披露:

脚本是一个ML模型,它加载.RDA对象,脚本调用预测函数

1 个答案:

答案 0 :(得分:0)

  1. 打开R控制台并运行/加载所需的所有库。
  2. 使用source()现在运行您的R脚本
  3. Test.R文件具有以下代码

    #This file has no library declarations
    c <- ggplot(mtcars, aes(factor(cyl)))
    c <- c + geom_bar()
    print(c)
    

    现在我从我的控制台运行

    > library(ggplot2)
    > source("<Path>/test.R")
    

    <强>输出:

    enter image description here

    编辑:传递Params以及source()命令

    您可以通过覆盖commandArgs()

    来执行此操作

    新test.R文件代码:

    c <- ggplot(mtcars, aes(factor(cyl)))
    c <- c + geom_bar()
    print(c)
    print(commandArgs())
    

    现在来自控制台:

    > commandArgs <- function() c('a','b')
    > source("<Path>/test.R")
    [1] "a" "b"
    (Along with the graph)
    
相关问题