Learn R Programming

test.assessr (version 1.1.1)

extract_short_path: Extract the Last Two Path Components from a File Path

Description

This helper function takes any full file path and extracts only the last two components, such as `"R/add.R"`. It supports both forward slashes (`/`) and backslashes (`\`) to accommodate Windows, macOS, and Linux paths.

Usage

extract_short_path(long_file_name)

Value

A character scalar containing the last two components of the path joined with a forward slash—for example `"R/add.R"`—or a single component if only one exists. The returned value is always of class character.

Arguments

long_file_name

Character string. A full file path using `/` or `\` as separators.

Output Meaning

This function is intended for display and logging purposes, where only the tail portion of a full file path is meaningful. It does not check for file existence; it simply processes the string supplied by the user.

Details

Trailing separators are preserved where meaningful (e.g., `"R/"` when the input ends with a slash). Empty path components are removed except when required to detect a trailing separator.

Examples

Run this code
extract_short_path("pkg/R/add.R")
extract_short_path("C:\\\\projects\\\\mypkg\\\\R\\\\helper.R")

Run the code above in your browser using DataLab