【问题标题】:How to run a function contained in a R script, from Python ? The inputs for R function will be coming from Python如何从 Python 运行 R 脚本中包含的函数? R 函数的输入将来自 Python
【发布时间】:2019-04-05 08:51:47
【问题描述】:

我正在为 Python 使用 Pycharm。我在 R 中有一个名为“forecast.R”的脚本,其中包含一个函数 -> dummy_forecast(start_date, end_date) 输出给定日期提前 1 周的预测。输出采用包含 time_stamp 和预测值的数据帧格式。我想从 Python 指定脚本的输入,并将预测数据帧也存储在 Python 中。

有没有办法在python中导入forecast.R脚本并调用函数dummy_forecast(),输入start_date和end_date存储在python中并输入到函数中?

rpy2 包可用。但是,我们需要编写整个 R 脚本,这是多余的,并且需要更多的 Python 脚本处理时间。

我需要在 Pycharm 而不是命令行中完成此操作。

【问题讨论】:

    标签: python r pycharm forecasting


    【解决方案1】:

    如果这个'R'脚本是用python编写的,你可以编辑函数在运行结束时返回预测,然后当你在主脚本中调用这个函数时,你可以给它分配一个变量,所以例如:

    脚本 R:

    def dummy_forecast(s, e):
        #do calculations etc
        return forecast #or whatever you have called the data stored in here.
    
    

    主脚本:

    #as long as script R is in the same folder as the main script, you can just do:
    import R
    #if it is not, this is what you want to do:
    import sys
    sys.path.insert(0, r'C:\directory\of\script\r')
    import R
    
    #then once you have imported R, you can do the following
    start = 'blah blah'
    end = 'blah blah'
    
    forecast = R.dummy_forecast(start, end)
    print forecast
    
    

    【讨论】:

    • 看来我没有正确阅读您的问题,这对您的问题没有任何用处,对不起!祝你好运。
    • 你可以删除你的答案然后
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2019-07-11
    • 1970-01-01
    • 2013-11-22
    • 2013-05-03
    相关资源
    最近更新 更多