How to use Python block in R markdown? - r

I am trying to use a Python block in my R markdown document. Below is what I have been trying:
"""{r setup, include=FALSE}
require(reticulate)
use_python("/Users/hyunjindominiquecho/opt/anaconda3", required = T)
"""
"""{python}
# import the necessary python packages
import numpy as np
import pandas as pd
import scipy.stats as st
from rpy2 import robjects
import math
from scipy.optimize import newton
import torch
from pandas import dataframe
from statistics import mean
"""
but when I try to do this, R studio shows the following error:
Error in system2(command = python, args = paste0("\"", config_script, :
error in running command
How can I resolve this issue? Thank you,

What if you did:
```{python, engine.path = '/Users/hyunjindominiquecho/opt/anaconda3'}
import sys
print(sys.version)
```
What happens when you only run the first chunk? Do you still get the same error?

Related

Cannot import specific function in Jupyter notebook, however, some function in the same "~.py" can be imported

I have utils.py and there are two functions: runrealcmd, mol_with_atom_index
If I try to import the two function with the following code:
from utils import mol_with_atom_index, runrealcmd
It fails to import runrealcmd. The error message is like below:
ImportError: cannot import name 'runrealcmd' from 'utils'
However, if I try to import only the mol_with_atom_index with the following code:
from utils import mol_with_atom_index
It successes. The function of mol_with_atom_index can be imported in Jupyter notebook.
However, the function of runrealcmd cannot be imported in Jupyter notebook although both of the two functions are in the same utils.py file.
ImportError: cannot import name 'runrealcmd' from 'utils'
utils.py
from subprocess import Popen, PIPE, STDOUT
from IPython.core.magic import register_line_magic
#register_line_magic
def runrealcmd(command):
# Display instantly: https://stackoverflow.com/questions/52545512/realtime-output-from-a-shell-command-in-jupyter-notebook
process = Popen(command, stdout=PIPE, shell=True, stderr=STDOUT, bufsize=1, close_fds=True)
for line in iter(process.stdout.readline, b''):
print(line.rstrip().decode('utf-8'))
process.stdout.close()
process.wait()
def mol_with_atom_index(mol):
for atom in mol.GetAtoms():
atom.SetAtomMapNum(atom.GetIdx())
return mol
(Jupyter notebook) If you want to import the same-named function (previously imported with the same name) from the other path, you should restart the kernel first.

NameError: name 'np' is not defined

I try to import the following functions:
import numpy as np
def load_labels(path):
y = np.load(path)
return y
def print_sentence():
print("hi")
from a Jupyter notebook, with name "save_load" into another Jupyter notebook with the following code:
!pip install import-ipynb
import import_ipynb
import save_load
from save_load import load_labels, print_sentence
The function print_sentence works fine in the notebook, but with the function load_labels I receive the following error:
NameError: name 'np' is not defined
What could be the reason for this error? I've imported numpy as np in both notebooks.
In "save_load" instead of import numpy as np try import numpy, it worked for me.
You can try this:
import numpy as np
or
from numpy import *
I had the same problem when I was fixing my codes on VScode. Try saving the file. Then run it again.

importerror trying to convert pandas dataframe to R data frame

I am attempting to convert some pandas dataframes to R objects for use in rpy2.
I am getting an error when i try to import the conversion the module. from rpy2.robjects import pandas2ri yields ImportError: cannot import name 're_type' as follows:
/opt/conda/lib/python3.6/site-packages/pandas/core/dtypes/inference.py in <module>()
6 from collections import Iterable
7 from numbers import Number
----> 8 from pandas.compat import (PY2, string_types, text_type,
9 string_and_binary_types, re_type)
10 from pandas._libs import lib
ImportError: cannot import name 're_type'
I haven't really seen any discussion of this error elsewhere.
in terms of dependencies, i am using pandas 0.23.4, rpy2 2.9.4, R 3.4, and it is running on the docker jupyter container datascience-notebook
Really hoping someone could help me here!

linear_model.py:1283: RuntimeWarning: invalid value encountered in sqrt return rho, np.sqrt(sigmasq)

i have encountered a big problem when I do exercise of time series analysis with plot_ACF and plot_PACF.
when I run my code, it always gives me warning like this:
linear_model.py:1283: RuntimeWarning: invalid value encountered in
sqrt
return rho, np.sqrt(sigmasq)
and I tried to find solution on Google, but found nothing helpful.
Appreciation if someone could help me fix this warning.
many many thx!
my coding environment: python 3.6.6 win64
statsmodels lib used in my laptop:
statsmodels-0.9.0-cp36-cp36m-win_amd64
and my source code is listed below:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf
dta=[10930,10318,10595,10972,7706,6756,9092,10551,9722,10913,11151,8186,6422,
6337,11649,11652,10310,12043,7937,6476,9662,9570,9981,9331,9449,6773,6304,9355,
10477,10148,10395,11261,8713,7299,10424,10795,11069,11602,11427,9095,7707,10767,
12136,12812,12006,12528,10329,7818,11719,11683,12603,11495,13670,11337,10232,
13261,13230,15535,16837,19598,14823,11622,19391,18177,19994,14723,15694,13248,
9543,12872,13101,15053,12619,13749,10228,9725,14729,12518,14564,15085,14722,
11999,9390,13481,14795,15845,15271,14686,11054,10395]
dta_list = np.arange(2001,2091)
data=pd.DataFrame(dta,index=dta_list)
data.plot()
plt.show()
D_data=data.diff(1).dropna()
D_data.plot()
plt.show()
plot_acf(D_data).show()
plot_pacf(D_data).show()

Using R's arima function in python with rpy2

I am using rpy2 to call R functions in python3.4 and I'm struggling with calling the arima function.
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
import pandas as pd
from rpy2.robjects import pandas2ri
ts=robjects.r('ts')
forecast=importr('forecast')
pandas2ri.activate()
traindf=pd.read_csv('MARUTI.NS.csv',index_col=0)
traindf.index=traindf.index.to_datetime()
rdata=ts(traindf.Close,frequency=1)
fit=forecast.arima(rdata,c=(1,0,0)) # error occurs here
forecast_output=forecast.forecast(fit,h=4,level=(95.0))
print(forecast_output)
Error:
AttributeError: 'InstalledSTPackage' object has no attribute 'arima'.
This error meant that there was no ARIMA function in forecast. You can find it in an R environment using ?ARIMA
The forecast object is forecast.auto_arima. Alternatively, you could import stats and run the arima.
stats = importr("stats")
fit=stats.arima(rdata,c=(1,0,0)) \

Resources