Here connect Python to R with the reticulate package. The use_python() function is only needed, in general, for Mac users, to locate the Anaconda distribution of Python and not the pre-installed version on the Mac that is the old version of Python, and does not contain the needed data analysis packages.
The py_config() function is not needed unless there is a problem of R referencing Python. In any case, the function displays the current linked Python configuration, and all available versions of Python found on the computer.
library(reticulate)
use_python("~/anaconda3/bin") # Mac Users only, the ~ means home directory
py_config() # checking to see what Python that R is finding
## python: /Users/davidgerbing/anaconda3/bin/python
## libpython: /Users/davidgerbing/anaconda3/lib/libpython3.7m.dylib
## pythonhome: /Users/davidgerbing/anaconda3:/Users/davidgerbing/anaconda3
## version: 3.7.5 (default, Oct 25 2019, 10:52:18) [Clang 4.0.1 (tags/RELEASE_401/final)]
## numpy: /Users/davidgerbing/anaconda3/lib/python3.7/site-packages/numpy
## numpy_version: 1.17.3
##
## python versions found:
## /Users/davidgerbing/anaconda3/bin/python
## /usr/bin/python
Access the needed Python data analysis libraries for analysis, pandas and numpy, and visualization, seaborn. Define the abbreviations to type less in subsequent calls of functions from these packages.
import pandas as pd
import numpy as np
import seaborn as sns
Some simple Python code just to demonstrate that Python is working with the packages properly imported. The read_excel() function is from the pandas package, so preface with pd., the abbreviation for the pandas package.
d = pd.read_excel('http://lessRstats.com/data/employee.xlsx')
d.head()
## Name Years Gender Dept Salary JobSat Plan Pre Post
## 0 Ritchie, Darnell 7.0 M ADMN 53788.26 med 1 82 92
## 1 Wu, James NaN M SALE 94494.58 low 1 62 74
## 2 Hoang, Binh 15.0 M SALE 111074.86 low 3 96 97
## 3 Jones, Alissa 5.0 F NaN 53772.58 NaN 1 65 62
## 4 Downs, Deborah 7.0 F FINC 57139.90 high 2 90 86