如何永久修改R包RTextTools中函数指定行以修复bug
I’ve run into similar package modification scenarios before, so here are a few reliable ways to make your fix permanent instead of relying on trace() every time:
Method 1: Directly Replace the Original Function File in the Package Directory
This is the most straightforward approach if you just need to tweak a single function:
- First, find where RTextTools is installed on your system. Run this command in R:
This will return a path likefind.package("RTextTools")/Library/Frameworks/R.framework/Versions/X.Y/Resources/library/RTextTools(Mac/Linux) orC:/Program Files/R/R-X.Y.Z/library/RTextTools(Windows). - Navigate to the
Rsubfolder inside that directory—this is where all the package’s R function files live. - Locate the specific
.Rfile that contains the target function you modified. If yourXXX.ris the full modified version of that file, simply replace the original file with yourXXX.r(rename it to match the original filename if needed).- Note for Windows users: You may need to open your file explorer as an administrator to modify files in the Program Files directory.
- Note for Mac/Linux users: You might need to use
sudowhen replacing the file via terminal to get write permissions.
Method 2: Build and Install a Modified Local Version of the Package
If you want a fully self-contained, updatable version of the fixed package (and avoid losing your changes if the original package gets updated):
- Grab the source code of RTextTools (you can download the source tarball from CRAN’s archive, or clone the GitHub repo if it’s hosted there).
- Replace the relevant function file in the source’s
Rfolder with your modifiedXXX.r. - Use the
devtoolspackage to build and install your modified version:
This will install your fixed version over the original, and it will behave like any other installed package.# Install devtools if you haven't already install.packages("devtools") # Install from your local source directory devtools::install_local("path/to/modified/RTextTools/source")
Method 3: Auto-Overwrite the Function on R Startup
If you don’t want to modify the package files directly, you can set up R to automatically load your fixed function every time you start R:
- Open your
.Rprofilefile (you can find its location withpath.expand("~/.Rprofile")). If it doesn’t exist, create it in your home directory. - Add this code to the file, replacing
"path/to/XXX.r"with the full path to your modified function file:
Now, every time you load RTextTools with.onAttach <- function(libname, pkgname) { if (pkgname == "RTextTools") { source("path/to/XXX.r") message("Loaded fixed version of target function for RTextTools") } }library(RTextTools), your fixed function will automatically replace the original one.
Each method has its pros: Method 1 is quick but gets overwritten if you update the package; Method 2 gives you a permanent, installable fixed package; Method 3 is non-intrusive and won’t interfere with package updates (but you’ll need to keep your XXX.r file intact).
内容的提问来源于stack exchange,提问作者R newbie from Texas

