Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using R in Stata - Problem with rsource command.

    Hello,

    In my Stata code I need to use only 1 or 2 commands that are in R. For that I would like to be able to use directly commands from R in my Stata code.
    I found that the "rsource" package could answer this need but unfortunately I can't get it to work.

    Basically, I would like to be able to save data calculated in Stata, then open and process them using R commands but remaining in Stata.

    In my code below, I saved my data (line "save") and then tried to use the rsource command. I only put in a few fake commands afterwards just to see if rsource worked.

    Unfortunately, the result is always the same: only the first line is read (the installation of the first library) but then no other commands are applied. Stata indicates "End of R output" well before the term chosen as "terminator" (END_OF_R).

    Does anyone have an idea of the problem?

    Thanks for your help

    Valentin


    The Stata code I use:

    *Save the data to be reused in R

    save "C:\Users\valentin.favrebul\switchdrive2\INFIN EED (EWG call 2022)\Data\Segmentation\R\k_means_R.dta", replace



    ***Use R


    rsource, terminator(END_OF_R) rpath("C:/PROGRA~1/R/R-42~1.2/bin/R") roption(`"--vanilla"')


    library(tidyverse);
    library(haven);
    rk <- read.dta("C:\Users\valentin.favrebul\switchdrive2\ INFINEED (EWG call 2022)\Data\Segmentation\R\k_means_R.dta");
    dwplot(rk);
    ggsave("C:\Users\valentin.favrebul\switchdrive2\IN FINEED (EWG call 2022)\Data\Segmentation\Rtest.png");

    END_OF_R


    What returns Stata:
    *Save the data to be reused in R


    . save "C:\Users\valentin.favrebul\switchdrive2\INFIN EED (EWG call 2022)

    > \Data\Segmentation\R\k_means_R.dta", replace

    file C:\Users\valentin.favrebul\switchdrive2\INFINEED (EWG call

    2022)\Data\Segmentation\R\k_means_R.dta saved


    . ***Use R

    . rsource, terminator(END_OF_R) rpath("C:/PROGRA~1/R/R-42~1.2/bin/R") ro

    > ption(`"--vanilla"')

    Assumed R program path: "C:/PROGRA~1/R/R-42~1.2/bin/R"

    Beginning of R output



    R version 4.2.2 (2022-10-31 ucrt) -- "Innocent and Trusting"

    Copyright (C) 2022 The R Foundation for Statistical Computing

    Platform: x86_64-w64-mingw32/x64 (64-bit)



    R est un logiciel libre livré sans AUCUNE GARANTIE.

    Vous pouvez le redistribuer sous certaines conditions.

    Tapez 'license()' ou 'licence()' pour plus de détails.



    R est un projet collaboratif avec de nombreux contributeurs.

    Tapez 'contributors()' pour plus d'information et

    'citation()' pour la façon de le citer dans les publications.



    Tapez 'demo()' pour des démonstrations, 'help()' pour l'aide

    en ligne ou 'help.start()' pour obtenir l'aide au format HTML.

    Tapez 'q()' pour quitter R.



    >

    >

    > library(tidyverse);

    End of R output



    .

    end of do-file



    An example of the data set if needed:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    
    clear
    
    input long ni float(kwh_night kwh_morning kwh_daytime kwh_evening sd_mean) byte(c1 c2 c3 c4 c5 c6 c7 c8 c9 c10)
    
    13101200  .02586881 .035411272  .05646975  .05709381  .2945937 1 1 3 1 1 1 1 1 1  5
    
    13103610  .02408313  .03798587  .04707423  .06328735   .358707 1 1 1 1 5 5 5 5 5 10
    
    13105070 .036413535  .03101066  .04396908  .05319417  .2135963 1 1 3 3 1 6 6 6 8  8
    
    13107000  .03016816  .03498455  .04724505  .05708116  .3215022 1 1 1 1 5 1 5 1 5 10
    
    13111430 .025377603   .0434453  .05113981  .05618017  .4663341 1 2 1 4 4 5 4 4 4  4
    
    13112200  .02435697  .03886757  .04483773  .06370943 .29367575 1 1 3 1 1 1 1 1 1  5
    
    13122000 .036456723  .03789596  .05115277  .04509922  .5637156 1 2 1 4 4 4 2 2 2  2
    
    14410250  .07006316  .02337986 .011070595  .03308997   .582551 1 2 1 4 4 4 2 2 2  2
    
    14501410  .03919303  .04002412  .03097596  .05189463  .8916321 1 2 2 2 2 2 7 7 7  7
    
    14501910  .07103175 .010951414 .013404476  .03761295 .27307943 1 1 3 1 1 1 1 8 1  5
    
    14602100  .01887927  .03966153  .06914034  .05641131  .3323737 1 1 1 1 5 1 5 1 5 10
    
    14608800 .025716087 .036751334  .05562153  .05700911  .3882697 1 1 1 1 5 5 4 5 9  9
    
    14620210 .036209047  .04292444  .04290661  .04725634 .20890547 1 1 3 3 3 6 6 6 8  8
    
    15201900 .021521136 .035143267  .05910018  .06133372  .2814808 1 1 3 1 1 1 1 8 1  5
    
    15202200 .035738543  .03659693  .04295029  .05145203  .5333049 1 2 1 4 4 4 2 4 4  4
    
    15202600 .021742014  .05251586  .06040862  .05037485 .42399395 1 2 1 1 5 5 4 5 9  9
    
    15205500  .02434692    .042062  .05606373  .05548212  .2852345 1 1 3 1 1 1 1 8 1  5
    
    15205700  .03355397  .03836403  .05201047  .04807376  .4634457 1 2 1 4 4 5 4 4 4  4
    
    15208120  .04238089  .03842289   .0416528  .04260989 .16595367 1 1 3 3 3 3 6 6 6  6
    
    15210110  .02832986 .027237374  .04778056  .06356564 .17616875 1 1 3 3 3 3 6 6 6  6
    
    15210930 .029481975  .05171331  .04260881   .0510534  .5213418 1 2 1 4 4 4 2 4 4  4
    
    15211500    .024277  .04877212  .05978931  .04960875  .4197294 1 1 1 1 5 5 4 5 9  9
    
    15214500 .024340505  .04663818  .06941263  .04524746  .7740105 1 2 2 2 2 4 2 2 2  2
    
    15216210 .025350844   .0407245  .04972705   .0585766 .22932576 1 1 3 3 1 6 1 8 8  1
    
    15216810 .023958636  .04163208   .0490776  .06021908  .1494886 1 1 3 3 3 3 3 3 6  6
    
    15219000 .020975254 .030881196  .06562112  .06074478  .3290723 1 1 1 1 5 1 5 1 5 10
    
    15221010 .022790574  .04689084  .06136829  .05169262   .314393 1 1 1 1 5 1 5 1 5 10
    
    15221330  .02691225  .04020908  .05783731  .05222917 .11344614 1 1 3 3 3 3 3 3 3  3
    
    15221390  .05252532  .03525682 .035140626  .03509748 .10457812 1 1 3 3 3 3 3 3 3  3
    
    15222650  .04067245  .03767105  .03023052  .05176309  .1597672 1 1 3 3 3 3 6 6 6  6
    
    15224200  .02543188  .03883823    .061365  .05290002  .3185767 1 1 1 1 5 1 5 1 5 10
    
    15224210  .02434349   .0534762  .05354432  .05040378  .3804943 1 1 1 1 5 5 5 5 9  9
    
    15226410  .03198881  .03558574  .04239853  .05716623 .29128334 1 1 3 1 1 1 1 1 1  5
    
    15227620  .02938271 .031873442  .05473233  .05559037 .11161292 1 1 3 3 3 3 3 3 3  3
    
    15227660  .02103754 .027261456 .036936503  .07912432  .1936669 1 1 3 3 3 6 6 6 6  8
    
    15227800 .033216402 .029598124  .04108968  .05975731  .3951791 1 1 1 1 5 5 4 5 9  9
    
    15228000 .030896673 .034683917   .0473326  .05626627  .4592344 1 2 1 4 4 5 4 4 4  4
    
    15228200 .032206327  .04869044  .04972863  .04520955  .4869684 1 2 1 4 4 4 4 4 4  4
    
    15302600  .02760275  .03894076  .05234834  .05520269 .17479844 1 1 3 3 3 3 6 6 6  6
    
    15304700 .030649504  .04403342  .04844512  .05060576 .34572035 1 1 1 1 5 1 5 1 5 10
    
    15304710  .02304187  .04814539  .04926133  .05757089 .34702775 1 1 1 1 5 1 5 1 5 10
    
    15307000  .02781314  .05424609  .05354634  .04550172  .1662198 1 1 3 3 3 3 6 6 6  6
    
    15307600 .027197136  .04666263 .064587995  .04431761 .31376365 1 1 1 1 5 1 5 1 5 10
    
    15308160 .025448374  .04638337  .05707046  .05102134 .29608256 1 1 3 1 1 1 1 1 1  5
    
    15310330 .037051972   .0418521   .0440039  .04615832 .11954229 1 1 3 3 3 3 3 3 3  3
    
    15310340  .03469745  .02506317  .04657263  .05731139 .12348424 1 1 3 3 3 3 3 3 3  3
    
    15314010 .031904735 .027626395  .03891844  .06381115  .2587303 1 1 3 1 1 6 1 8 1  1
    
    15314020  .02650743   .0417422  .04635015  .05843768 .18425795 1 1 3 3 3 6 6 6 6  8
    
    15314030   .0374409  .03608905  .04553516  .04807644 .13647024 1 1 3 3 3 3 3 3 3  3
    
    15314050  .03396099  .03302645  .04254599  .05600876  .1768054 1 1 3 3 3 3 6 6 6  6
    
    15314900 .031465095  .03741217  .05160815  .05153327  .3672676 1 1 1 1 5 5 5 5 9  9
    
    15315100  .02753504  .04663713  .04603246   .0545009  .3582725 1 1 1 1 5 5 5 5 5 10
    
    15316210  .02626883  .03883746  .04759697  .05969183  .2797991 1 1 3 1 1 1 1 8 1  5
    
    15316600 .023747684  .04830111  .05465997   .0534895 .23978594 1 1 3 3 1 6 1 8 8  1
    
    15328400  .02772328  .03037119  .04871125  .06202296  .4104678 1 1 1 1 5 5 4 5 9  9
    
    15328410  .03225486  .03484228  .04935026  .05327659  .4301646 1 2 1 4 5 5 4 5 9  9
    
    15336010  .02688133  .04639446   .0467752  .05505563 .26562417 1 1 3 1 1 1 1 8 1  5
    
    15336040  .02787359 .036560908   .0448806  .06048166 .22354685 1 1 3 3 1 6 6 8 8  1
    
    15337610  .02768893  .04597757  .03921753  .05857417 .20083825 1 1 3 3 3 6 6 6 6  8
    
    15337620   .0344498   .0277405  .03933028  .06023838  .3021616 1 1 3 1 1 1 1 1 1  5
    
    15337660 .034423746 .031399023 .037188616   .0594051 .21090555 1 1 3 3 1 6 6 6 8  8
    
    15337860   .0300364   .0421133  .04942461  .05193154   .182705 1 1 3 3 3 6 6 6 6  8
    
    15341500  .02875146  .06676867  .04571384  .04161525  .1988762 1 1 3 3 3 6 6 6 6  8
    
    15344000 .012208587  .03926394  .06914538 .065212205   .659497 1 2 2 4 4 4 2 2 2  2
    
    15344600  .03099126  .04727986  .05099335  .04685511 .24548885 1 1 3 3 1 6 1 8 8  1
    
    15347400 .029063804  .03731951  .04834737  .05653689  .7652071 1 2 2 2 2 4 2 2 2  2
    
    15347410 .018458106  .04698581  .06109795  .05736315  .4732007 1 2 1 4 4 5 4 4 4  4
    
    15400100 .029964086  .03929965   .0447981  .05627603  .3156581 1 1 1 1 5 1 5 1 5 10
    
    15400200 .035897676  .03732134  .04461002  .04988507 .25058717 1 1 3 3 1 6 1 8 8  1
    
    15400500 .023093125   .0506128  .05129687  .05493188  .3892962 1 1 1 1 5 5 4 5 9  9
    
    15402300 .025029905  .04676428  .05356009  .05334762  .3264055 1 1 1 1 5 1 5 1 5 10
    
    15403400 .031229144  .04206314  .04904048  .05064617  .4860765 1 2 1 4 4 4 4 4 4  4
    
    15403950  .03007495  .05782443  .05102915   .0419873 .23120515 1 1 3 3 1 6 1 8 8  1
    
    15404350  .03372863   .0386604  .04899151  .04940496   .488883 1 2 1 4 4 4 4 4 4  4
    
    15404750  .02891766  .04495323  .05497454  .04857571 .26337236 1 1 3 1 1 1 1 8 1  5
    
    15404800 .030232877  .03538747  .05418892  .05279979  .3300436 1 1 1 1 5 1 5 1 5 10
    
    15405200  .02175255  .04134126  .06375925  .05483215 .28678185 1 1 3 1 1 1 1 1 1  5
    
    15405500  .03410082  .03280187 .035724543  .05985528 .38087445 1 1 1 1 5 5 5 5 9  9
    
    15406260 .018312199  .04860537  .07188654  .05046037  .3333767 1 1 1 1 5 1 5 1 5 10
    
    15406270  .02392641  .02343402  .05287202  .06849116 .23228675 1 1 3 3 1 6 1 8 8  1
    
    15406280  .04061633 .034062974  .04212404  .04710072  .1691886 1 1 3 3 3 3 6 6 6  6
    
    15407010  .01569861  .04161722 .068562545  .05971335 .32061505 1 1 1 1 5 1 5 1 5 10
    
    15407070 .025452597   .0302059  .05499605  .06144555 .20845804 1 1 3 3 3 6 6 6 8  8
    
    15412500 .028118705  .04921112  .04788784   .0512194   .216717 1 1 3 3 1 6 6 6 8  8
    
    15414630 .033400092 .032076877  .04570103  .05546965 .17627287 1 1 3 3 3 3 6 6 6  6
    
    15415020  .02889109  .04442965  .05138792  .05095856  .1702276 1 1 3 3 3 3 6 6 6  6
    
    15416700 .036950205  .03735865  .03882569  .05181583 .56499135 1 2 1 4 4 4 2 2 2  2
    
    15417940  .02245741  .04371686  .05208512  .05923934  .4229427 1 2 1 1 5 5 4 5 9  9
    
    15419510  .02830568 .036637492  .05468416  .05428033  .4646525 1 2 1 4 4 5 4 4 4  4
    
    15422500 .025564494 .032177508  .05437494  .06052997  .1883459 1 1 3 3 3 6 6 6 6  8
    
    15422520 .028461317 .021228384  .04005329  .07124592 .15422384 1 1 3 3 3 3 3 6 6  6
    
    15422550  .02348976  .04876135  .04653764  .05819945  .4691559 1 2 1 4 4 5 4 4 4  4
    
    15422590  .03053169 .034362853  .05210266  .05419325  .1725324 1 1 3 3 3 3 6 6 6  6
    
    15426720  .02968101  .03942148   .0375687  .06070146  .2412916 1 1 3 3 1 6 1 8 8  1
    
    15430300  .04230434   .0360676 .033395357  .04877272 .21842062 1 1 3 3 1 6 6 6 8  1
    
    15430320  .03776399  .02820253  .04375992  .05318205  .1659682 1 1 3 3 3 3 6 6 6  6
    
    15431210  .02564985   .0393334  .05004849   .0588034  .6002723 1 2 1 4 4 4 2 2 2  2
    
    15431300 .027341293  .03900664  .05244322  .05544699  .3099915 1 1 1 1 5 1 5 1 5 10
    
    15501190 .019712787  .05064317  .06331743  .05239179  .4165552 1 1 1 1 5 5 4 5 9  9
    
    15501210  .02684342  .03992781  .05009848  .05690058 .35191795 1 1 1 1 5 1 5 1 5 10
    
    end

  • #2
    Hi Valentin,

    -read.dta()- is not a valid -{haven}- function (at least as of haven v.2.5.1).

    Try -read_dta()- instead.

    -rsource- does not report back on errors during runtime by default but will just terminate the R process silently.

    - joe
    Joseph Nicholas Luchman, Ph.D., PStat® (American Statistical Association)
    ----
    Research Fellow
    Fors Marsh

    ----
    Version 18.0 MP

    Comment

    Working...
    X