Use of trimws for trimming character vectors:
### Remove all leading and trailing whitespaces
trimws(" This has trailing spaces. ")
# [1] "This has trailing spaces."
### Remove leading whitespaces
trimws(" This has trailing spaces. ", which = "left")
# [1] "This has trailing spaces. "
### Remove trailing whitespaces
trimws(" This has trailing spaces. ", which = "right")
# [1] " This has trailing spaces."
See also strtrim to trim character strings to specified display widths.