How to use R in Jupyter Notebook and How to use Python in RStudio
Yes, I know we should use R in RStudio and Python in Jupyter Notebook. But just for FUN!
Assume you have already installed anaconda environment and jupyter notebook in your computer. If not, go to [https://www.anaconda.com/download/] or google how to install anaconda and jupyter notebook. The following guide is made in Windows 10, it might be different for other systems.
Step 1: Open an anaconda prompt https://github.com/Inhenn/RStuidoJupyterNotebook/blob/master/10.png
Step 2: In the directory you desired, type "conda install -c r r-irkernel" to install irkernel. (Referring to [https://irkernel.github.io/] )
Yes Yes Yes!!!
Kernel Installed
Step 4: If you launch jupyter notebook, you will see a new kernel called r
Step 5: Now, we need to go back and install some packages. In anaconda prompt, type "r" to launch R, and type "install.packages(" packages you want to install ")", select a CRAN mirror. (Referring to [https://irkernel.github.io/installation/])
Step 6: Now launch jupyter notebook again, and open a new file in "r", we get our first Notebook in R
Assume you have also installed R and Rstudio in your computer, if not go to download R and Rstudio [https://www.rstudio.com/products/rstudio/download/]. Also you need to have anaconda and python in your computer.
Step 1:Launch Rstudio, open a new script, install the package "reticulate"
Step 2: Type
library(reticulate)
use_condaenv("C:/Software/PythonAnaconda/envs")
#use_python("/usr/local/bin/python")
#use_virtualenv("~/myenv")
py_config()
print("hello world")
Use the library, specify the python path or conda environment or virtual env. Here I use my condaenv as an example. If you do not know where is your python or conda environment, open a jupyter notebook and type
import os
import sys
os.path.dirname(sys.executable)
Then, do py_config in Rstudio, and we are in the python environment!!!
We can also run python in a markdown environment, by doing the same thing. Initialize python in the first chunk. In the second chunk, we can directly use
` ```{python}
` ```
and write python code. The nice thing about markdown file is that we have colors in variable names and functions just like a normal python editor. (Refering to [https://cran.r-project.org/web/packages/reticulate/vignettes/r_markdown.html])