elmeraa-usgs / ds-pipelines-targets-2

https://lab.github.com/USGS-R/usgs-targets-tips-and-tricks
0 stars 0 forks source link

Learn the differences between different types of targets #7

Closed github-learning-lab[bot] closed 2 years ago

github-learning-lab[bot] commented 2 years ago

Targets

"Targets" are the main things that the targets package interacts with (if the name hadn't already implied that :zany_face:). They represent things that are made (they're also the vertices of the dependency graph). If you want to make a plot called plot.pdf, then that's a target. If you depend on a dataset called data.csv, that's a target (even if it already exists).

In targets, there are two main types:


:keyboard: Activity: Assign yourself to this issue to get started.


I'll sit patiently until you've assigned yourself to this one.

github-learning-lab[bot] commented 2 years ago

More details on object targets

As stated above, object targets are R objects that represent intermediate objects in an analysis.

Object targets are common in the example pipelines we have shown before. They are distinguished from file targets in the following ways:

These objects are often used because they offer a brevity advantage over files (e.g., you don't need to pass in a filename to the function) and preserve the classes and formatting of the data, which makes it a bit easier to keep dates, factors, and other special data types from changing when you write - and then later read in - a file (such as a .csv). Objects also give you the illusion that they aren't taking up space in your project directory and make workspaces look a bit tidier.

The "illusion" :tophat::rabbit: of objects not taking up space is because behind the scenes, these objects are actually written to file (.rds files, to be specific). You can see what exists under the hood with dir('_targets/objects'). The default is for targets to store these as .rds files. There are other formats that can be used to store the intermediate objects; if you're curious, check out the documentation for the format argument to tar_target().

You can take a look at that same object referenced in https://github.com/elmeraa/ds-pipelines-targets-2/issues/4 by using

readRDS('_targets/objects/map.config')
$missing_data
[1] "grey90"

$plot_CRS
[1] "+init=epsg:2163"

$wfs
[1] "http://cida.usgs.gov/gdp/geoserver/wfs"

$feature
[1] "derivative:wbdhu8_alb_simp"

$countBins
 [1]    0    1    2    5   10   20   50  100  200  500 1000

(Not as convenient as accessing the data with tar_read('map.config') instead, which is what we'd recommend).


:keyboard: Add a comment to this issue so we know you're ready to continue learning


I'll sit patiently until you've added a comment to this issue.

elmeraa-usgs commented 2 years ago

Adding comment to continue on

github-learning-lab[bot] commented 2 years ago

More details on file targets

File targets are very flexible and, of course, are also easy to share or store elsewhere.

Additionally, many file formats are either language agnostic (e.g., csv, tsv, txt, nc files) or are meant to be shared across languages, such as the feather format designed for exchange between R and Python.

When specifying a file target in a makefile, the path to the file needs to be either absolute or relative to the working directory that the _targets.R file is in.


Since file targets in the targets package are not the default and require you to add format = "file", you may feel deterred from using files as targets. It's true, the benefits of files are often small compared to the advantages of using objects. However, we still recommend that files be used liberally, especially for targets that you'll want to access outside of R (e.g., browsing figure files in Finder/Windows Explorer; opening a spatial data file in a GIS) or share with others (e.g., using outputs from one pipeline as inputs to another).


:keyboard: Activity: Close this issue when you are ready to move on to the next assignment


I'll sit patiently until this issue is closed.

github-learning-lab[bot] commented 2 years ago


When you are done poking around, check out the next issue.