klmr / box

Write reusable, composable and modular R code
https://klmr.me/box/
MIT License
862 stars 48 forks source link

Local module sourcing occasionally fails with RStudio run #365

Open caldwellst opened 4 months ago

caldwellst commented 4 months ago

Error description

When interactively developing in RStudio, I often might run selections of code by running line by line or highlighting a section to run with cmd + enter or the Run button. This occasionally generates errors when running box::use(local/module), even though the relative sourcing is correct. Below is an example with two modules a and b in the same directory:

a.R

#' Title
#' @export
a <- function() {
  print("I am a")
}

b.R

box::use(./a)

a$a()

If I open up b.R and highlight the code and run, it is successful:

image

However, if I leave the same code block highlighted, restart my R session, and click run without re-highlighting the code, it fails, indicating that it is unable to load module a in my current project directory.

image

However, clicking back on the b.R script and running line by line or re-highlighting will successfully run. With more complex modules with multiple imports, running code line by line can generate the same error, where it seems the RStudio pathing has lost track of the source file and defaults to project directory or base directory, depending if in a project environment or not.

I can explore a more complex reprex if helpful. This is not a critical bug, but something other colleagues have noticed so flagging here.

R version

_                           
platform       x86_64-apple-darwin20       
arch           x86_64                      
os             darwin20                    
system         x86_64, darwin20            
status                                     
major          4                           
minor          3.3                         
year           2024                        
month          02                          
day            29                          
svn rev        86002                       
language       R                           
version.string R version 4.3.3 (2024-02-29)
nickname       Angel Food Cake

RStudio version

$mode
[1] "desktop"

$version
[1] ‘2023.12.1.402’

$long_version
[1] "2023.12.1+402"

$release_name
[1] "Ocean Storm"

‘box’ version

‘1.2.0’

klmr commented 2 months ago

Hi,

Thanks for the detailed error description! Unfortunately I am completely unable to reproduce this issue… I’ve attached a video where I am running your example as closely as I can reproduce it, including using an ‘renv’ environment.

https://github.com/user-attachments/assets/b9e728d4-65a5-4b3f-a0f2-6332c66e0378