2014-02-25 124 views
2

當打印到控制檯或返回一個字符串,我得到這個:我該如何擺脫「[1]」?

[1] "Please choose species to add data for".

我有這個惱人的:[1],因爲我無法擺脫它的字符串的開頭。

這裏是我的示例代碼,它的寫有光澤的包裝和輸出在GUI:

DataSets <<- input$newfile 
    if (is.null(DataSets)) 
    return("Please choose species to add data for") 

回答

5

隨着cat

> print("Please choose species to add data for") 
[1] "Please choose species to add data for" 
> cat("Please choose species to add data for") 
Please choose species to add data for 
+0

哇它是如此簡單,謝謝! – dmitriy

8

不要使用cat這一點。這是更好地使用message

fun <- function(DataSets) { 
    if (is.null(DataSets)) { 
    message("Please choose species to add data for") 
    invisible(NULL) 
    } 
} 

fun(NULL) 
#Please choose species to add data for 

不過,我會返回一個警告:

fun <- function(DataSets) { 
    if (is.null(DataSets)) { 
    warning("Please choose species to add data for") 
    invisible(NULL) 
    } 
} 

fun(NULL) 
#Warning message: 
# In fun(NULL) : Please choose species to add data for 

或錯誤:

fun <- function(DataSets) { 
    if (is.null(DataSets)) { 
    stop("Please choose species to add data for") 
    } 
} 

fun(NULL) 
#Error in fun(NULL) : Please choose species to add data for