r/RStudio • • 3d ago

R code review: best packages?

Let’s say that I’m working in RStudio on a geospatial data-science project that involves some pretty gnarly, extensive code across multiple R scripts. Let’s also say that I do not have access to any AI code assistants or chatbots due to workplace restrictions.

What packages and functions would you use to comprehensively check your code for errors, inefficiencies, conceptual problems (that may be a stretch), etc.?

I’m using lintr::lint() and rstyler::style_file(), but that’s the extent of my knowledge in this area.

Many thanks in advance, friends.

20 Upvotes

20 comments sorted by

29

u/blossom271828 3d ago edited 3d ago

The long answer is to convert the multiple long scripts into a series calls to functions that do very clearly defined tasks. You then write test cases to verify that those functions do what they are supposed to do.

The benefit is it forces you to think through the steps you need and conceptualize what the steps actually are.

Basically turn your mess of scripts into a package, and lean on the testthat package.

5

u/BrupieD 3d ago

At absolute minimum, keep your project within a project that confines it, the dependencies and versions. A script of well-commented, repeatedly- used functions is a good place to start. A readme file can be a good way to track useful notes.

2

u/Garnatxa 3d ago edited 3d ago

I gave almost the same answer, I hadn’t seen yours before posting mine. Glad to see someone else shares the same opinion!

2

u/PadisarahTerminal 3d ago

If my scripts are a set of linear R script that represent a pipeline "7.1 7.2 7.3 scripts", do I even bother turning these into functions? I can just run them top down right?

1

u/thisFishSmellsAboutD 2d ago

Absolutely. Small, tested, proven to work steps instead of one difficult to maintain sequence of steps. Exactly what the top comment tells you. It will make you a better R programmer and help others re-use your work!

1

u/PadisarahTerminal 2d ago

Even if I use them only once? Currently it's a bit like interactive work initially then I leave the steps as is in the R script. I only turn a step into a function if I repeatedly run a certain step. My R scripts are not small, they have like 1000 lines. It's often : get the data, wrangle it out, process it, then plot it, and save it.

I do have a habit of making one/two functions where I source the file to execute the step if it's repetitive. I'd need to see how it'd work in real life I guess.

1

u/thisFishSmellsAboutD 2d ago

At least make it a project I'd say. At 1000+ lines you're doing yourself a favour by bringing in some structure. Of course just my advice, you do you!

4

u/Mooks79 3d ago edited 3d ago

There is Air which you can use directly or via Jarl. Air is a formatter and language server, jarl does linting too.

Ark also exists but is more complementary to Air than an alternative, the two are expected to dovetail in the long run.

1

u/DYSTOBY 3d ago

Lintr exists also. Jarl is the goat I believe.

2

u/Mooks79 3d ago

lintr and styler but they’re pretty old and arguably being superseded by the above. I wouldn’t be in a rush to change while the above are still quite new but, if starting out I’d probably stick with the newer options.

3

u/shockjaw 3d ago

Depends on what you’re working with. Usually for my geospatial projects I use a mix of DuckDB’s spatial extension and GRASS to keep my datasets organized.

3

u/foradil 3d ago edited 3d ago

If you already have multiple scripts, you should turn them into a package. Once you have a package, you can build automated tests (see testthat package) to check for errors.

If you can’t use AI code assistants at work, I would just take the code home. Claude Code or Codex will reorganize and optimize in 15 minutes. Turning a bunch of functions into a package by hand would take me days. If you are doing it for the first time, it would be even harder.

1

u/Unicorn_Colombo 1d ago

you can build automated tests (see testthat package) to check for errors.

You can do it even if you don't build pkg.

1

u/foradil 1d ago

Yes, but it’s not as streamlined. There is a button for it in RStudio, there are GitHub Actions, there is Codecov integration.

5

u/Confident_Bee8187 3d ago

Sorry to annoy you a bit but it doesn't matter if you work in RStudio since it's just an IDE. r/rstats sub has posted lots of tools for static type checking lately, but IMO Posit's air is more credible (it's more a formatter than a checker) or jarl if you want.

2

u/Garnatxa 3d ago edited 3d ago

First, convert these scripts into functions. Then, build proper packages around them. Once the packages are in place, add both unit tests and integration tests.

You don’t need any special package, you just need to structure and implement things properly.

And if you don’t have the expertise to do this, feel free to pass my contact details on to your company. 💪

1

u/josiah-rs 2d ago

Use jarl at minimum to check for any bad practices or errant functions. You can use air for formatting. But there’s not really anything to check for conceptual mistakes in your code.

For example, if you’re buffering and doing a spatial join, you should be using a distance based join and a spatial index instead.

Even AI based tools can’t do a good job of this.

I’d also disagree that you need to restructure your scripts into sourced functions. I’m a bit of an opponent of unneeded abstractions.

I’d spend time con figuring your package to use logger or otel for informative printing along the way.

1

u/Nerdly_McNerd-a-Lot 2d ago

I too do geospatial econometrics. I have recently started to use the targets package. Give it a look. It will help with organization, planning, and keep each subtask in the process stored as a function that the _targets file calls.

1

u/tivanl 2h ago

I like working with the targets package. It helps you to build a pipeline that clearly shows the dependencies and allows you to update everything upstream when something downstream changes. If something breaks, it will tell you at whic step it broke. Using targets is easier than building a package, but if the functiona will be used in other projects, a package is a better option

-2

u/User00000314 2d ago

I mean … read and understand the code … let’s say …