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

  • Home
  • SEARCH
  • 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 8916895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:21:55+00:00 2026-06-15T05:21:55+00:00

I am trying to override the tidy.source function of the knitr package. The problem

  • 0

I am trying to override the tidy.source function of the knitr package. The problem is that tidy.source is defined in the formatR package, which is imported by the knitr package.
If I run:

get("tidy.source", envir=asNamespace("knitr"))

I get the original code.
So I am tempted to override tidy.source with:

assignInNamespace ("tidy.source", function()print("My tidy.source"), "knitr"),

but I get:

Error in bindingIsLocked(x, ns) : no binding for "tidy.source".

In fact tidy.source is defined in formatR and inherited by knitr. With:

assignInNamespace ("tidy.source", function()print("My tidy.source"), "formatR")

everything is apparently smooth, but checking again get("tidy.source", envir=asNamespace("knitr")) shows that inside knitr nothing has changed.

Any help?

EDIT:

This question is partly obsolete due to the new development release of knitr/formatR. Many thanks to Yihui for noticing this discussion and for deciding to update his package. See:

https://github.com/yihui/formatR/commit/6f70360f359caa8d2bb33190a1c89530defb0e98

I can definitely switch from Sweave to knitr.

The general question concerning the overriding of an imported package function remains anyway open. Since it is not related to knitr/formatR package anymore, I restate it in more general terms.

Suppose you have a package main importing the package imp. If you load the former, "package:main" shows in the list of the attached packages and both "main" and "sub" show among the names of the loaded namespaces.

Assume that main imports the exported sub function exp.sub.func, which calls in turns the non-exported sub function prv.sub.func. If you want to change/customise exp.sub.func with your exp.sub.func.mod, you may think of using:

assign("exp.sub.func", exp.sub.func.mod, asNamespace ("sub"))

As a result, by running sub::exp.sub.func you will get your patched version (that is exp.sub.func.mod).
Unfortunately, as far as your exp.sub.func.mod keeps on relying on prv.sub.func, you get the error:

Error in [...] : object 'prv.sub.func' not found

In fact:

environment(sub::exp.sub.func) 

returns now: <environment: R_GlobalEnv> while it was <environment: namespace:sub> before patching.

The question is: how to move the patched function to the proper namespace?

To implement the problem above you can use whatever packages of course; in my case I used knitr and formatR as main and imported namespace and tidy.source() as patched function.

  • 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-06-15T05:21:56+00:00Added an answer on June 15, 2026 at 5:21 am

    Changing the function in the formatR namespace doesn’t change what knitr uses because knitr is already loaded. So, you could unload and reload it.

    assignInNamespace("tidy.source", function()print("My tidy.source"), "formatR")
    detach('package:knitr', unload=TRUE)
    library(knitr)
    get("tidy.source", envir=asNamespace("knitr"))
    #function()print("My tidy.source")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to override Variables that are already defined. Here is my code: package
I am trying to override the Page_PreInit function inside my class _Default which inherits
I ran into a problem today trying to override an implementation of an interface
Hi I'm trying to override Mage_Customer_AccountController, so that I can extend the createPostAction method.
I'm trying to override a CSS class which is used to display an arrow.
I am trying to override one function by replacing it with a different version.
I'm trying to override Ant compiler attributes via the command line so that all
I'm trying to override an overridden method (if that makes sense!) in C#. I
I'm trying to override an action in a controller defined by a Rails Engine.
Am trying to override get() method in my view as : broadcast = Broadcast.objects.get(request,

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.