1# File src/library/base/R/toString.R 2# Part of the R package, https://www.R-project.org 3# 4# Copyright (C) 1995-2012 The R Core Team 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# A copy of the GNU General Public License is available at 17# https://www.R-project.org/Licenses/ 18 19#functions to convert their first argument to strings 20toString <- function(x, ...) UseMethod("toString") 21 22toString.default <- function(x, width = NULL, ...) 23{ 24 string <- paste(x, collapse=", ") 25 if( missing(width) || is.null(width) || width == 0) return(string) 26 if( width < 0 ) stop("'width' must be positive") 27 if(nchar(string, type = "w") > width) { 28 width <- max(6, width) ## Leave something! 29 string <- paste0(strtrim(string, width - 4), "....") 30 } 31 string 32} 33