--- title: "Live Code Execution with typeRun" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Live Code Execution with typeRun} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = FALSE # Don't actually run typeRun in vignette ) ``` ```{r setup} library(typeR) ``` ## Introduction `typeRun()` is an enhanced version of `typeR()` that not only simulates typing animation but also **executes your R code in real-time**. This makes it perfect for: - ๐ŸŽ“ Live teaching and workshops - ๐ŸŽค Conference presentations - ๐Ÿ“น Recording tutorials - ๐Ÿงช Interactive demonstrations ## Basic Usage ### Simple Script Execution The most basic usage is to run an R script with typing animation and live execution: ```{r basic} # Create a simple script cat("# Data Analysis Demo x <- 1:10 mean(x) sum(x) ", file = "demo.R") # Type and execute it typeRun("demo.R") ``` **What you'll see:** - Code types out character by character - Each line executes as it's typed - Results appear immediately after each command ### Comparison: typeR vs typeRun ```{r comparison} # typeR: Just displays, doesn't execute typeR("demo.R") # Output: Shows the code typing out (no results) # typeRun: Types AND executes typeRun("demo.R") # Output: Shows code typing + execution results ``` ## Customizing the Experience ### Control Typing Speed ```{r speed} # Slower typing for dramatic effect typeRun("demo.R", delay = 0.1, jitter = 0.02) # Faster typing for quick demos typeRun("demo.R", delay = 0.02, jitter = 0.005) ``` **Parameters:** - `delay`: Base time between characters (seconds) - `jitter`: Random variation for natural feel ### Limit Output Length For long outputs, control how much is displayed: ```{r output-control} cat(" # Long vector long_vec <- 1:1000 long_vec # Large data frame big_df <- mtcars[rep(1:32, 10), ] big_df ", file = "long_output.R") # Show only first 5 elements/rows typeRun("long_output.R", max_print = 5) ``` ## Working with Models ### Linear Models ```{r lm} cat(" # Linear regression model <- lm(mpg ~ hp + wt, data = mtcars) summary(model) ", file = "model_demo.R") typeRun("model_demo.R") ``` **What happens:** - `model <- lm(...)` executes silently (no output) - `summary(model)` displays the full summary with coefficients, R-squared, etc. ### GLM and Other Models `typeRun()` handles all model types intelligently: ```{r models} cat(" # Logistic regression glm_model <- glm(am ~ hp + wt, data = mtcars, family = binomial) summary(glm_model) # Poisson regression pois_model <- glm(carb ~ mpg, data = mtcars, family = poisson) summary(pois_model) ", file = "glm_demo.R") typeRun("glm_demo.R") ``` ## Interactive Control ### Pause and Resume During execution, you have full control: 1. **Press ESC** (or Ctrl+C) to pause 2. You'll see: `Enter choice (1 resume or 2 stop):` 3. **Enter 1** to continue from where you paused 4. **Enter 2** to stop completely ```{r interactive, eval=FALSE} typeRun("long_script.R") # Press ESC while running # Enter 1 to resume # Or enter 2 to stop ``` This is perfect for: - Answering questions during presentations - Explaining specific code sections - Debugging during demonstrations ## Working with R Markdown and Quarto ### R Markdown Files `typeRun()` intelligently handles `.Rmd` files: ```{r} # Create an R Markdown example typeRun("report.Rmd") ``` **Behavior:** - โœ… Types and shows all text - โœ… Executes only R code chunks - โœ… Skips YAML headers - โœ… Preserves narrative flow ### Quarto Documents Works identically with `.qmd` files: ```{r qmd, eval=FALSE} typeRun("analysis.qmd") ``` ## Advanced Features ### Custom Evaluation Environment Run code in an isolated environment to keep your workspace clean: ```{r env} # Create custom environment my_env <- new.env() # Run in that environment typeRun("demo.R", envir = my_env) # Check what was created ls(my_env) ls() # Your global environment is unchanged ``` ### Suppressing Library Messages Package loading messages are automatically suppressed: ```{r library} cat(" library(ggplot2) # No startup message shown library(dplyr) # Clean output # But code works normally mtcars %>% head() ", file = "packages.R") typeRun("packages.R") # Shows only the actual results, not package messages ``` --- ## Output Handling Details ### What Executes Silently These don't produce output (cleaner demos): ```{r silent} cat(" # Assignments - silent x <- 1:10 y <- mean(x) # Plots - execute but don't clutter console plot(x, y) hist(x) # Package loading - no startup messages library(stats) ", file = "silent.R") typeRun("silent.R") ``` ### What Shows Output ```{r visible} cat(" # Direct values 1:10 # Shows the vector # Function calls mean(1:10) # Shows: [1] 5.5 # Print statements print('Hello') # Shows: [1] 'Hello' # Model summaries summary(lm(mpg ~ hp, data = mtcars)) # Shows full summary ", file = "visible.R") typeRun("visible.R") ``` --- ## Practical Examples ### Teaching Linear Regression ```{r teaching} cat(" # Load data data(mtcars) head(mtcars, 3) # Visualize relationship plot(mtcars$hp, mtcars$mpg, xlab = 'Horsepower', ylab = 'Miles per Gallon', main = 'MPG vs Horsepower') # Fit model model <- lm(mpg ~ hp, data = mtcars) summary(model) # Add regression line abline(model, col = 'red', lwd = 2) # Predictions new_data <- data.frame(hp = c(100, 150, 200)) predict(model, new_data) ", file = "teaching_demo.R") typeRun("teaching_demo.R", delay = 0.08) ``` ### Data Analysis Workflow ```{r workflow} cat(" # 1. Load and explore data <- iris str(data) # 2. Summary statistics summary(data) # 3. Visualization boxplot(Sepal.Length ~ Species, data = data, main = 'Sepal Length by Species', col = c('lightblue', 'lightgreen', 'pink')) # 4. Statistical test aov_result <- aov(Sepal.Length ~ Species, data = data) summary(aov_result) # 5. Post-hoc test TukeyHSD(aov_result) ", file = "analysis_demo.R") typeRun("analysis_demo.R", delay = 0.06, max_print = 8) ``` ### GLM Demonstration ```{r glm-example} cat(" # Binary outcome: Manual transmission (am) # Predictors: HP and weight # Fit logistic regression logit_model <- glm(am ~ hp + wt, data = mtcars, family = binomial(link = 'logit')) # Model summary summary(logit_model) # Odds ratios exp(coef(logit_model)) # Predicted probabilities mtcars$pred_prob <- predict(logit_model, type = 'response') head(mtcars[, c('am', 'hp', 'wt', 'pred_prob')]) ", file = "glm_example.R") typeRun("glm_example.R", max_print = 6) ``` --- ## Tips and Best Practices ### 1. Prepare Your Script Before presenting: ```r # Test your script normally first source("demo.R") # Then test with typeRun typeRun("demo.R") ``` ### 2. Timing Considerations ```r # For 5-minute demos: ~0.05 delay typeRun("demo.R", delay = 0.05) # For dramatic reveals: ~0.1 delay typeRun("demo.R", delay = 0.1) # For quick reviews: ~0.02 delay typeRun("demo.R", delay = 0.02) ``` ### 3. Output Management ```r # Lots of output? Limit it typeRun("demo.R", max_print = 5) # Models in script? They'll show nicely # Plots in script? They'll render silently ``` ### 4. Keep Scripts Focused ```r # Good: One concept per script typeRun("01_data_loading.R") typeRun("02_visualization.R") typeRun("03_modeling.R") # Less good: One giant script typeRun("everything.R") # Too long, hard to control ``` --- ## Troubleshooting ### Script Not Found ```r # Error: The file does not exist typeRun("missing.R") # Solution: Check file path file.exists("demo.R") list.files(pattern = "\\.R$") ``` ### Unexpected Output ```r # If output looks wrong, test the script normally first source("demo.R") # Then try with typeRun typeRun("demo.R") ``` ### Want to Stop Mid-Execution ```r # Press ESC, then enter 2 # Or press Ctrl+C (on some systems) ``` --- ## Comparison Table | Feature | `typeR()` | `typeRun()` | |---------|-----------|-------------| | Typing animation | โœ… | โœ… | | Code execution | โŒ | โœ… | | Shows output | โŒ | โœ… | | Interactive pause/resume | โŒ | โœ… | | Output truncation | โŒ | โœ… | | Custom environment | โŒ | โœ… | | Model summary handling | N/A | โœ… | | Library message suppression | N/A | โœ… | --- ## See Also - `?typeR` - Basic typing animation without execution - `?typeRun` - Full function documentation - [Package website](https://Fgazzelloni.github.io/typeR/) --- ## Cleanup ```{r cleanup, include=FALSE} # Remove demo files created in this vignette files_to_remove <- c( "demo.R", "long_output.R", "model_demo.R", "glm_demo.R", "report.Rmd", "packages.R", "silent.R", "visible.R", "teaching_demo.R", "analysis_demo.R", "glm_example.R" ) invisible(lapply(files_to_remove, function(f) { if (file.exists(f)) file.remove(f) })) ```