# Define a custom structure (list in R)
point <- list(x = 1, y = 2)
# Basic printing of a list
cat(sprintf("struct1: %s\n", toString(point)))
# Print list with names
cat(sprintf("struct2: %s\n", paste(names(point), unlist(point), sep = ":", collapse = " ")))
# Print list in a more R-like syntax
cat(sprintf("struct3: %s\n", deparse(point)))
# Print the type of an object
cat(sprintf("type: %s\n", class(point)))
# Formatting booleans
cat(sprintf("bool: %s\n", TRUE))
# Formatting integers
cat(sprintf("int: %d\n", 123))
# Print binary representation
cat(sprintf("bin: %s\n", paste0(as.integer(intToBits(14)), collapse = "")))
# Print character corresponding to an integer
cat(sprintf("char: %s\n", intToUtf8(33)))
# Hexadecimal encoding
cat(sprintf("hex: %x\n", 456))
# Formatting floats
cat(sprintf("float1: %f\n", 78.9))
# Scientific notation
cat(sprintf("float2: %e\n", 123400000.0))
cat(sprintf("float3: %E\n", 123400000.0))
# Basic string printing
cat(sprintf("str1: %s\n", '"string"'))
# Quoting strings
cat(sprintf('str2: "%s"\n', '"string"'))
# Hexadecimal representation of a string
cat(sprintf("str3: %s\n", paste(charToRaw("hex this"), collapse = "")))
# Print memory address (not directly supported in R)
cat(sprintf("pointer: %s\n", "Not directly supported in R"))
# Controlling width of integers
cat(sprintf("width1: |%6d|%6d|\n", 12, 345))
# Controlling width and precision of floats
cat(sprintf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45))
# Left-justifying floats
cat(sprintf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45))
# Controlling width of strings
cat(sprintf("width4: |%6s|%6s|\n", "foo", "b"))
# Left-justifying strings
cat(sprintf("width5: |%-6s|%-6s|\n", "foo", "b"))
# Using sprintf to format and return a string
s <- sprintf("sprintf: a %s", "string")
cat(s, "\n")
# Writing to stderr (similar to Fprintf in Go)
cat("io: an error\n", file = stderr())