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 6007653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:42:41+00:00 2026-05-23T01:42:41+00:00

As the termplot function in R is containing some weird code that is giving

  • 0

As the termplot function in R is containing some weird code that is giving me annoying bugs, I want to override it in my own test code until I find a more permanent solution. Problem is that the changed function is not loaded by the mgcv package. The mgcv package loads termplot from the stats package in its namespace, using importFrom() in the NAMESPACE file.

How can I convince mgcv to use the changed termplot? I tried :

unlockBinding("termplot", as.environment("package:stats"))
assign("termplot", my.termplot, as.environment("package:stats"))
lockBinding("termplot", as.environment("package:stats"))

and when applied to lm-objects, this works and the altered termplot is used. But when using gam-objects made by the mgcv package, this doesn’t work. I’m not really going to build the stats package from source if I can avoid it…

To clarify, I also tried with

assignInNamespace("termplot", my.termplot, ns="stats")
assignInNamespace("termplot", my.termplot, ns="mgcv")

in all possible combinations, before attaching mgcv, after attaching mgcv, and I didn’t manage to get it working.


EDIT :

I tried all options given here (apart from rebuilding either package), and couldn’t get it to work. The easy way out for me is using a wrapper function. That discussion can be found here. Thanks for all the tips.


A reproducible example :

my.termplot <- function (x) print("my new termplot")

  unlockBinding("termplot", as.environment("package:stats"))
  assignInNamespace("termplot", my.termplot, ns="stats", envir=as.environment("package:stats"))
  assign("termplot", my.termplot, as.environment("package:stats"))
  lockBinding("termplot", as.environment("package:stats"))


y <- 1:10
x <- 1:10
xx <- lm(y~x)
termplot(xx)
require(mgcv)
dat <- gamSim(1, n = 400, dist = "normal", scale = 2)
b <- gam(y ~ s(x0) + s(x1) + s(x2) + x3, data = dat)
plot(b,all=TRUE)

plot.gam calls termplot for the non-smooth terms (x3 in this case), but fails to find the new termplot function.


EDIT2 : apparently, my example works. I see now I solved my own question: In the first code, I didn’t add both the namespace and the package in assignInNamespace. It is important to remember to change the function both in the namespace and the package before loading the other package. Thx @hadley for pointing me in the right direction, @Marek for testing the code and reporting it works, and the rest for taking the effort to answer.

  • 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-23T01:42:42+00:00Added an answer on May 23, 2026 at 1:42 am

    I’m stumped – I can’t figure out how plot.gam is locating termplot – it’s not using the ordinary scoping rules as far as I can tell. This seems to need a deeper understanding of namespaces than I currently possess.

    my.termplot <- function (x) print("my new termplot")
    
    # where is it defined?
    getAnywhere("termplot")
    # in package and in namespace
    
    unlockBinding("termplot", as.environment("package:stats"))
    assign("termplot", my.termplot, "package:stats")
    
    unlockBinding("termplot", getNamespace("stats"))
    assign("termplot", my.termplot, getNamespace("stats"))
    
    getAnywhere("termplot")[1]
    getAnywhere("termplot")[2]
    # now changed in both places
    
    y <- 1:10
    x <- 1:10 + runif(10)
    xx <- lm(y ~ x)
    termplot(xx) # works
    
    library("mgcv")
    b <- gam(y ~ s(x), data = data.frame(x, y))
    plot(b) # still calls the old termplot
    
    # I'm mystified - if try and find termplot as
    # seen from the environment of plot.gam, it looks 
    # like what we want
    get("termplot", environment(plot.gam)) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.