作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 RStudio 并希望为特殊分类的 data.frame 定义一个 View 方法,但仍然保留标准 data.frame/matrix 对象的现有 View 方法。
RStudio 默认:
View(x)
时,在“源” Pane 中的表格浏览器中打开 data.frame 或矩阵对象或单击“环境” Pane 中的对象时。 View()
我的对象的行为,但也要保持原始行为?
iris_myclass <- iris
class(iris_myclass) <- c("myclass", "data.frame")
View(iris)
## opens a tabular data browser in the Source pane
View(iris_myclass)
## opens a tabular data browser in the Source pane
View <- function(x, title) {
UseMethod("View")
}
View.default <- function(x, title) {
utils::View(x, title)
}
View.myclass <- function(x, title) {
DT::datatable(x)
}
View(iris)
## opens an Xwindows Viewer
View(iris_myclass)
## opens an HTML datatable browser in the Viewer pane
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.2
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] DT_0.2
最佳答案
它确实像这样工作:
View.default <- function(...) {
get('View', as.environment('package:utils'))(...)
}
utils::View
和
mget('View')
返回不同的函数。正如@KevinUshey 解释的那样,Rstudio 会覆盖
View
在搜索路径上(在
package:utils
环境中),但不在
utils
中命名空间。所以我们不能用
utils::View
访问它,但我们可以使用
mget
.
get
而不是
mget
甚至更好(感谢@Willem)。
关于r - 如何为自定义数据框重载 RStudio 的 View 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41532300/
我是一名优秀的程序员,十分优秀!