This is continued from SDS Project Setup.
Earlier, all the three languages were working together, but the values did not transfer across code blocks. For the different blocks to be able to communicate with each other, the language needs to run in the same session. Setting this up for Python and R was straightforward, but Julia had me stuck for a long time.
The dataset used below is available at Allison Horst’s page.
For Python and R
It was a straightforward matter for Python and R. Note the header.
#+title: Exploratory Data Analysis
#+author:
#+property: header-args:python :session *python-eda* :results output
#+property: header-args:R :session *r-eda* :results output
#+property: header-args:julia :session *julia-eda*
The first property line specifies that if a source block has Python, use the Python session python-eda and give the output as result. (By default, value is provided as result instead.) See Computational Notebook Setup for more.
For R, the setup is the same. The only added step is that when the first code block is run, you have to set SDS as the location for the project.
Do C-c C-c on each property line to refresh the local setup. (Do this at the beginning.) The run the sds functions (my/sds-python, my/sds-r as in SDS Project Configuration 1 ) to configure Babel to activate the environments.
After this, running the Python and R code blocks should produce the results shown in the appendix.
For Julia
The Babel backend for Julia turned out to be unreliable. It got stuck during evaluations, forcing me to exit every time.
The fix was to use Julia-vterm instead. I need the following modification in my-org.el in my Emacs config.
(use-package julia-vterm
:ensure t)
(use-package ob-julia-vterm
:ensure t
:after (org julia-vterm)
:config
;; Make normal "julia" blocks use the vterm backend
(defalias 'org-babel-execute:julia
'org-babel-execute:julia-vterm)
(defalias 'org-babel-variable-assignments:julia
'org-babel-variable-assignments:julia-vterm)
(org-babel-do-load-languages
'org-babel-load-languages
'((python . t)
(julia-vterm . t) ; the actual backend
(R . t)
;; do NOT put (julia . t) here if you want pure vterm
)))
The function my/sds-julia in my-functions.el also had to be modified.
(defun my/sds-julia ()
(interactive)
(setq julia-vterm-repl-program
"julia --project=/home/nes/Documents/Projects/SDS/Julia")
(message "julia-vterm: SDS project activated"))
Now, Julia works, and the correct environment gets utilized. Care must be taken to ensure that a different Julia session is not running and has taken over the operations in the relevant org file.
Appendix: The File In Which I Tested Sessions
This might be too much to put here, but I want a detailed snapshot of what worked.
Note: See SDS Project Configuration 5 for a more concise file which tests more aspects of the necessary setup.
#+title: Exploratory Data Analysis
#+author:
#+property: header-args:python :session *python-eda* :results output
#+property: header-args:R :session *r-eda* :results output
#+property: header-args:julia :session *julia-eda*
* Palmer Penguins
** Loading the data
*** Python
#+begin_src python
import pandas as pd
penguins = pd.read_csv("/home/deltamagna/Documents/Projects/SDS/Data/Raw/penguins.csv")
pd.set_option("display.precision", 2)
#+end_src
#+RESULTS:
#+begin_src python
print(penguins.info())
print("\n First 5 Records: \n", penguins.head())
print("\n Summary: \n", penguins.describe())
#+end_src
#+RESULTS:
#+begin_example
<class 'pandas.DataFrame'>
RangeIndex: 344 entries, 0 to 343
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 species 344 non-null str
1 island 344 non-null str
2 bill_length_mm 342 non-null float64
3 bill_depth_mm 342 non-null float64
4 flipper_length_mm 342 non-null float64
5 body_mass_g 342 non-null float64
6 sex 333 non-null str
7 year 344 non-null int64
dtypes: float64(4), int64(1), str(3)
memory usage: 21.6 KB
None
First 5 Records:
species island bill_length_mm ... body_mass_g sex year
0 Adelie Torgersen 39.1 ... 3750.0 male 2007
1 Adelie Torgersen 39.5 ... 3800.0 female 2007
2 Adelie Torgersen 40.3 ... 3250.0 female 2007
3 Adelie Torgersen NaN ... NaN NaN 2007
4 Adelie Torgersen 36.7 ... 3450.0 female 2007
[5 rows x 8 columns]
Summary:
bill_length_mm bill_depth_mm ... body_mass_g year
count 342.00 342.00 ... 342.00 344.00
mean 43.92 17.15 ... 4201.75 2008.03
std 5.46 1.97 ... 801.95 0.82
min 32.10 13.10 ... 2700.00 2007.00
25% 39.23 15.60 ... 3550.00 2007.00
50% 44.45 17.30 ... 4050.00 2008.00
75% 48.50 18.70 ... 4750.00 2009.00
max 59.60 21.50 ... 6300.00 2009.00
[8 rows x 5 columns]
#+end_example
*** R
#+begin_src R
penguins <- read.csv("/home/deltamagna/Documents/Projects/SDS/Data/Raw/penguins.csv")
options(digits = 4)
#+end_src
#+RESULTS:
#+begin_src R
str(penguins)
#+end_src
#+RESULTS:
: 'data.frame': 344 obs. of 8 variables:
: $ species : chr "Adelie" "Adelie" "Adelie" "Adelie" ...
: $ island : chr "Torgersen" "Torgersen" "Torgersen" "Torgersen" ...
: $ bill_length_mm : num 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
: $ bill_depth_mm : num 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
: $ flipper_length_mm: int 181 186 195 NA 193 190 181 195 193 190 ...
: $ body_mass_g : int 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
: $ sex : chr "male" "female" "female" NA ...
: $ year : int 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...
#+begin_src R
head(penguins)
#+end_src
#+RESULTS:
: species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
: 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007
: 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007
: 3 Adelie Torgersen 40.3 18.0 195 3250 female 2007
: 4 Adelie Torgersen NA NA NA NA <NA> 2007
: 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007
: 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007
#+begin_src R
summary(penguins)
#+end_src
#+RESULTS:
: species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
: Length :344 Length :344 Min. :32.1 Min. :13.1 Min. :172 Min. :2700 Length :344 Min. :2007
: N.unique : 3 N.unique : 3 1st Qu.:39.2 1st Qu.:15.6 1st Qu.:190 1st Qu.:3550 N.unique : 2 1st Qu.:2007
: N.blank : 0 N.blank : 0 Median :44.5 Median :17.3 Median :197 Median :4050 N.blank : 0 Median :2008
: Min.nchar: 6 Min.nchar: 5 Mean :43.9 Mean :17.2 Mean :201 Mean :4202 Min.nchar: 4 Mean :2008
: Max.nchar: 9 Max.nchar: 9 3rd Qu.:48.5 3rd Qu.:18.7 3rd Qu.:213 3rd Qu.:4750 Max.nchar: 6 3rd Qu.:2009
: Max. :59.6 Max. :21.5 Max. :231 Max. :6300 NAs : 11 Max. :2009
: NAs :2 NAs :2 NAs :2 NAs :2
*** Julia
#+begin_src julia
#versioninfo()
Base.active_project()
#pwd()
#+end_src
#+RESULTS:
: /home/deltamagna/Documents/Projects/SDS/Julia/Project.toml
#+begin_src julia
using CSV
using DataFrames
penguins = CSV.read(
"/home/deltamagna/Documents/Projects/SDS/Data/Raw/penguins.csv",
DataFrame
)
#+end_src
#+RESULTS:
#+begin_example
344×8 DataFrame
Row │ species island bill_length_mm bill_depth_mm flipper_length_mm ⋯
│ String15 String15 String7 String7 String3 ⋯
─────┼──────────────────────────────────────────────────────────────────────────
1 │ Adelie Torgersen 39.1 18.7 181 ⋯
2 │ Adelie Torgersen 39.5 17.4 186
3 │ Adelie Torgersen 40.3 18 195
4 │ Adelie Torgersen NA NA NA
5 │ Adelie Torgersen 36.7 19.3 193 ⋯
6 │ Adelie Torgersen 39.3 20.6 190
7 │ Adelie Torgersen 38.9 17.8 181
8 │ Adelie Torgersen 39.2 19.6 195
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱
338 │ Chinstrap Dream 46.8 16.5 189 ⋯
339 │ Chinstrap Dream 45.7 17 195
340 │ Chinstrap Dream 55.8 19.8 207
341 │ Chinstrap Dream 43.5 18.1 202
342 │ Chinstrap Dream 49.6 18.2 193 ⋯
343 │ Chinstrap Dream 50.8 19 210
344 │ Chinstrap Dream 50.2 18.7 198
3 columns and 329 rows omitted
#+end_example
#+begin_src julia
first(penguins, 5)
#+end_src
#+RESULTS:
#+begin_example
5×8 DataFrame
Row │ species island bill_length_mm bill_depth_mm flipper_length_mm ⋯
│ String15 String15 String7 String7 String3 ⋯
─────┼──────────────────────────────────────────────────────────────────────────
1 │ Adelie Torgersen 39.1 18.7 181 ⋯
2 │ Adelie Torgersen 39.5 17.4 186
3 │ Adelie Torgersen 40.3 18 195
4 │ Adelie Torgersen NA NA NA
5 │ Adelie Torgersen 36.7 19.3 193 ⋯
3 columns omitted
#+end_example
#+begin_src julia
describe(penguins)
#+end_src
#+RESULTS:
#+begin_example
8×7 DataFrame
Row │ variable mean min median max nmissing eltype ⋯
│ Symbol Union… Any Union… Any Int64 DataTy ⋯
─────┼──────────────────────────────────────────────────────────────────────────
1 │ species Adelie Gentoo 0 String ⋯
2 │ island Biscoe Torgersen 0 String
3 │ bill_length_mm 32.1 NA 0 String
4 │ bill_depth_mm 13.1 NA 0 String
5 │ flipper_length_mm 172 NA 0 String ⋯
6 │ body_mass_g 2700 NA 0 String
7 │ sex NA male 0 String
8 │ year 2008.03 2007 2008.0 2009 0 Int64
1 column omitted
#+end_example