【问题标题】:How do I get rid of "[1]"? [duplicate]如何摆脱“[1]”? [复制]
【发布时间】:2014-02-25 08:49:32
【问题描述】:

当打印到控制台或返回一个字符串时,我得到这个:

[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")

【问题讨论】:

    标签: r


    【解决方案1】:

    不要为此使用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
    

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 2021-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多