Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:59:14+00:00 2026-05-27T21:59:14+00:00

I basically want to change a non-visible function of a package. For visible functions,

  • 0

I basically want to change a non-visible function of a package. For visible functions, i.e. functions that have no asterix when methods is called on them, I found two posts how I could achieve my goal:

  1. Use assignInNamespace: see post on R-help.
  2. Use fix: see post on stackoverflow

Although both approaches work for an exported / visible function (I use predict.lm as an example further below for the second approach and tested the first approach with the function subset.data.frame), they do not work for a non-visible function, e.g. predict.ar. Why is that? Is there a workaround?

Here is a minimal example:

Show that predict.lm is visible, predict.ar is not:

methods(predict)
 [1] predict.Arima*             predict.HoltWinters*       predict.StructTS*         
 [4] predict.ar*                predict.arima0*            predict.glm               
 [7] predict.lm                 predict.loess*             predict.mlm               
[10] predict.nls*               predict.poly               predict.ppr*              
[13] predict.prcomp*            predict.princomp*          predict.smooth.spline*    
[16] predict.smooth.spline.fit*

Apply predict.lm:

x <- rnorm(5)
y <- x + rnorm(5)
predict(lm(y ~ x))
#          1          2          3          4          5 
#  1.0783047  1.5288031  0.3268405  0.8373520 -0.9833746

Change predict.lm by entering cat(“First line changed for predict.lm\n”)
at the beginning of the function body. (You have to do that manually in the editor):

fix(predict.lm)
predict(lm(y ~ x))
# First line changed for predict.lm
#          1          2          3          4          5 
#  1.0783047  1.5288031  0.3268405  0.8373520 -0.983374

Apply predict.ar:

sunspot.ar <- ar(sunspot.year)
predict(sunspot.ar, n.ahead=25)
# $pred
# Time Series:
# Start = 1989 
# End = 2013 

Try to change predict.ar:

fix(predict.ar) #Here, an empty function body appears for me
fix("stats:::predict.ar") #Here as well
fix(stats:::predict.ar)
#Error in fix(stats:::predict.ar) : 'fix' requires a name

Try to use assignInNamespace instead. (Note that I just copied the function stats:::predict.ar in an editor and added the line cat("First line changed for predict.ar\n") at the beginning of the body. Because the function body is quite long, I only show the first couple of lines here)

mypredict <- function (object, newdata, n.ahead = 1, se.fit = TRUE, ...) 
{
    cat("First line changed for predict.ar\n")
    if (n.ahead < 1) 
        stop("'n.ahead' must be at least 1")
    #Rest of body of stats:::predict.ar
}
assignInNamespace("predict.ar", mypredict, ns="stats")
predict(sunspot.ar, n.ahead=25)
# First line changed for predict.ar
# Error in predict.ar(sunspot.ar, n.ahead = 25) : 
#   object 'C_artoma' not found

Since “First line changed for predict.ar” is actually printed to the console, predict.ar must have been changed. However, why is the object ‘C_artoma’ not found anymore?

UPDATE: OK, this is super embarrassing, but I can’t delete that post anymore: The answer was already at the link I provided with Richie Cotton’s answer at the end. Sorry for wasting your time! I think I checked everything and then I don’t see the obvious. Could someone just post this as an answer an I accept it. Sorry again.

fixInNamespace(predict.ar, pos="package:stats")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:59:14+00:00Added an answer on May 27, 2026 at 9:59 pm

    Use fixInNamespace. 🙂

    fixInNamespace("predict.ar", "stats")
    

    or

    fixInNamespace("predict.ar", pos="package:stats")
    

    (Several years later…)
    From Nicholas H’s comment: if you want to push some code to CRAN that depends upon an internal function from another package, it will throw a build warning and be rejected by R-core. If you want that internal function, you should just take a copy of it using the ::: operator and maintain it yourself.

    predict.ar <- stats:::predict.ar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I basically want to have a static method in my AR class to
I have a 2-dimensional array of objects and I basically want to databind each
I have an SPListItemCollection. I basically want to get one of the items (randomly)
I have an existing system that I do not wish to change where I
Basically i want to change the marker position dynamically by changing its longitude and
I am designing an application where I basically want to load geographical maps that
Basically I have a slightly non-standard implementation of FancyBox. By default you have to
Basically I want to change the font and the color of my section header,
I have a bit of HTML code that I would like to change based
I basically want to build 10 copies of a windows service that was coded.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.