R scripts run in R but not in Rstudio

hen I run the script below in the R console, it runs, but when I use RStudio I get this:

Error in sink(type = "output") : invalid connection

I have been working my way through Richard McElreath, Statistical Rethinking: A Bayesian Course with Examples in R and Stan, 2nd ed. McElreath uses an R package, Rethinking, which contains data for analysis and software for analysis. Based on the author’s strong suggestion I have used Rstudio and become comfortable with it. In Chapter 9, McElreath moves to use Markov Chains for analysis, relying on Stan. At this transition, I consistently get the error message: Error in sink(type = "output") : invalid connection. I struggled with installing Rstan and StanHeaders packages—learning along the way.

I finally tried running the example code in R alone. What generated the error in RStudio ran without difficulty in R.

I have done this using Microsoft 4.0.2 release and the most recent R 4.0.4. I have been using Rstudio Version 1.4.1103. I have tried several of the earlier RStudio releases and have had the same problem. Unfortunately, I have become used to having the advantages of RStudio in working with R.

Below is the code example. R Code 11.14, the first to use Ulam, generated the error: Error in sink(type = "output") : invalid connection

R code 9.11

library(rethinking)
data(rugged)
d <- rugged
d$log_gdp <- log(d$rgdppc_2000)
dd <- d[ complete.cases(d$rgdppc_2000) , ]
dd$log_gdp_std <- dd$log_gdp / mean(dd$log_gdp)
dd$rugged_std <- dd$rugged / max(dd$rugged)
dd$cid <- ifelse( dd$cont_africa==1 , 1 , 2 )

# R code 9.12
m8.3 <- quap(
  alist(
    log_gdp_std ~ dnorm( mu , sigma ) ,
    mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
    a[cid] ~ dnorm( 1 , 0.1 ) ,
    b[cid] ~ dnorm( 0 , 0.3 ) ,
    sigma ~exp( 1 )
  ) , data=dd )
precis( m8.3 , depth=2 )

# library(parallel)
# options(mc.cores = parallel::detectCores())
# Sys.setenv(LOCAL_CPPFLAGS = '-march=native')

# R code 9.13, Februry 13, 2021
dat_slim <- list(
  log_gdp_std = dd$log_gdp_std,
  rugged_std = dd$rugged_std,
  cid = as.integer( dd$cid )
)
str(dat_slim)

# R code 9.14, February 13, 2021
m9.1 <- ulam(
  alist(
    log_gdp_std ~ dnorm( mu , sigma ) ,
    mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
    a[cid] ~ dnorm( 1 , 0.1 ) ,
    b[cid] ~ dnorm( 0 , 0.3 ) ,
    sigma ~ dexp( 1 )
  ) , data=dat_slim , chains = 5 )

Hi @gary.crim,
I tested your posted code in RStudio after working out where to get the rethinking package.
I also set two of the suggested options (see below).

library("remotes")
remotes::install_github("rmcelreath/rethinking")
#> Skipping install of 'rethinking' from a github remote, the SHA1 (3b48ec8d) has not changed since last install.
#>   Use `force = TRUE` to force installation
# Also includes updating several dependencies from CRAN

library(rethinking)
#> Loading required package: rstan
#> Loading required package: StanHeaders
#> Loading required package: ggplot2
#> rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
#> For execution on a local, multicore CPU with excess RAM we recommend calling
#> options(mc.cores = parallel::detectCores()).
#> To avoid recompilation of unchanged Stan programs, we recommend calling
#> rstan_options(auto_write = TRUE)
#> Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file
#> Loading required package: parallel
#> rethinking (Version 2.13)
#> 
#> Attaching package: 'rethinking'
#> The following object is masked from 'package:stats':
#> 
#>     rstudent

# Option changes suggested when package is loaded with library()
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)

# run your posted code
data(rugged)
d <- rugged
d$log_gdp <- log(d$rgdppc_2000)
dd <- d[ complete.cases(d$rgdppc_2000) , ]
dd$log_gdp_std <- dd$log_gdp / mean(dd$log_gdp)
dd$rugged_std <- dd$rugged / max(dd$rugged)
dd$cid <- ifelse( dd$cont_africa==1 , 1 , 2 )

# R code 9.12
m8.3 <- quap(
  alist(
    log_gdp_std ~ dnorm( mu , sigma ) ,
    mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
    a[cid] ~ dnorm( 1 , 0.1 ) ,
    b[cid] ~ dnorm( 0 , 0.3 ) ,
    sigma ~exp( 1 )
  ) , data=dd )
precis( m8.3 , depth=2 )
#>              mean         sd       5.5%     94.5%
#> a[1]  0.992488451 0.09684286  0.8377149 1.1472620
#> a[2]  1.006906254 0.09269967  0.8587543 1.1550582
#> b[1]  0.005747998 0.29623087 -0.4676861 0.4791821
#> b[2] -0.005778612 0.29313825 -0.4742701 0.4627129

# R code 9.13
dat_slim <- list(
  log_gdp_std = dd$log_gdp_std,
  rugged_std = dd$rugged_std,
  cid = as.integer( dd$cid )
)
str(dat_slim)
#> List of 3
#>  $ log_gdp_std: num [1:170] 0.88 0.965 1.166 1.104 0.915 ...
#>  $ rugged_std : num [1:170] 0.138 0.553 0.124 0.125 0.433 ...
#>  $ cid        : int [1:170] 1 2 2 2 2 2 2 2 2 1 ...

# R code 9.14
m9.1 <- ulam(
  alist(
    log_gdp_std ~ dnorm( mu , sigma ) ,
    mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
    a[cid] ~ dnorm( 1 , 0.1 ) ,
    b[cid] ~ dnorm( 0 , 0.3 ) ,
    sigma ~ dexp( 1 )
  ) , data=dat_slim , chains = 5 )
#> 
#> SAMPLING FOR MODEL '4e638bc82c4247257e76c1202e0a46c7' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 1: Iteration: 100 / 1000 [ 10%]  (Warmup)
#> Chain 1: Iteration: 200 / 1000 [ 20%]  (Warmup)
#> Chain 1: Iteration: 300 / 1000 [ 30%]  (Warmup)
#> Chain 1: Iteration: 400 / 1000 [ 40%]  (Warmup)
#> Chain 1: Iteration: 500 / 1000 [ 50%]  (Warmup)
#> Chain 1: Iteration: 501 / 1000 [ 50%]  (Sampling)
#> Chain 1: Iteration: 600 / 1000 [ 60%]  (Sampling)
#> Chain 1: Iteration: 700 / 1000 [ 70%]  (Sampling)
#> Chain 1: Iteration: 800 / 1000 [ 80%]  (Sampling)
#> Chain 1: Iteration: 900 / 1000 [ 90%]  (Sampling)
#> Chain 1: Iteration: 1000 / 1000 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.039 seconds (Warm-up)
#> Chain 1:                0.021 seconds (Sampling)
#> Chain 1:                0.06 seconds (Total)
#> Chain 1: 
#> 
#> SAMPLING FOR MODEL '4e638bc82c4247257e76c1202e0a46c7' NOW (CHAIN 2).
#> Chain 2: 
#> Chain 2: Gradient evaluation took 0 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2: 
#> Chain 2: 
#> Chain 2: Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 2: Iteration: 100 / 1000 [ 10%]  (Warmup)
#> Chain 2: Iteration: 200 / 1000 [ 20%]  (Warmup)
#> Chain 2: Iteration: 300 / 1000 [ 30%]  (Warmup)
#> Chain 2: Iteration: 400 / 1000 [ 40%]  (Warmup)
#> Chain 2: Iteration: 500 / 1000 [ 50%]  (Warmup)
#> Chain 2: Iteration: 501 / 1000 [ 50%]  (Sampling)
#> Chain 2: Iteration: 600 / 1000 [ 60%]  (Sampling)
#> Chain 2: Iteration: 700 / 1000 [ 70%]  (Sampling)
#> Chain 2: Iteration: 800 / 1000 [ 80%]  (Sampling)
#> Chain 2: Iteration: 900 / 1000 [ 90%]  (Sampling)
#> Chain 2: Iteration: 1000 / 1000 [100%]  (Sampling)
#> Chain 2: 
#> Chain 2:  Elapsed Time: 0.039 seconds (Warm-up)
#> Chain 2:                0.023 seconds (Sampling)
#> Chain 2:                0.062 seconds (Total)
#> Chain 2: 
#> 
#> SAMPLING FOR MODEL '4e638bc82c4247257e76c1202e0a46c7' NOW (CHAIN 3).
#> Chain 3: 
#> Chain 3: Gradient evaluation took 0 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3: 
#> Chain 3: 
#> Chain 3: Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 3: Iteration: 100 / 1000 [ 10%]  (Warmup)
#> Chain 3: Iteration: 200 / 1000 [ 20%]  (Warmup)
#> Chain 3: Iteration: 300 / 1000 [ 30%]  (Warmup)
#> Chain 3: Iteration: 400 / 1000 [ 40%]  (Warmup)
#> Chain 3: Iteration: 500 / 1000 [ 50%]  (Warmup)
#> Chain 3: Iteration: 501 / 1000 [ 50%]  (Sampling)
#> Chain 3: Iteration: 600 / 1000 [ 60%]  (Sampling)
#> Chain 3: Iteration: 700 / 1000 [ 70%]  (Sampling)
#> Chain 3: Iteration: 800 / 1000 [ 80%]  (Sampling)
#> Chain 3: Iteration: 900 / 1000 [ 90%]  (Sampling)
#> Chain 3: Iteration: 1000 / 1000 [100%]  (Sampling)
#> Chain 3: 
#> Chain 3:  Elapsed Time: 0.039 seconds (Warm-up)
#> Chain 3:                0.027 seconds (Sampling)
#> Chain 3:                0.066 seconds (Total)
#> Chain 3: 
#> 
#> SAMPLING FOR MODEL '4e638bc82c4247257e76c1202e0a46c7' NOW (CHAIN 4).
#> Chain 4: 
#> Chain 4: Gradient evaluation took 0 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 4: Adjust your expectations accordingly!
#> Chain 4: 
#> Chain 4: 
#> Chain 4: Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 4: Iteration: 100 / 1000 [ 10%]  (Warmup)
#> Chain 4: Iteration: 200 / 1000 [ 20%]  (Warmup)
#> Chain 4: Iteration: 300 / 1000 [ 30%]  (Warmup)
#> Chain 4: Iteration: 400 / 1000 [ 40%]  (Warmup)
#> Chain 4: Iteration: 500 / 1000 [ 50%]  (Warmup)
#> Chain 4: Iteration: 501 / 1000 [ 50%]  (Sampling)
#> Chain 4: Iteration: 600 / 1000 [ 60%]  (Sampling)
#> Chain 4: Iteration: 700 / 1000 [ 70%]  (Sampling)
#> Chain 4: Iteration: 800 / 1000 [ 80%]  (Sampling)
#> Chain 4: Iteration: 900 / 1000 [ 90%]  (Sampling)
#> Chain 4: Iteration: 1000 / 1000 [100%]  (Sampling)
#> Chain 4: 
#> Chain 4:  Elapsed Time: 0.033 seconds (Warm-up)
#> Chain 4:                0.03 seconds (Sampling)
#> Chain 4:                0.063 seconds (Total)
#> Chain 4: 
#> 
#> SAMPLING FOR MODEL '4e638bc82c4247257e76c1202e0a46c7' NOW (CHAIN 5).
#> Chain 5: 
#> Chain 5: Gradient evaluation took 0 seconds
#> Chain 5: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 5: Adjust your expectations accordingly!
#> Chain 5: 
#> Chain 5: 
#> Chain 5: Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 5: Iteration: 100 / 1000 [ 10%]  (Warmup)
#> Chain 5: Iteration: 200 / 1000 [ 20%]  (Warmup)
#> Chain 5: Iteration: 300 / 1000 [ 30%]  (Warmup)
#> Chain 5: Iteration: 400 / 1000 [ 40%]  (Warmup)
#> Chain 5: Iteration: 500 / 1000 [ 50%]  (Warmup)
#> Chain 5: Iteration: 501 / 1000 [ 50%]  (Sampling)
#> Chain 5: Iteration: 600 / 1000 [ 60%]  (Sampling)
#> Chain 5: Iteration: 700 / 1000 [ 70%]  (Sampling)
#> Chain 5: Iteration: 800 / 1000 [ 80%]  (Sampling)
#> Chain 5: Iteration: 900 / 1000 [ 90%]  (Sampling)
#> Chain 5: Iteration: 1000 / 1000 [100%]  (Sampling)
#> Chain 5: 
#> Chain 5:  Elapsed Time: 0.042 seconds (Warm-up)
#> Chain 5:                0.02 seconds (Sampling)
#> Chain 5:                0.062 seconds (Total)
#> Chain 5:

# I get a warning message about but it seems to work...
precis(m9.1)
#> 4 vector or matrix parameters hidden. Use depth=2 to show them.
#>            mean          sd      5.5%     94.5%   n_eff    Rhat4
#> sigma 0.1117258 0.006213256 0.1021732 0.1221128 2489.33 1.000138
plot(m9.1, depth=2)

Created on 2021-02-24 by the reprex package (v1.0.0)

It seems to work but I did get a warning about an unavailable g++ executable??
My setup details are:

RStudio.Version()$version
[1] ‘1.4.1103’

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rethinking_2.13      rstan_2.21.2         ggplot2_3.3.3        StanHeaders_2.21.0-7
[5] remotes_2.2.0       

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         mvtnorm_1.1-1      lattice_0.20-41    prettyunits_1.1.1 
 [5] ps_1.5.0           assertthat_0.2.1   rprojroot_2.0.2    digest_0.6.27     
 [9] utf8_1.1.4         V8_3.4.0           R6_2.5.0           backports_1.2.1   
[13] reprex_1.0.0       stats4_4.0.3       coda_0.19-4        evaluate_0.14     
[17] highr_0.8          pillar_1.5.0       rlang_0.4.10       curl_4.3          
[21] rstudioapi_0.13    callr_3.5.1        rmarkdown_2.6      styler_1.3.2      
[25] loo_2.4.1          munsell_0.5.0      compiler_4.0.3     xfun_0.21         
[29] pkgconfig_2.0.3    pkgbuild_1.2.0     shape_1.4.5        clipr_0.7.1       
[33] htmltools_0.5.1.1  tidyselect_1.1.0   tibble_3.0.6       gridExtra_2.3     
[37] codetools_0.2-16   matrixStats_0.58.0 fansi_0.4.2        crayon_1.4.1      
[41] dplyr_1.0.4        withr_2.4.1        MASS_7.3-53        grid_4.0.3        
[45] jsonlite_1.7.2     gtable_0.3.0       lifecycle_1.0.0    DBI_1.1.1         
[49] magrittr_2.0.1     scales_1.1.1       RcppParallel_5.0.2 cli_2.3.1         
[53] debugme_1.1.0      fs_1.5.0           ellipsis_0.3.1     generics_0.1.0    
[57] vctrs_0.3.6        tools_4.0.3        glue_1.4.2         purrr_0.3.4       
[61] processx_3.4.5     yaml_2.2.1         inline_0.3.17      colorspace_2.0-0  
[65] knitr_1.31

If you don't make progress, I suggest you contact the package author and send a fully-reproducible example.