RStudio IDE Preview: problems when chunk options refer to variables

crash-report
ide-issue

#1

I have not been able to reduce this to a minimal reproducible case, but when I'm working with a big RMarkdown document and I assign values to chunk options that are computed from variables defined/assigned in earlier chunks, I get problems when I try to run chunks interactively.

When I make small toy RMarkdown documents, I don't have this problem and having chunk options refer to variables defined in earlier chunks does not create these errors, so I am having trouble figuring out how to go from my big RMarkdown files to a small reproducible example.

Here is an example, excerpted from a larger document:

```{r block_variogram_plot, include=TRUE, dependson=c('make_spatial','load_data','interpolate')}
month_list <- c('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC')
var_labels <- c(precip = "Precipitation", t.max = expression(T[max]), 
                t.min = expression(T[min]), t.avg = expression(T[avg]))
cap_labels <- c(precip = "Precipitation", t.max = "$T_{\text{max}}$", 
                t.min = "$T_{\text{min}}$", t.avg = "$T_{\text{min}}$")
block_variogram_plot <- function(data, variable) {
  variograms <- lapply(0:11, function(m) {
    model_variogram(data[data$month == m,], formula(paste0(variable, " ~ 1")), 
                    filter=TRUE)
    })
  v <- lapply(0:11, function(x) {
    y <- variograms[[x+1]]$variogram; y$month = x; y
    }) %>% bind_rows()
  vx <- lapply(0:11, function(m) { 
    x <- variogramLine(variograms[[m+1]]$model, 
                       maxdist = max(v$dist[v$month == m]))
    x$month <- m; x
    }) %>% bind_rows()
  v$month <- factor(month_list[v$month + 1], levels = month_list, ordered = TRUE)
  vx$month <- factor(month_list[vx$month + 1], levels = month_list, ordered = TRUE)
  p <- ggplot(v, aes(x = dist, y = gamma)) + geom_point(size = I(3))
  p <- p + geom_line(data = vx)
  p <- p + facet_wrap( ~ month, scales = 'fixed')
  invisible(p)
  }
```{r plot_variograms, include=TRUE, fig.cap="Semivariograms with fitted linear models", fig.subcap=unlist(lapply(cap_labels, function(x) paste0("Semivariogram of ", x, " with linear model."))), dependson=c('block_variogram_plot')}
for (var in c('precip','t.max','t.min','t.avg')) {
  var <- paste0(var, "_mean")
  p <- block_variogram_plot(baseline, var)
  plot(p + labs(x = "Distance (deg. lat/lon)", y = "Semivariance", title = var_labels[var]) + 
         theme_classic(base_size = 10))
  # cat('\n\n')
  }

If go to the plot_variograms chunk and interactively run all previous chunks (Ctrl+Alt+P), and then run the plot_variograms chunk with Ctrl+Shift+Enter or try to run the for loop by putting the cursor on the for line and doing Ctrl+Enter, I get an error message on the console I get the error message

Error in lapply(cap_labels, function(x) paste0("Semivariogram of ", x, : 
   object 'cap_labels' not found

However, cap_labels is defined in the interactive session:

> cap_labels
           precip             t.max             t.min             t.avg 
  "Precipitation" "$T_{\text{max}}$" "$T_{\text{min}}$" "$T_{\text{min}}$" 

Further, RStudio sporadically crashes when I am doing this interactively, giving me a "Fatal error: Unexpected exception: bad allocation" message. The crashes are reasonably frequent but don't happen predictably enough for me to give a recipe for reproducing them.

I never had any of this trouble with RStudio 1.1 and earlier, so this is a new thing with the 1.2 previews.

Here is my SessionInfo:

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

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

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

other attached packages:
 [1] bindrcpp_0.2.2 ggthemes_4.0.1 ggplot2_3.0.0  gstat_1.1-6    sp_1.3-1       readr_1.1.1   
 [7] purrr_0.2.5    stringr_1.3.1  dplyr_0.7.6    tidyr_0.8.1    readxl_1.1.0   rvest_0.3.2   
[13] xml2_1.2.0     knitr_1.20    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19     plyr_1.8.4       cellranger_1.1.0 pillar_1.3.0     compiler_3.5.1  
 [6] bindr_0.1.1      tools_3.5.1      xts_0.11-1       packrat_0.4.9-3  gtable_0.2.0    
[11] tibble_1.4.2     lattice_0.20-35  pkgconfig_2.0.2  rlang_0.2.2      rstudioapi_0.8  
[16] rgdal_1.3-4      withr_2.1.2      httr_1.3.1       hms_0.4.2        tidyselect_0.2.5
[21] spacetime_1.2-2  glue_1.3.0       R6_2.3.0         selectr_0.4-1    magrittr_1.5    
[26] scales_1.0.0     intervals_0.15.1 assertthat_0.2.0 colorspace_1.3-2 stringi_1.2.4   
[31] lazyeval_0.2.1   munsell_0.5.0    crayon_1.3.4     FNN_1.1.2.1      zoo_1.8-4       

I have tried to paste the diagnostics report into this message, but I get an error about the body of my message being too long (the diagnostics report is 1.9 million characters) and I don't seem to be able to upload the diagnostics report because ".txt" is not a permitted file extension.


#2

File uploads here are limited to images only. For really long output, I think your best bet for now is to link to it hosted elsewhere (GitHub gist, Google Drive, etc).