0

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:

FrankZhu
  • 514
  • 4
  • 10
  • Can [this SO post](https://stackoverflow.com/questions/33103203/rtools-is-not-being-detected-from-rstudio) help? – Rui Barradas Sep 02 '22 at 13:29
  • This doesn't look like the same problem you linked to . The error message `& was unexpected at this time.` Seems quite different. Those don't look like the right PATH values for a windows machine. What exactly does `Sys.getenv("PATH")` return when you run it in R? Did you create a `.RProfle` or `.Renviron` file in your home directory? If so what exactly are the contents of that file? – MrFlick Sep 02 '22 at 13:33
  • I don't have .RProfile or .Renviron in my home directory. the result is here ```Sys.getenv("PATH") [1] "blah blah;C:\\Program Files\\R\\R-4.2.1\\bin\\x64;C:\\rtools42\\usr\\bin;;C:/Program Files/RStudio/bin/quarto/bin"``` – FrankZhu Sep 02 '22 at 13:36
  • So "blah blah" is literally on your path? How did that get there? Make sure you edit your path variables so that only valid paths are present. – MrFlick Sep 02 '22 at 13:45
  • blah blah means some other path. I cannot post the whole paths since it exceeds the characters limit. But the path after blah blah is R related. – FrankZhu Sep 02 '22 at 13:47
  • @RuiBarradas I did try your reference and it doesn't help. The link suggest to add two envs. this is what I added. `Sys.setenv(PATH = paste("C:/Rtools42/usr/bin", Sys.getenv("PATH"), sep=";")) and Sys.setenv(BINPREF = "C:/Rtools42/usr/bin/")` it seems that Rtools 4.x and Rtoos 3.x are a bit different in default tools path. Rtools 3.x use `C:/tools3x/mingw_64/bin` but Rtools4x use `C:/tools42/user/bin` – FrankZhu Sep 02 '22 at 13:51
  • Is there a `&` character somewhere in the path? You should be able to edit the PATH into your main question rather than including it as a comment. What does `Sys.which("make")` return? – MrFlick Sep 02 '22 at 15:20
  • @MrFlick please see the result updated in post above. there is no & in path – FrankZhu Sep 02 '22 at 16:23
  • 1
    The path looks OK and since `Sys.which()` doesn't have a problem finding `make` and `pkgbuild::find_rtools()` returns `TRUE`, this doesn't really seem to be related to RTools at all. This problem this seems most similar to is https://stackoverflow.com/questions/59480657/cmd-exe-throws-error-was-unexpected-at-this-time where there is a problem with out a user customized their command prompt. Have you tried to customize your terminal at all? Also, have you tried restarting your computer to make sure it's not a temporary environment issue? – MrFlick Sep 02 '22 at 17:20
  • this link you provided fixed it! The offender is autorun commands placed by the miniconda3 hooks. After remove these, my command prompt fixed and I can build. I agree this is not a rtools issue, it is issue without a user command prompt. – FrankZhu Sep 02 '22 at 19:24
  • @MrFlick if you can put your comment in answer and i can mark it resolved. – FrankZhu Sep 02 '22 at 19:29

1 Answers1

1

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.

  • diagnosis Although I cannot start cmd prompt directly, I can still use vs code terminal to run dos commands. below is the following command,
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

  • solution then issue command to remove these
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!

FrankZhu
  • 514
  • 4
  • 10