我遇到了MNP软件包的问题,​​该软件包跟踪到不幸的deparse调用(其最大宽度限制为500个字符)。

背景(如果您很无聊,则可以轻松跳过)

因为mnp使用某种特有的语法来允许变化的选择集(在公式定义中包括cbind(choiceA,choiceB,...)),所以当model.matrix.default在其上调用deparse时,公式调用的左侧为1700个字符左右。由于deparse支持最大500个字符的width.cutoff,因此sapply(attr(t, "variables"), deparse, width.cutoff = 500)[-1L]中的model.matrix.default行具有第一个元素:

[1] "cbind(plan1, plan2, plan3, plan4, plan5, plan6, plan7, plan8, plan9, plan10, plan11, plan12, plan13, plan14, plan15, plan16, plan17, plan18, plan19, plan20, plan21, plan22, plan23, plan24, plan25, plan26, plan27, plan28, plan29, plan30, plan31, plan32, plan33, plan34, plan35, plan36, plan37, plan38, plan39, plan40, plan41, plan42, plan43, plan44, plan45, plan46, plan47, plan48, plan49, plan50, plan51, plan52, plan53, plan54, plan55, plan56, plan57, plan58, plan59, plan60, plan61, plan62, plan63, "
[2] "    plan64, plan65, plan66, plan67, plan68, plan69, plan70, plan71, plan72, plan73, plan74, plan75, plan76, plan77, plan78, plan79, plan80, plan81, plan82, plan83, plan84, plan85, plan86, plan87, plan88, plan89, plan90, plan91, plan92, plan93, plan94, plan95, plan96, plan97, plan98, plan99, plan100, plan101, plan102, plan103, plan104, plan105, plan106, plan107, plan108, plan109, plan110, plan111, plan112, plan113, plan114, plan115, plan116, plan117, plan118, plan119, plan120, plan121, plan122, plan123, "
[3] "    plan124, plan125, plan126, plan127, plan128, plan129, plan130, plan131, plan132, plan133, plan134, plan135, plan136, plan137, plan138, plan139, plan140, plan141, plan142, plan143, plan144, plan145, plan146, plan147, plan148, plan149, plan150, plan151, plan152, plan153, plan154, plan155, plan156, plan157, plan158, plan159, plan160, plan161, plan162, plan163, plan164, plan165, plan166, plan167, plan168, plan169, plan170, plan171, plan172, plan173, plan174, plan175, plan176, plan177, plan178, plan179, "
[4] "    plan180, plan181, plan182, plan183, plan184, plan185, plan186, plan187, plan188, plan189, plan190, plan191, plan192, plan193, plan194, plan195, plan196, plan197, plan198, plan199, plan200, plan201, plan202, plan203, plan204, plan205, plan206, plan207, plan208, plan209, plan210, plan211, plan212, plan213, plan214, plan215, plan216, plan217, plan218, plan219, plan220, plan221, plan222, plan223, plan224, plan225, plan226, plan227, plan228, plan229, plan230, plan231, plan232, plan233, plan234, plan235, "
[5] "    plan236, plan237, plan238, plan239, plan240, plan241, plan242, plan243, plan244, plan245, plan246, plan247, plan248, plan249, plan250, plan251, plan252, plan253, plan254, plan255, plan256, plan257, plan258, plan259, plan260, plan261, plan262, plan263, plan264, plan265, plan266, plan267, plan268, plan269, plan270, plan271, plan272, plan273, plan274, plan275, plan276, plan277, plan278, plan279, plan280, plan281, plan282, plan283, plan284, plan285, plan286, plan287, plan288, plan289, plan290, plan291, "
[6] "    plan292, plan293, plan294, plan295, plan296, plan297, plan298, plan299, plan300, plan301, plan302, plan303, plan304, plan305, plan306, plan307, plan308, plan309, plan310, plan311, plan312, plan313)"

model.matrix.default针对data.frame中的变量对此进行测试时,它将返回错误。

问题

为了解决这个问题,我编写了一个新的deparse函数:
deparse <- function (expr, width.cutoff = 60L, backtick = mode(expr) %in%
  c("call", "expression", "(", "function"), control = c("keepInteger",
                                                        "showAttributes", "keepNA"), nlines = -1L)  {
    ret <- .Internal(deparse(expr, width.cutoff, backtick, .deparseOpts(control), nlines))
    paste0(ret,collapse="")
  }

但是,当我再次运行mnp并逐步执行时,由于相同的原因,它会返回相同的错误(正在运行base::deparse,而不是我的deparse)。

这让我有些惊讶,因为我期望的是此示例的典型代表,在该示例中,用户定义的函数暂时覆盖了基本函数:
> print <- function() {
+   cat("user-defined print ran\n")
+ }
> print()
user-defined print ran

我意识到解决此问题的正确方法是重写model.matrix.default,但是作为调试工具,我很好奇如何强制其使用deparse以及为什么此处未发生预期的行为(我自己)。

最佳答案

提供函数fixInNamespaceassignInNamespace可以编辑现有功能。您可以尝试...但是我不会,因为用deparse进行处理看起来太危险了:

 assignInNamespace("deparse",
                  function (expr, width.cutoff = 60L, backtick = mode(expr) %in%
               c("call", "expression", "(", "function"), control = c("keepInteger",
               "showAttributes", "keepNA"), nlines = -1L)  {
    ret <- .Internal(deparse(expr, width.cutoff, backtick, .deparseOpts(control), nlines))
    paste0(ret,collapse="")
                         }   , "base")

帮助页面上有指示,这些功能的使用受到限制,对于此类核心功能可能具有附加的保护层,我也不会感到惊讶。由于它是通过副作用起作用的,因此您无需分配结果。

09-25 22:30