Use of Python in R

Han Oostdijk

2020/04/18

Date last run: 26Apr2020

Setup chunk

I understood that the setup chunk does not need the library statement for reticulate because it checks if that package is installed. This is not the case, so don’t forget it

library(reticulate)
knitr::opts_chunk$set(echo = TRUE)

Create a testfile

write_testfile <- function(outfile) {
  fo     = file(outfile, open = 'wt')
  on.exit(close(fo), add = T)
  txt = c (
    'dest,carrier,dep_delay,arr_delay' ,
    'ORD,KLM,3,4' ,
    'ORD,KLM,2,5' ,
    'ORD,BEA,2,5' ,
    'PSE,KLM,3,4' ,
    'PSE,KLM,3,4'
  )
  cat(txt,
      sep = "\n",
      file = fo,
      append = F)
}
write_testfile("flights.csv")

Read the testfile in Python

Here I display the chunk as code in the Rmd file:

`​``{python}
import pandas
flights = pandas.read_csv("flights.csv")
flights = flights[flights['dest'] == "ORD"]
flights = flights[['carrier', 'dep_delay', 'arr_delay']]
flights = flights.dropna()
print(flights)

import sys
print(sys.version)
`​``

with result

#>   carrier  dep_delay  arr_delay
#> 0     KLM          3          4
#> 1     KLM          2          5
#> 2     BEA          2          5
#> 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]

Use the Python object in R

Refer to Python object with the prefix py$ :

print(py$flights)
#>   carrier dep_delay arr_delay
#> 0     KLM         3         4
#> 1     KLM         2         5
#> 2     BEA         2         5

Conclusion

And that is it. Only I got a message related to Miniconda that is discussed here.

I will try other interface possibilities between Python and R at a later time.

Currently problem with the other way round:
Jupyter notebook with Python and trying to use R with rpy2 : issue

Session Info

This document was produced on 26Apr2020 with the following R environment:

  #> R version 4.0.0 (2020-04-24)
  #> Platform: x86_64-w64-mingw32/x64 (64-bit)
  #> Running under: Windows 10 x64 (build 18363)
  #> 
  #> Matrix products: default
  #> 
  #> locale:
  #> [1] LC_COLLATE=English_United States.1252 
  #> [2] LC_CTYPE=English_United States.1252   
  #> [3] LC_MONETARY=English_United States.1252
  #> [4] LC_NUMERIC=C                          
  #> [5] LC_TIME=English_United States.1252    
  #> 
  #> attached base packages:
  #> [1] stats     graphics  grDevices utils     datasets  methods   base     
  #> 
  #> other attached packages:
  #> [1] reticulate_1.15
  #> 
  #> loaded via a namespace (and not attached):
  #>  [1] Rcpp_1.0.4.6    lattice_0.20-41 rappdirs_0.3.1  grid_4.0.0     
  #>  [5] jsonlite_1.6.1  magrittr_1.5    evaluate_0.14   rlang_0.4.5    
  #>  [9] stringi_1.4.6   Matrix_1.2-18   tools_4.0.0     stringr_1.4.0  
  #> [13] glue_1.4.0      purrr_0.3.4     xfun_0.13       compiler_4.0.0 
  #> [17] knitr_1.28