rbuildrstudiortools

how to ensure Rstudio can find Rtools?


My R version is 4.2.1, Rstudio version 2022.07.1 build 554 Rtools 4.2

/c/Program Files/R/R-4.2.1/bin/x64:/c/rtools42/usr/bin are added in PATH in both system and user.

In Rstudio, pkgbuild::find_rtools() [1] TRUE

Sys.which("make")
                              make 
"C:\\rtools42\\usr\\bin\\make.exe" 
> Sys.getenv("PATH")
[1] "C:\\rtools42/x86_64-w64-mingw32.static.posix/bin;C:\\rtools42/usr/bin;C:\\rtools42/usr/bin;C:\\Program Files\\R\\R-4.2.1\\bin\\x64;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\Interactive Intelligence\\ININ Trace Initialization\\;C:\\Program Files (x86)\\Interactive Intelligence\\ICUserApps\\;C:\\Program Files\\Interactive Intelligence\\ICUserApps\\;C:\\Program Files (x86)\\Microsoft SQL Server\\150\\DTS\\Binn\\;C:\\Program Files\\Azure Data Studio\\bin;C:\\Program Files\\PuTTY\\;C:\\Program Files\\Docker\\Docker\\resources\\bin;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Program Files\\Git\\cmd;C:\\Program Files\\R\\R-4.2.1\\bin\\x64;C:\\rtools42\\usr\\bin;C:\\Users\\fzhu\\Miniconda3;C:\\Users\\fzhu\\Miniconda3\\Library\\mingw-w64\\bin;C:\\Users\\fzhu\\Miniconda3\\Library\\usr\\bin;C:\\Users\\fzhu\\Miniconda3\\Library\\bin;C:\\Users\\fzhu\\Miniconda3\\Scripts;C:\\Users\\fzhu\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\fzhu\\AppData\\Local\\Programs\\Git\\cmd;C:\\Users\\fzhu\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\fzhu\\Anaconda3\\condabin;C:\\Program Files\\R\\R-4.2.1\\bin\\x64;C:\\rtools42\\usr\\bin;;C:/Program Files/RStudio/bin/quarto/bin"

Basically i have exact problem https://community.rstudio.com/t/rstudio-cannot-find-rtools/60007

when build any source package, i got non-zero exit

install.packages("jsonlite", type="source")
Installing package into ‘C:/Users/fzhu/AppData/Local/R/win-library/4.2’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/jsonlite_1.8.0.tar.gz'
Content type 'application/x-gzip' length 1051625 bytes (1.0 MB)
downloaded 1.0 MB

& was unexpected at this time.
Warning in install.packages :
  installation of package ‘jsonlite’ had non-zero exit status

This happens no matter use command line or build from menu.

Also since the Rstudio cannot find the Rtools, if use menu, it will ask you to redownload the Rtools42. But after that, build still fails. I wish the Rtools can automatically add tools path to environment.

Thanks for help!

reference:


Solution

  • As @MrFlick pointed out, when install package, the error & was unexpected at this time. is not caused by rtools. The package installation on windows rely on a command prompt. However, the command prompt can not be started. ( window disappear after issue a cmd in search window. )

    @MrFlick referred link, cmd.exe throws error "& was unexpected at this time." , provided problem diagnosis and solution.

    Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun  
    
    AutoRun      : if exist & if exist "C:\Users\fzhu\Miniconda3\condabin\conda_hook.bat"
    PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Command Processor       
    PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft
    PSChildName  : Command Processor
    PSDrive      : HKCU
    PSProvider   : Microsoft.PowerShell.Core\Registry
    

    This shows cmd shell has autorun commands attached by miniconda3

    Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun |  Remove-ItemProperty -Name AutoRun
    

    After that, my cmd prompt is back.

    And immediately, the installation error & was unexpected at this time. disappeared.

    Last note, I think this issue is very rare, but might be helpful to add "check cmd prompt shell function properly" into the R build library instructions. The error is very elusive and hard to figure out.

    Again, thanks MrFlick!