Like Example #1, but modified for the following reasons: (1) exploration of layouts (2) testing the facility and functionality to mashup Python and R code and to access and mutate the same variables and data structures (3) main plot of GDP and Life Expectancy is more easily interpreted
This is a demonstration of the facility to access variables and data structures created in Python with R code. The head of a data frame is printed using both languages whilst the number of rows is set by a variable in Python and read in R. Note that the row indexes differ by 1 (0 and 1 based) and that the precision of printing is set differently. Note also, that R prints an additional line of data; the reason is that the code increments a python variable in R i.e. the code performs a mutability test and it works.
country continent year lifeExp pop gdpPercap iso_alpha \
0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG
1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG
2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG
iso_num pop_log10
0 4 6.925587
1 4 6.965716
2 4 7.011447
country continent year lifeExp pop gdpPercap iso_alpha iso_num
1 Afghanistan Asia 1952 28.801 8425333 779.4453 AFG 4
2 Afghanistan Asia 1957 30.332 9240934 820.8530 AFG 4
3 Afghanistan Asia 1962 31.997 10267083 853.1007 AFG 4
4 Afghanistan Asia 1967 34.020 11537966 836.1971 AFG 4
pop_log10
1 6.925587
2 6.965716
3 7.011447
4 7.062129
---
title: "Example #2"
format: dashboard
---
```{r}
# use of reticulate allows variables created in python
# to be accessed by R
library(reticulate)
```
```{python}
# comments
import pandas as pd
import math
import plotly.express as px
df = px.data.gapminder()
# this works but probably a better way
# it would be much easier in R for example
df['pop_log10'] = df['pop']
df['pop_log10'] = df['pop_log10'].map(lambda pop_log10: math.log10(pop_log10) )
# could be used to render size differently
```
# GDP and Life Expectancy
## Row {height="20%"}
Like Example #1, but modified for the following reasons: (1) exploration of layouts (2) testing the facility and functionality to
mashup Python and R code and to access and mutate the same variables and data structures (3) main plot of GDP and Life Expectancy
is more easily interpreted
## Row {height="80%"}
```{python}
#| title: GDP and Life Expectancy
px.scatter(
df, x="gdpPercap", y="lifeExp",
animation_frame="year", animation_group="country",
size="pop", color="continent", hover_name="country",
log_x=False, size_max=35,
range_x=[0,50000], range_y=[25,85]
)
```
# Population and Life Expectancy
## Row {.tabset}
```{python}
#| title: Population
px.area(
df, x="year", y="pop",
color="continent", line_group="country"
)
```
```{python}
#| title: Life Expectancy
px.line(
df, x="year", y="lifeExp",
color="continent", line_group="country"
)
```
# Data
This is a demonstration of the facility to access variables and data structures created in Python with R code. The head of a data
frame is printed using both languages whilst the number of rows is set by a variable in Python and read in R. Note that the row
indexes differ by 1 (0 and 1 based) and that the precision of printing is set differently. Note also, that R prints an additional
line of data; the reason is that the code increments a python variable in R i.e. the code performs a mutability test and it works.
## Row
```{python}
#| title: Head of python pandas data frame
n_head = 3
with pd.option_context(
'display.max_columns', 10,
'display.precision', 6,
):
print(df.head(n_head))
```
## Row
```{r}
#| title: The head of the same data frame but printed with R
# we get access to python variables in R
py$n_head <- py$n_head + 1
head(py$df,py$n_head)
```
# Code
## Row
```{python}
from pathlib import Path
from textwrap import wrap
# wrap just the long lines over a specified number of characters
def wrap_long_lines(input_text, max_line_chars):
lines = input_text.split('\n')
wrapped_lines = []
for line in lines:
if len(line) > max_line_chars:
wrapped_lines.extend(wrap(line, width=max_line_chars))
else:
wrapped_lines.append(line)
wrapped_text = '\n'.join(wrapped_lines)
return wrapped_text
txt = Path('example_2.qmd').read_text()
# its a wrap!
# those long comments are now readable the Code tab
wrapped_txt = wrap_long_lines(txt, 130)
print(wrapped_txt)
```