Windows环境下修改CRAN下载的R包源码后如何重新安装?
Got it, let’s walk through this step-by-step—this is a common workflow when you spot a bug in an R package and need to patch it yourself on Windows.
Step 1: Download the Source Package
First, grab the source version of the package from CRAN. You have two options:
- Go to the package’s CRAN page, scroll down to the "Sources" section, and download the
.tar.gzfile. - Or do it directly from R with this command:
This saves thedownload.packages("your_package_name", destdir = ".", type = "source").tar.gzfile in your current working directory.
Step 2: Extract the Source Files
Next, unpack the .tar.gz archive:
- GUI way: Right-click the file and use your favorite unzip tool (like 7-Zip or Windows’ built-in extractor) to extract it to a folder (e.g.,
./modified_your_package). - R command way: Run this to extract programmatically:
untar("your_package_name.tar.gz", exdir = "./modified_your_package")
Step 3: Fix the Problem Script
Navigate to the extracted folder. The R scripts you need to modify are almost always in the R/ subdirectory. Open the problematic .R file with RStudio (or any text editor you prefer), make your fixes, and save the file.
Pro tip: If the bug was in compiled code (like C/C++ files in the
src/folder), you’d edit those too—but since you mentioned a "script," theR/folder is your focus.
Step 4: Set Up the Windows Compilation Environment
Windows doesn’t come with a compiler by default, so you’ll need Rtools to build the package from source. Here’s how to get it sorted:
- Download the version of Rtools that matches your R version from the CRAN Rtools page (e.g., Rtools42 for R 4.2.x and newer).
- During installation, make sure to check the box that says "Add Rtools to system PATH" (this saves you from manual path setup later).
- Verify it’s working by running these commands in R:
IfSys.which("make") # Should return a path to make.exe in Rtools devtools::has_devel() # Should return TRUE if everything is set uphas_devel()fails, double-check your Rtools installation and PATH settings.
Step 5: Reinstall the Modified Package
Now you’re ready to build and install your patched package. Pick one of these methods:
Method 1: Use devtools (easiest for most)
If you don’t have devtools installed, install it first: install.packages("devtools"). Then run:
devtools::install("./modified_your_package")
This handles dependencies, builds the package, and installs it.
Method 2: Base R Command
You can also use the base R install.packages() function by pointing it to your modified source folder:
install.packages("./modified_your_package", repos = NULL, type = "source")
Method 3: RStudio Build Interface
If you open the extracted folder as an RStudio project (go to File > Open Project and select the .Rproj file in the source folder), use the Build pane:
- Click Build > Install and Restart to build the package and restart R with the new version loaded.
Step 6: Verify the Fix
Once installation finishes, load the package with library(your_package_name) and test the functionality that had the bug. If everything works as expected, you’re good to go!
Quick note: If you update R or the original package from CRAN later, your patched version will be overwritten—so keep a copy of your modified source folder if you need to reapply the fix down the line.
内容的提问来源于stack exchange,提问作者dsbisht

