最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Why printing with glue doesn't show array element? - Stack Overflow

programmeradmin3浏览0评论

Why

> print("hello")
[1] "hello"

shows a [1] at the start and the string quoted, while:

> library(glue)
> print(glue("hello"))
hello

doesn't?

Why

> print("hello")
[1] "hello"

shows a [1] at the start and the string quoted, while:

> library(glue)
> print(glue("hello"))
hello

doesn't?

Share Improve this question asked Mar 16 at 15:03 robertspierrerobertspierre 4,4823 gold badges41 silver badges63 bronze badges 2
  • 2 Glue objects have their own print method, look at print.glue() here - you will see that it uses cat which usually does not show the [1] which indicates the first index of a character printed to console (print("hello")-case) – Tim G Commented Mar 16 at 15:15
  • 4 @TimG I don't understand the downvotes. And I think your first comment is a good answer. – Rui Barradas Commented Mar 16 at 15:49
Add a comment  | 

1 Answer 1

Reset to default 2

Glue objects have their own print method, look at print.glue()

#' @export
print.glue <- function(x, ..., sep = "\n") {
  x[is.na(x)] <- style_na(x[is.na(x)])

  if (length(x) > 0) {
    cat(x, ..., sep = sep)
  }

  invisible(x)
}

You will see that it uses cat which usually does not show the [1]. This [1] indicates the first index of a character printed to console like in print("hello").

Let's compare

I commented out the style_na part, and it gives

print.glue <- function(x, ..., sep = "\n") {
  #x[is.na(x)] <- style_na(x[is.na(x)])
  
  if (length(x) > 0) {
    cat(x, ..., sep = sep)
  }
  
  invisible(x)
}

> print.glue("Hello")
Hello
> print("Hello")
[1] "Hello"
发布评论

评论列表(0)

  1. 暂无评论