![]() Nothing shows up in your RStudio environment pane, and no value is returned. If you run that code in R, it may look like nothing happened. The py_run_string() function executes whatever Python code is within the parentheses and quotation marks. The Python code looks like this: import numpy as np my_python_array = np.array()Īnd here’s one way to do that right in an R script: py_run_string("import numpy as np") py_run_string("my_python_array = np.array()") ![]() To keep things simple, let's start with just two lines of Python code to import the NumPy package for basic scientific computing and create an array of four numbers. If you'd like to follow along, install and load reticulate with install.packages("reticulate") and library(reticulate). ![]() You also need any Python modules, packages, and files your Python code depends on. In addition to reticulate, you need Python installed on your system. Thanks to the R reticulate package, you can run Python code right within an R script-and pass data back and forth between Python and R. Or an API you want to access that has sample code in Python but not R. Maybe it’s a great library that doesn’t have an R equivalent (yet). And there can be good reasons an R user would want to do some things in Python. ![]() ![]() As much as I love R, it’s clear that Python is also a great language-both for data science and general-purpose computing. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
May 2023
Categories |