# Simple example
text <- "Here is some R code:\n```r\nprint('Hello')\n```"
extract_r_code(text)
# Multiple code blocks
response <- "
First block:
```r
x <- 1:10
mean(x)
```
Second block:
```R
library(ggplot2)
ggplot(mtcars, aes(mpg, hp)) + geom_point()
```
"
codes <- extract_r_code(response)
length(codes) # Returns 2
# With surrounding text
llm_response <- "
To calculate the mean, use this code:
```r
data <- c(1, 2, 3, 4, 5)
result <- mean(data)
print(result)
```
The result will be 3.
"
extract_r_code(llm_response)
Run the code above in your browser using DataLab