Commit f392611e authored by JEAN-YVES SGRO's avatar JEAN-YVES SGRO
Browse files

Update 2023-Spring/2023-03-28-session-06/Python_in_RStudio/python-slides.docx,...

Update 2023-Spring/2023-03-28-session-06/Python_in_RStudio/python-slides.docx, 2023-Spring/2023-03-28-session-06/Python_in_RStudio/python-slides.pptx, 2023-Spring/2023-03-28-session-06/Python_in_RStudio/python-slides.pdf, 2023-Spring/2023-03-28-session-06/Python_in_RStudio/python-slides.Rmd, 2023-Spring/2023-03-28-session-06/Python_in_RStudio/python-slides.html
parent b8a0c5c0
Loading
Loading
Loading
Loading
+114 −0
Original line number Diff line number Diff line
---
title: "python-slides"
author: "Jean-Yves Sgro"
date: "`r lubridate::today()`"
output:
  pdf_document: default
  ioslides_presentation:
    fig_caption: no
    fig_height: 5
    fig_width: 7
    out_width: 70%
  powerpoint_presentation: default
  beamer_presentation: default
  slidy_presentation: default
  html_document: default
  word_document: default
---

```{r setup, include=FALSE}
options(digits = 3)
knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  cache = TRUE,
  out.width = "70%",
  fig.align = "center",
  fig.width = 6,
  fig.asp = 0.618,  # 1 / phi
  fig.show = "hold"
)

img_path <- "img"
```


```{r setupx, include=FALSE} 
## general set-up 
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning=FALSE, comment=NA, results = 'asis') 
``` 

## Slide 0

This is a test to see how we can make Python work within RStudio

## Slide 1

This is a test to run python code. Let's test if Python works:

The R package `reticulate` allows R to work with Python.

```{r}
library(reticulate)
```

We can then check which python is running:

```{python}
import sys

print("User Current Version:-", sys.version)
```

## Test 2
A simple Python script to check divisibility by 3.

```{python eval=TRUE}
num = 16
if num % 3 == 0:
  print("is divisible by 3")
else:
  print("is not divisible by 3")
```

## SillyNameGenerator-gpt.py {.smaller}
Testing a Python script generated by ChatGPT. It requires the `termcolor` package that can be installed by `pip` or `pip3`. Run if necessary.

```{bash}
# python3 -V
# pip3 install termcolor
```

Note on code next slide: The interactivity has been disabled otherwise the compilation of slides hangs, waiting for user input!

## Continued - SillyNameGenerator-gpt.py {.smaller}

```{python eval=FALSE}
import random
from termcolor import colored

# Define lists of first names and surnames
first_names = ['John', 'Mary', 'David', 'Emily', 'William']
surnames = ['Smith', 'Johnson', 'Brown', 'Taylor', 'Davis']

# Define a function to play the game
def play_game():
    # Choose a first name at random and assign it to a variable
    first_name = random.choice(first_names)

    # Choose a surname at random and assign it to a variable
    surname = random.choice(surnames)

    # Print the names to the screen in order and in red font
    print(colored(f"{first_name} {surname}", 'red'))

    # Ask the user to quit or play again
    choice = input("Do you want to play again? (y/n)")

    # If user plays again, call the play_game function again
    if choice == 'y':
        play_game()

# Call the play_game function to start the game
play_game()
```
+11.1 KiB

File added.

No diff preview for this file type.

+610 −0

File added.

Preview size limit exceeded, changes collapsed.

+176 KiB

File added.

No diff preview for this file type.

+33 KiB

File added.

No diff preview for this file type.