Learn R Programming

TheOpenAIR (version 0.1.0)

extract_r_code: Extract R code and comments from a given input string.

Description

This function takes an input string, detects R code and comments, and returns a character vector containing the R code and comments. The input string is split into lines based on newline characters, and each line is examined for R code and comment patterns. Only the lines that match either of these patterns are returned.

Usage

extract_r_code(input_string)

Value

A character vector containing R code and comment lines extracted from the input string. Each element in the vector corresponds to one line of code or a comment.

Arguments

input_string

A character string containing R code and comments, mixed with other text. The string may contain multiple lines separated by newline characters.

Examples

Run this code
example_string <- 
"This is a text string with R code and comments.\n
# A comment\n
x <- 5\
ny = 10\n
z <- x + y\n
Another line of text."
extract_r_code(example_string)

Run the code above in your browser using DataLab