【问题标题】:Calling R from Stata从 Stata 调用 R
【发布时间】:2015-04-14 15:36:46
【问题描述】:

由于某些非常烦人的原因,我将无法直接在数据中心调用我的 R 脚本。我只能调用stata,然后stata 将不得不调用我的 R 脚本。

目前,我正在尝试使用shell 命令:

capture cd C:\\Correct\Dir 
shell “C:\\Program Files\R-3.1.2\bin\Rscript.exe" "myFile.R"

路径是正确的,但我在 Stata 中运行它时只得到一个蓝屏,没有其他任何反应。蓝屏有一条消息,但它立即消失,所以我不知道它在说什么。

我该如何继续调试呢?有没有更好的方法来做到这一点?我不希望使用像rsource 这样的附加软件包,因为它们需要在安装到数据中心之前进行认证,这是一个漫长的过程。

【问题讨论】:

  • 刚刚注意到驱动器号后面有双反斜杠,但其他文件夹没有。如果你这样做会有所不同capture cd C:\\Correct\\Dir
  • @user2633645 下次我在数据中心时会检查一下。

标签: r shell stata


【解决方案1】:

我认为双反斜杠的存在导致了问题。以下对我有用:

状态

cd "path\of\choice"
shell "C:\Program Files\R\R-3.1.2\bin\Rscript.exe" "test.R"

test.r

setwd("path\\of\\choice")
data(mtcars)
mtcars
write.csv(mtcars, "cars.csv")

【讨论】:

    【解决方案2】:

    这是 Stata 调用 R 的一个示例(不可重现)。

    *----- CALL R -----
    
    // location of input/output files for R
    local dirq "`pdir'/proc_data/q_irepriv.csv" // input 1 -> arg1
    local dirh "`pdir'/proc_data/h_nophincome.csv" // input 2 -> arg2
    local dirRdta "`pdir'/proc_data/`dofile'.Rdta" // output -> arg3
    local dirout "`pdir'/" // project_dir -> arg4
    local dirout "`dofile'/" // do_file_stub -> arg5
    
    // call -rsource- passing the locations as arguments
    rsource using "`pdir'/r_files/`dofile'.R", ///
        roptions(`" --vanilla --args "`dirq'" "`dirh'" "`dirRdta'" "`pdir'" "`dofile'" "')
    
    *----- END OF R -----
    

    我使用rsource,这是一个用户编写的命令,您可以使用ssc install rsource 下载它。

    另一方面,Stata 倾向于在这些情况下使用正斜杠。请参阅 Nick Cox 的 Stata tip 65: Beware the backstabbing backslash

    编辑

    您报告蓝屏消失,没有其他任何事情发生。这可能是 R 因 R 脚本中的某些错误而窒息的结果。举个例子:

    .r 脚本包含:

    # output OK
    head(mtcars)
    
    # provoke error
    2+*2
    

    并且您的 Stata do 文件包含:

    shell "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" --no-save --no-restore --verbose "D:/Datos/rferrer/Desktop/rcars.r" 
    

    以上内容转载了您的报告。

    要进行调试,您可以使用 OS shell 命令重定向输出和错误消息。而不是后者,尝试:

    shell "C:/Program Files/R/R-3.0.3/bin/x64/Rscript.exe" --no-save --no-restore --verbose "D:/Datos/rferrer/Desktop/rcars.r" > Routput.txt 2> Rerror.txt
    

    这会产生两个文件:

    Routput.txt 包含

                       mpg cyl disp  hp drat    wt  qsec vs am gear carb
    Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
    Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
    Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
    Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
    Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
    Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
    

    并且Rerror.txt包含

    running
      'C:\Program Files\R\R-3.0.3\bin\x64\Rterm.exe --slave --no-restore --no-save --no-restore --file=D:/Datos/rferrer/Desktop/rcars.r'
    
    Error: inesperado '*' in "2+*"
    Ejecución interrumpida
    

    (错误是西班牙语的,但那当然是无关紧要的。)

    这是在 Windows 7 企业版上。

    【讨论】:

    • 关于你的注释,我从那篇文章中引用:“事实上,你可以在 Windows 的 Stata 中的目录或文件名中随意混合正斜杠和反斜杠,只要你不使用反斜杠就在左引号之前。”
    • 是的。措辞可能不表达最初的意图,但主要是作为建议。我从未声称这是您问题的根源。我也引用了那篇文章:“最简洁的解决方案是一致地使用正斜杠......”它不那么令人困惑,并且使您的代码可以跨操作系统移植(至少可能)。你可能会认为这是一个品味问题,你可以采取行动,但我仍然支持这个建议。
    • 希望我也能把我的错误变成西班牙语 - 它们听起来更酷! ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多