简体   繁体   English

如何覆盖 R 中字符向量的自动打印

[英]How can I overwrite autoprinting for a character vector in R

We're producing Rmd reports which were originally intended for internal use only.我们正在生成最初仅供内部使用的 Rmd 报告。 Now these reports are used for external communication as well, with the result that some of the language is incomprehensible for non-insiders.现在这些报告也用于对外交流,结果导致一些语言对于非内部人员来说是无法理解的。 The objective now is to incorporate a mechanism so that the same report can be produced in two versions: one in it's original form and one for external use.现在的目标是整合一种机制,以便可以生成两个版本的相同报告:一个是原始形式,另一个是供外部使用。 Apart from other stuff the external version also should have internal slang translated to normal language.除了其他东西外,外部版本还应该将内部俚语翻译成普通语言。
My basic idea was to overwrite the print methods with a version which first converts an object to its translated form and then forwards it to the standard print function.我的基本想法是用一个版本覆盖打印方法,该版本首先将 object 转换为其翻译形式,然后将其转发到标准打印 function。

Here what I've tried in a simplified form:这是我以简化形式尝试过的:

First I created a translate function which exchanges internal slang words for normal words:首先,我创建了一个翻译 function ,它将内部俚语交换为普通词:

dictionary <- data.frame(id = "roomNb", long = "No. of rooms")

translate <- function(x,
                      language     = "long",
                      translations = dictionary )
    {
    
    matched <- match(x, translations$id, 0)
    out <- character(length(x))
    # translate:
    out[matched != 0] <- as.character(translations[[language]][matched] )
    # copy other fields: 
    out[matched == 0] <- as.character(x[matched == 0])
   
    out  
  }

Now the new print methods for data frame and character vectors:现在数据帧和字符向量的新打印方法:

# overwrite print function for data frame
print.data.frame <- function(x, ...) {
  # first translate (only first col here)
  out <- x
  out[,1] <- translate(x[,1])
  # then forward to base print: 
  base::print.data.frame(out, ...)
}
# overwrite print function for character vector:
print.character <- function(x, ...) {
  out <- translate(x) 
  base::print.default(out, ...)
}

With data frames it works, with character vectors it doesn't:对于数据帧它可以工作,对于字符向量它不能:

testDF <- data.frame(a = c("roomNb", "otherStuff"), b = 1:2)
testDF # -> works! 

testChar <-  c("roomNb" , "otherStuff" )
testChar    # Does not work!!
print(testChar) # works, but that's not what I want

According to R Internals section 1.6 if the object has a class attribute it should be auto-printed by the respective method.根据R Internals section 1.6 如果 object 具有 class 属性,则应通过相应的方法自动打印。 But this doesn't seem to be the case.但情况似乎并非如此。
Grateful for any help!感谢您的帮助!

Defining custom print functions for chunks as described in https://cran.r-project.org/web/packages/knitr/vignettes/knit_print.html turned out to be the solution.https://cran.r-project.org/web/packages/knitr/vignettes/knit_print.html中所述为块定义自定义打印功能原来是解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中的字符向量中随机选择元素? - How can I randomly choosing elements in character vector in R? 如何将分数表达式转换为 R 中的字符向量? - How can I convert a fraction expression into a character vector in R? 如何以可以在R中创建另一个矢量的方式打印字符矢量? - how can I print a character vector in a way I can create another vector in R? 在R中,如何查看字符向量的三个元素在长度为5的向量中是否相同 - in R, how can I see if three elements of a character vector are the same out of a vector of length 5 如何在R中回收字符向量? - How do I recycle a character vector in R? 如何为R中的字符向量取的每个字符分配不同的值 - How can I assign different values to each character taken from a character vector in R R-自动打印后重置​​选项 - R - Reset options after autoprinting 如何将R字符向量转换为C字符指针? - How do I convert an R character vector to a C character pointer? 当我在R中键入字符向量的名称时,如何打印而没有多余的行? - When I type the name of a character vector in R, how can I print without unnecessary lines? 如何将带有时间的字符向量转换为 Time 类并在 r 中求和后获得总时间 - How can I convert character vector with time into Time class and get a total amount of time after summing in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM