From a2f71d4344e879fd850ca9830eafb1e6a780bfa7 Mon Sep 17 00:00:00 2001 From: Vaclav Petras Date: Thu, 19 Dec 2024 10:25:39 -0500 Subject: [PATCH 1/2] Update to PoPS Core with explicit casts (#206) This updates PoPS Core code to the latest version on main with explicit casts and conversions from floating point numbers to integers and booleans. --- inst/cpp/pops-core | 2 +- inst/include/actions.hpp | 19 +++---- inst/include/config.hpp | 9 +++- inst/include/deterministic_kernel.hpp | 6 ++- inst/include/host_pool.hpp | 46 +++++++++------- inst/include/network.hpp | 13 ++--- inst/include/pest_host_table.hpp | 2 +- inst/include/pest_pool.hpp | 37 ++++++------- inst/include/quarantine.hpp | 8 +-- inst/include/radial_kernel.hpp | 4 +- inst/include/raster.hpp | 77 ++++++++++++++++++++++++--- inst/include/simulation.hpp | 9 ++-- inst/include/soils.hpp | 2 +- inst/include/treatments.hpp | 38 +++++++------ 14 files changed, 175 insertions(+), 97 deletions(-) diff --git a/inst/cpp/pops-core b/inst/cpp/pops-core index 19eab166..1d3ff980 160000 --- a/inst/cpp/pops-core +++ b/inst/cpp/pops-core @@ -1 +1 @@ -Subproject commit 19eab166224bb5f75a285ec2d96920ec8e2d3f4c +Subproject commit 1d3ff980e4da276527183d6bbf21be271250d9aa diff --git a/inst/include/actions.hpp b/inst/include/actions.hpp index e92bbc54..1b05188e 100644 --- a/inst/include/actions.hpp +++ b/inst/include/actions.hpp @@ -90,16 +90,14 @@ class SpreadAction // From all the generated dispersers, some go to the soil in the // same cell and don't participate in the kernel-driven dispersal. auto dispersers_to_soil = - std::round(to_soil_percentage_ * dispersers_from_cell); + std::lround(to_soil_percentage_ * dispersers_from_cell); soil_pool_->dispersers_to(dispersers_to_soil, i, j, generator); dispersers_from_cell -= dispersers_to_soil; } - pests.set_dispersers_at(i, j, dispersers_from_cell); - pests.set_established_dispersers_at(i, j, dispersers_from_cell); + pests.set_dispersers_at(i, j, dispersers_from_cell, 0); } else { - pests.set_dispersers_at(i, j, 0); - pests.set_established_dispersers_at(i, j, 0); + pests.set_dispersers_at(i, j, 0, 0); } } } @@ -123,17 +121,15 @@ class SpreadAction if (pests.dispersers_at(i, j) > 0) { for (int k = 0; k < pests.dispersers_at(i, j); k++) { std::tie(row, col) = dispersal_kernel_(generator, i, j); - // if (row < 0 || row >= rows_ || col < 0 || col >= cols_) { if (host_pool.is_outside(row, col)) { pests.add_outside_disperser_at(row, col); - pests.remove_established_dispersers_at(i, j, 1); continue; } // Put a disperser to the host pool. auto dispersed = host_pool.disperser_to(row, col, generator.establishment()); - if (!dispersed) { - pests.remove_established_dispersers_at(i, j, 1); + if (dispersed) { + pests.add_established_dispersers_at(i, j, 1); } } } @@ -370,7 +366,8 @@ class MoveOverpopulatedPests // for leaving_percentage == 0.5 // 2 infected -> 1 leaving // 3 infected -> 1 leaving - int leaving = original_count * leaving_percentage_; + int leaving = + static_cast(std::floor(original_count * leaving_percentage_)); leaving = hosts.pests_from(i, j, leaving, generator.overpopulation()); if (row < 0 || row >= rows_ || col < 0 || col >= cols_) { pests.add_outside_dispersers_at(row, col, leaving); @@ -510,7 +507,7 @@ class Mortality void action(Hosts& hosts) { for (auto indices : hosts.suitable_cells()) { - if (action_mortality_) { + if (static_cast(action_mortality_)) { hosts.apply_mortality_at( indices[0], indices[1], mortality_rate_, mortality_time_lag_); } diff --git a/inst/include/config.hpp b/inst/include/config.hpp index 79ea58b1..a5382a9e 100644 --- a/inst/include/config.hpp +++ b/inst/include/config.hpp @@ -569,12 +569,19 @@ class Config for (const auto& row : values) { if (row.size() < 3) { throw std::invalid_argument( - "3 values are required for each pest-host table row"); + "3 values are required for each pest-host table row " + "(but row size is " + + std::to_string(row.size()) + ")"); } PestHostTableDataRow resulting_row; resulting_row.susceptibility = row[0]; resulting_row.mortality_rate = row[1]; resulting_row.mortality_time_lag = row[2]; + if (resulting_row.susceptibility < 0 || resulting_row.susceptibility > 1) { + throw std::invalid_argument( + "Susceptibility needs to be >=0 and <=1, not " + + std::to_string(resulting_row.susceptibility)); + } pest_host_table_data_.push_back(std::move(resulting_row)); } } diff --git a/inst/include/deterministic_kernel.hpp b/inst/include/deterministic_kernel.hpp index a3df69eb..edcd02a9 100644 --- a/inst/include/deterministic_kernel.hpp +++ b/inst/include/deterministic_kernel.hpp @@ -172,8 +172,10 @@ class DeterministicDispersalKernel // The invalid state is checked later, in this case using the kernel type. return; } - number_of_columns = ceil(max_distance / east_west_resolution) * 2 + 1; - number_of_rows = ceil(max_distance / north_south_resolution) * 2 + 1; + number_of_columns = + static_cast(ceil(max_distance / east_west_resolution)) * 2 + 1; + number_of_rows = + static_cast(ceil(max_distance / north_south_resolution)) * 2 + 1; Raster prob_size(number_of_rows, number_of_columns, 0); probability = prob_size; probability_copy = prob_size; diff --git a/inst/include/host_pool.hpp b/inst/include/host_pool.hpp index 2c600b31..efedc488 100644 --- a/inst/include/host_pool.hpp +++ b/inst/include/host_pool.hpp @@ -26,6 +26,7 @@ #include "environment_interface.hpp" #include "competency_table.hpp" #include "pest_host_table.hpp" +#include "utils.hpp" namespace pops { @@ -306,7 +307,8 @@ class HostPool : public HostPoolInterface } } else { - dispersers_from_cell = lambda * infected_at(row, col); + dispersers_from_cell = + static_cast(std::floor(lambda * infected_at(row, col))); } return dispersers_from_cell; } @@ -326,7 +328,17 @@ class HostPool : public HostPoolInterface if (pest_host_table_) { suitability *= pest_host_table_->susceptibility(this); } - return environment_.influence_suitability_at(row, col, suitability); + suitability = environment_.influence_suitability_at(row, col, suitability); + if (suitability < 0 || suitability > 1) { + throw std::invalid_argument( + "Suitability should be >=0 and <=1, not " + std::to_string(suitability) + + " (susceptible: " + std::to_string(susceptible_(row, col)) + + ", total population: " + + std::to_string(environment_.total_population_at(row, col)) + + ", susceptibility: " + + std::to_string(pest_host_table_->susceptibility(this)) + ")"); + } + return suitability; } /** @@ -477,16 +489,9 @@ class HostPool : public HostPoolInterface // Since suitable cells originally comes from the total hosts, check first total // hosts and proceed only if there was no host. if (total_hosts_(row_to, col_to) == 0) { - for (auto indices : suitable_cells_) { - int i = indices[0]; - int j = indices[1]; - // TODO: This looks like a bug. Flag is needed for found and push back - // should happen only after the loop. - if ((i == row_to) && (j == col_to)) { - std::vector added_index = {row_to, col_to}; - suitable_cells_.push_back(added_index); - break; - } + std::vector new_index = {row_to, col_to}; + if (!container_contains(suitable_cells_, new_index)) { + suitable_cells_.push_back(new_index); } } @@ -528,10 +533,10 @@ class HostPool : public HostPoolInterface void completely_remove_hosts_at( RasterIndex row, RasterIndex col, - double susceptible, - std::vector exposed, - double infected, - const std::vector& mortality) + int susceptible, + std::vector exposed, + int infected, + const std::vector& mortality) { if (susceptible > 0) susceptible_(row, col) = susceptible_(row, col) - susceptible; @@ -795,6 +800,9 @@ class HostPool : public HostPoolInterface * individuals is multiplied by the mortality rate to calculate the number of hosts * that die that time step. * + * If mortality rate is zero (<=0), no mortality is applied and mortality tracker + * vector stays as is, i.e., no hosts die. + * * To be used together with step_forward_mortality(). * * @param row Row index of the cell @@ -805,6 +813,8 @@ class HostPool : public HostPoolInterface void apply_mortality_at( RasterIndex row, RasterIndex col, double mortality_rate, int mortality_time_lag) { + if (mortality_rate <= 0) + return; int max_index = mortality_tracker_vector_.size() - mortality_time_lag - 1; for (int index = 0; index <= max_index; index++) { int mortality_in_index = 0; @@ -815,8 +825,8 @@ class HostPool : public HostPoolInterface mortality_in_index = mortality_tracker_vector_[index](row, col); } else { - mortality_in_index = - mortality_rate * mortality_tracker_vector_[index](row, col); + mortality_in_index = static_cast(std::floor( + mortality_rate * mortality_tracker_vector_[index](row, col))); } mortality_tracker_vector_[index](row, col) -= mortality_in_index; died_(row, col) += mortality_in_index; diff --git a/inst/include/network.hpp b/inst/include/network.hpp index 56caf671..6349cccb 100644 --- a/inst/include/network.hpp +++ b/inst/include/network.hpp @@ -58,7 +58,7 @@ class EdgeGeometry : public std::vector /** Get cost of the whole segment (edge). */ double cost() const { - if (total_cost_) + if (static_cast(total_cost_)) return total_cost_; // This is short for ((size - 2) + (2 * 1/2)) * cost per cell. return (this->size() - 1) * cost_per_cell_; @@ -72,7 +72,7 @@ class EdgeGeometry : public std::vector /** Get cost per cell for the segment (edge). */ double cost_per_cell() const { - if (total_cost_) + if (static_cast(total_cost_)) return total_cost_ / (this->size() - 1); return cost_per_cell_; } @@ -879,14 +879,9 @@ class Network auto node_1_id = node_id_from_text(node_1_text); auto node_2_id = node_id_from_text(node_2_text); if (node_1_id < 1 || node_2_id < 1) { - throw std::runtime_error(std::string( - "Node ID must be greater than zero: " + node_1_text + " " - + node_2_text)); - } - if (node_1_id == node_2_id) { throw std::runtime_error( - std::string("Edge cannot begin and end with the same node: ") - + node_1_text + " " + node_2_text); + std::string("Node ID must be greater than zero (node 1, node 2): ") + + node_1_text + ", " + node_2_text + ", line: " + line); } Segment segment; diff --git a/inst/include/pest_host_table.hpp b/inst/include/pest_host_table.hpp index 0d70ba3e..a0017e43 100644 --- a/inst/include/pest_host_table.hpp +++ b/inst/include/pest_host_table.hpp @@ -103,7 +103,7 @@ class PestHostTable * @param host Pointer to the host to get the information for * @return Mortality time lag value */ - double mortality_time_lag(const HostPool* host) const + int mortality_time_lag(const HostPool* host) const { auto host_index = environment_.host_index(host); return mortality_time_lags_.at(host_index); diff --git a/inst/include/pest_pool.hpp b/inst/include/pest_pool.hpp index 7eb3e6bb..e548cf3f 100644 --- a/inst/include/pest_pool.hpp +++ b/inst/include/pest_pool.hpp @@ -53,13 +53,17 @@ class PestPool {} /** * @brief Set number of dispersers + * * @param row Row number * @param col Column number - * @param value The new value + * @param dispersers Number of dispersers + * @param established_dispersers Number of established dispersers */ - void set_dispersers_at(RasterIndex row, RasterIndex col, int value) + void set_dispersers_at( + RasterIndex row, RasterIndex col, int dispersers, int established_dispersers) { - dispersers_(row, col) = value; + dispersers_(row, col) = dispersers; + established_dispersers_(row, col) = established_dispersers; } /** * @brief Return number of dispersers @@ -82,32 +86,21 @@ class PestPool { return dispersers_; } + /** - * @brief Set number of established dispersers + * @brief Add established dispersers * - * Established are dispersers which left cell (row, col) and established themselves - * elsewhere, i.e., origin of the established dispersers is tracked. + * Established dispersers are dispersers which left cell (row, col) and + * established themselves elsewhere, i.e., origin of the established dispersers + * is tracked. * * @param row Row number * @param col Column number - * @param value The new value - */ - void set_established_dispersers_at(RasterIndex row, RasterIndex col, int value) - { - established_dispersers_(row, col) = value; - } - // TODO: The following function should not be necessary because pests can't - // un-establish. It exists just because it mirrors how the raster was handled in the - // original Simulation code. - /** - * @brief Remove established dispersers - * @param row Row number - * @param col Column number - * @param count How many dispers to remove + * @param count How many dispersers to add */ - void remove_established_dispersers_at(RasterIndex row, RasterIndex col, int count) + void add_established_dispersers_at(RasterIndex row, RasterIndex col, int count) { - established_dispersers_(row, col) -= count; + established_dispersers_(row, col) += count; } /** * @brief Add a disperser which left the study area diff --git a/inst/include/quarantine.hpp b/inst/include/quarantine.hpp index 71da68b1..6720ab5c 100644 --- a/inst/include/quarantine.hpp +++ b/inst/include/quarantine.hpp @@ -147,20 +147,20 @@ class QuarantineEscapeAction DistDir closest; if (directions_.at(Direction::N) && (i - n) * north_south_resolution_ < mindist) { - mindist = (i - n) * north_south_resolution_; + mindist = static_cast(std::floor((i - n) * north_south_resolution_)); closest = std::make_tuple(mindist, Direction::N); } if (directions_.at(Direction::S) && (s - i) * north_south_resolution_ < mindist) { - mindist = (s - i) * north_south_resolution_; + mindist = static_cast(std::floor((s - i) * north_south_resolution_)); closest = std::make_tuple(mindist, Direction::S); } if (directions_.at(Direction::E) && (e - j) * west_east_resolution_ < mindist) { - mindist = (e - j) * west_east_resolution_; + mindist = static_cast(std::floor((e - j) * west_east_resolution_)); closest = std::make_tuple(mindist, Direction::E); } if (directions_.at(Direction::W) && (j - w) * west_east_resolution_ < mindist) { - mindist = (j - w) * west_east_resolution_; + mindist = static_cast(std::floor((j - w) * west_east_resolution_)); closest = std::make_tuple(mindist, Direction::W); } return closest; diff --git a/inst/include/radial_kernel.hpp b/inst/include/radial_kernel.hpp index b6e4722b..ce265cb9 100644 --- a/inst/include/radial_kernel.hpp +++ b/inst/include/radial_kernel.hpp @@ -198,8 +198,8 @@ class RadialDispersalKernel } theta = von_mises(generator); - row -= round(distance * cos(theta) / north_south_resolution); - col += round(distance * sin(theta) / east_west_resolution); + row -= lround(distance * cos(theta) / north_south_resolution); + col += lround(distance * sin(theta) / east_west_resolution); return std::make_tuple(row, col); } diff --git a/inst/include/raster.hpp b/inst/include/raster.hpp index 03ff60d8..54ccdc2c 100644 --- a/inst/include/raster.hpp +++ b/inst/include/raster.hpp @@ -275,7 +275,11 @@ class Raster } template - Raster& operator+=(OtherNumber value) + typename std::enable_if< + !(std::is_floating_point::value + && std::is_integral::value), + Raster&>::type + operator+=(OtherNumber value) { std::for_each( data_, data_ + (cols_ * rows_), [&value](Number& a) { a += value; }); @@ -283,7 +287,11 @@ class Raster } template - Raster& operator-=(OtherNumber value) + typename std::enable_if< + !(std::is_floating_point::value + && std::is_integral::value), + Raster&>::type + operator-=(OtherNumber value) { std::for_each( data_, data_ + (cols_ * rows_), [&value](Number& a) { a -= value; }); @@ -291,7 +299,11 @@ class Raster } template - Raster& operator*=(OtherNumber value) + typename std::enable_if< + !(std::is_floating_point::value + && std::is_integral::value), + Raster&>::type + operator*=(OtherNumber value) { std::for_each( data_, data_ + (cols_ * rows_), [&value](Number& a) { a *= value; }); @@ -299,13 +311,65 @@ class Raster } template - Raster& operator/=(OtherNumber value) + typename std::enable_if< + !(std::is_floating_point::value + && std::is_integral::value), + Raster&>::type + operator/=(OtherNumber value) { std::for_each( data_, data_ + (cols_ * rows_), [&value](Number& a) { a /= value; }); return *this; } + template + typename std::enable_if< + std::is_floating_point::value && std::is_integral::value, + Raster&>::type + operator+=(OtherNumber value) + { + std::for_each(data_, data_ + (cols_ * rows_), [&value](Number& a) { + a += static_cast(std::floor(value)); + }); + return *this; + } + + template + typename std::enable_if< + std::is_floating_point::value && std::is_integral::value, + Raster&>::type + operator-=(OtherNumber value) + { + std::for_each(data_, data_ + (cols_ * rows_), [&value](Number& a) { + a -= static_cast(std::floor(value)); + }); + return *this; + } + + template + typename std::enable_if< + std::is_floating_point::value && std::is_integral::value, + Raster&>::type + operator*=(OtherNumber value) + { + std::for_each(data_, data_ + (cols_ * rows_), [&value](Number& a) { + a *= static_cast(std::floor(value)); + }); + return *this; + } + + template + typename std::enable_if< + std::is_floating_point::value && std::is_integral::value, + Raster&>::type + operator/=(OtherNumber value) + { + std::for_each(data_, data_ + (cols_ * rows_), [&value](Number& a) { + a /= static_cast(std::floor(value)); + }); + return *this; + } + template typename std::enable_if< std::is_floating_point::value @@ -496,12 +560,13 @@ class Raster return out; } - friend inline Raster pow(Raster image, double value) + friend inline Raster pow(const Raster& image, double value) { image.for_each([value](Number& a) { a = std::pow(a, value); }); return image; } - friend inline Raster sqrt(Raster image) + + friend inline Raster sqrt(const Raster& image) { image.for_each([](Number& a) { a = std::sqrt(a); }); return image; diff --git a/inst/include/simulation.hpp b/inst/include/simulation.hpp index 582185bb..e4d5db8d 100644 --- a/inst/include/simulation.hpp +++ b/inst/include/simulation.hpp @@ -35,10 +35,11 @@ namespace pops { /*! A class to control the spread simulation. * - * \deprecated - * The class is deprecated in favor of individual action classes and a higher-level - * Model. The class corresponding to the original Simulation class before too much code - * accumulated in Simulation is SpreadAction. The class is now used only in tests. + * @note + * The class is deprecated for external use in favor of individual action classes and a + * higher-level Model. The class corresponding to the original Simulation class before + * too much code accumulated in Simulation is SpreadAction. The class is now used only + * in tests. * * The Simulation class handles the mechanics of the model, but the * timing of the events or steps should be handled outside of this diff --git a/inst/include/soils.hpp b/inst/include/soils.hpp index 8e23435d..3c4802d3 100644 --- a/inst/include/soils.hpp +++ b/inst/include/soils.hpp @@ -90,7 +90,7 @@ class SoilPool } } else { - dispersers = lambda * count; + dispersers = static_cast(std::floor(lambda * count)); } auto draw = draw_n_from_cohorts(*rasters_, dispersers, row, col, generator); size_t index = 0; diff --git a/inst/include/treatments.hpp b/inst/include/treatments.hpp index 39381a65..d6f15df3 100644 --- a/inst/include/treatments.hpp +++ b/inst/include/treatments.hpp @@ -136,7 +136,7 @@ class BaseTreatment : public AbstractTreatment return count * this->map_(i, j); } else if (application == TreatmentApplication::AllInfectedInCell) { - return this->map_(i, j) ? count : 0; + return static_cast(this->map_(i, j)) ? count : 0; } throw std::runtime_error( "BaseTreatment::get_treated: unknown TreatmentApplication"); @@ -173,18 +173,20 @@ class SimpleTreatment : public BaseTreatment for (auto indices : host_pool.suitable_cells()) { int i = indices[0]; int j = indices[1]; - double remove_susceptible = this->get_treated( - i, j, host_pool.susceptible_at(i, j), TreatmentApplication::Ratio); - double remove_infected = - this->get_treated(i, j, host_pool.infected_at(i, j)); - std::vector remove_mortality; + int remove_susceptible = static_cast(std::ceil(this->get_treated( + i, j, host_pool.susceptible_at(i, j), TreatmentApplication::Ratio))); + int remove_infected = static_cast( + std::ceil(this->get_treated(i, j, host_pool.infected_at(i, j)))); + std::vector remove_mortality; for (int count : host_pool.mortality_by_group_at(i, j)) { - remove_mortality.push_back(this->get_treated(i, j, count)); + remove_mortality.push_back( + static_cast(std::ceil(this->get_treated(i, j, count)))); } - std::vector remove_exposed; + std::vector remove_exposed; for (int count : host_pool.exposed_by_group_at(i, j)) { - remove_exposed.push_back(this->get_treated(i, j, count)); + remove_exposed.push_back( + static_cast(std::ceil(this->get_treated(i, j, count)))); } host_pool.completely_remove_hosts_at( i, @@ -241,23 +243,29 @@ class PesticideTreatment : public BaseTreatment // Given how the original code was written (everything was first converted // to ints and subtractions happened only afterwards), this needs ints, // not doubles to pass the r.pops.spread test (unlike the other code which - // did substractions before converting to ints). - int susceptible_resistant = this->get_treated( - i, j, host_pool.susceptible_at(i, j), TreatmentApplication::Ratio); + // did substractions before converting to ints), so the conversion to ints + // happened only later. Now get_treated returns double and floor or ceil is + // applied to the result to get the same results as before. + int susceptible_resistant = static_cast(std::floor(this->get_treated( + i, j, host_pool.susceptible_at(i, j), TreatmentApplication::Ratio))); std::vector resistant_exposed_list; for (const auto& number : host_pool.exposed_by_group_at(i, j)) { - resistant_exposed_list.push_back(this->get_treated(i, j, number)); + resistant_exposed_list.push_back( + static_cast(std::floor(this->get_treated(i, j, number)))); } std::vector resistant_mortality_list; for (const auto& number : host_pool.mortality_by_group_at(i, j)) { - resistant_mortality_list.push_back(this->get_treated(i, j, number)); + resistant_mortality_list.push_back( + static_cast(std::floor(this->get_treated(i, j, number)))); } + int infected = static_cast( + std::floor(this->get_treated(i, j, host_pool.infected_at(i, j)))); host_pool.make_resistant_at( i, j, susceptible_resistant, resistant_exposed_list, - this->get_treated(i, j, host_pool.infected_at(i, j)), + infected, resistant_mortality_list); } } From 9354e1f571844629ded6fea58038800e4b1355ff Mon Sep 17 00:00:00 2001 From: Vaclav Petras Date: Thu, 19 Dec 2024 11:15:04 -0500 Subject: [PATCH 2/2] Use rounding for non-stochastic dispersers from cell (#205) * Use rounding for non-stochastic dispersers from cell Update to PoPS Core with flooring instead of rounding in computation of number of non-stochastic dispersers from cell. * update renv * update github test coverage yaml * update documentation * test lock fiile with dev dependicies. * update workflow for checks * update to newest pops-core main --------- Co-authored-by: unknown --- .github/workflows/R-CMD-check.yaml | 21 +- .github/workflows/test-coverage.yaml | 14 +- DESCRIPTION | 2 +- inst/cpp/pops-core | 2 +- inst/include/host_pool.hpp | 3 +- man/calibrate.Rd | 13 +- man/pops.Rd | 13 +- man/pops_model.Rd | 17 +- man/pops_multirun.Rd | 13 +- man/validate.Rd | 13 +- renv.lock | 279 +++++++++--------- renv/activate.R | 423 +++++++++++++++++++-------- 12 files changed, 510 insertions(+), 303 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 183469fe..e0d8fc05 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -6,7 +6,7 @@ on: pull_request: branches: [main] -name: R-CMD-check +name: R-CMD-check.yaml jobs: R-CMD-check: @@ -38,13 +38,22 @@ jobs: - name: Install macOS system dependencies if: runner.os == 'macOS' - run: brew install gdal proj fribidi + run: brew install gdal proj + + - name: Install Linux system dependencies + if: runner.os == 'Linux' + run: | + sudo apt install \ + libudunits2-dev \ + libgdal-dev \ + libgeos-dev \ + libproj-dev \ - uses: r-lib/actions/setup-renv@v2 - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: any::rcmdcheck - needs: check + # - uses: r-lib/actions/setup-r-dependencies@v2 + # with: + # extra-packages: any::rcmdcheck + # needs: check - uses: r-lib/actions/check-r-package@v2 with: diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index c17d1fe5..17fceb9a 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -6,7 +6,7 @@ on: pull_request: branches: [main, master] -name: test-coverage +name: test-coverage.yaml jobs: test-coverage: @@ -15,7 +15,9 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-pandoc@v2 - uses: r-lib/actions/setup-r@v2 with: @@ -23,10 +25,10 @@ jobs: - uses: r-lib/actions/setup-renv@v2 - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: any::covr - needs: coverage + # - uses: r-lib/actions/setup-r-dependencies@v2 + # with: + # extra-packages: any::covr + # needs: coverage - name: Test coverage run: | diff --git a/DESCRIPTION b/DESCRIPTION index 46041320..1efe1e1b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -52,5 +52,5 @@ Suggests: pkgdown LinkingTo: Rcpp -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 VignetteBuilder: knitr diff --git a/inst/cpp/pops-core b/inst/cpp/pops-core index 1d3ff980..9345ef10 160000 --- a/inst/cpp/pops-core +++ b/inst/cpp/pops-core @@ -1 +1 @@ -Subproject commit 1d3ff980e4da276527183d6bbf21be271250d9aa +Subproject commit 9345ef10c6aa1a4f30995b5855a6b793b647b597 diff --git a/inst/include/host_pool.hpp b/inst/include/host_pool.hpp index efedc488..ec7221d5 100644 --- a/inst/include/host_pool.hpp +++ b/inst/include/host_pool.hpp @@ -307,8 +307,7 @@ class HostPool : public HostPoolInterface } } else { - dispersers_from_cell = - static_cast(std::floor(lambda * infected_at(row, col))); + dispersers_from_cell = std::lround(lambda * infected_at(row, col)); } return dispersers_from_cell; } diff --git a/man/calibrate.Rd b/man/calibrate.Rd index 931205cd..67a4a1dd 100644 --- a/man/calibrate.Rd +++ b/man/calibrate.Rd @@ -154,14 +154,17 @@ We recommend running at least 1,000 but the greater this number the more accurate the model parameters selected will be.} \item{pest_host_table}{The file path to a csv that has these columns in this order: host, -susceptibility, mortality rate, and mortality time lag as columns with each row being the -species. Host species must be in the same order in the host_file_list, infected_file_list, -pest_host_table rows, and competency_table columns. The host column is only used for metadata -and labeling output files.} +susceptibility_mean, susceptibility_sd, mortality_rate, mortality_rate_mean, +and mortality_time_lag as columns with each row being the species. Host species +must be in the same order in the host_file_list, infected_file_list, +pest_host_table rows, and competency_table columns. The host column is character +string of the species name, and is only used for metadata and labeling output files. +Susceptibility and mortality_rate values must be between 0 and 1.} \item{competency_table}{A csv with the hosts as the first n columns (n being the number of hosts) and the last column being the competency value. Each row is a set of Boolean for host presence -and the competency value (between 0 and 1) for that combination of hosts in a cell.} +and the competency value (between 0 and 1) for that combination of hosts in a cell. +#'} \item{infected_file_list}{paths to raster files with initial infections and standard deviation for each host can be based in 2 formats (a single file with number of hosts or a single file with diff --git a/man/pops.Rd b/man/pops.Rd index 45798c51..03d785f2 100644 --- a/man/pops.Rd +++ b/man/pops.Rd @@ -110,14 +110,17 @@ percent_natural_dispersal, anthropogenic_dispersal_distance, natural kappa, anth network_min_distance, and network_max_distance) Should be 8x8 matrix.} \item{pest_host_table}{The file path to a csv that has these columns in this order: host, -susceptibility, mortality rate, and mortality time lag as columns with each row being the -species. Host species must be in the same order in the host_file_list, infected_file_list, -pest_host_table rows, and competency_table columns. The host column is only used for metadata -and labeling output files.} +susceptibility_mean, susceptibility_sd, mortality_rate, mortality_rate_mean, +and mortality_time_lag as columns with each row being the species. Host species +must be in the same order in the host_file_list, infected_file_list, +pest_host_table rows, and competency_table columns. The host column is character +string of the species name, and is only used for metadata and labeling output files. +Susceptibility and mortality_rate values must be between 0 and 1.} \item{competency_table}{A csv with the hosts as the first n columns (n being the number of hosts) and the last column being the competency value. Each row is a set of Boolean for host presence -and the competency value (between 0 and 1) for that combination of hosts in a cell.} +and the competency value (between 0 and 1) for that combination of hosts in a cell. +#'} \item{temp}{boolean that allows the use of temperature coefficients to modify spread (TRUE or FALSE)} diff --git a/man/pops_model.Rd b/man/pops_model.Rd index 8f50759b..969b053e 100644 --- a/man/pops_model.Rd +++ b/man/pops_model.Rd @@ -93,8 +93,8 @@ pops_model( compatibility option). Default is FALSE.} \item{random_seeds}{vector of random seeds in the order of "disperser_generation", -"natural_dispersal", "anthropogenic_dispersal", "establishment", "weather", "movement", -"overpopulation", "survival_rate", "soil"} +"natural_dispersal", "anthropogenic_dispersal", "establishment", "weather", +"lethal_temperature", "movement", "overpopulation", "survival_rate", "soil"} \item{use_lethal_temperature}{A boolean to answer the question: does your pest or pathogen have a temperature at which it cannot survive? (TRUE or FALSE)} @@ -121,13 +121,16 @@ mortality_tracker for that host.} \item{competency_table}{A csv with the hosts as the first n columns (n being the number of hosts) and the last column being the competency value. Each row is a set of Boolean for host presence -and the competency value (between 0 and 1) for that combination of hosts in a cell.} +and the competency value (between 0 and 1) for that combination of hosts in a cell. +#'} \item{pest_host_table}{The file path to a csv that has these columns in this order: host, -susceptibility, mortality rate, and mortality time lag as columns with each row being the -species. Host species must be in the same order in the host_file_list, infected_file_list, -pest_host_table rows, and competency_table columns. The host column is only used for metadata -and labeling output files.} +susceptibility_mean, susceptibility_sd, mortality_rate, mortality_rate_mean, +and mortality_time_lag as columns with each row being the species. Host species +must be in the same order in the host_file_list, infected_file_list, +pest_host_table rows, and competency_table columns. The host column is character +string of the species name, and is only used for metadata and labeling output files. +Susceptibility and mortality_rate values must be between 0 and 1.} \item{mortality_on}{Boolean to indicate if mortality is used} diff --git a/man/pops_multirun.Rd b/man/pops_multirun.Rd index a5c028e9..b619a7d5 100644 --- a/man/pops_multirun.Rd +++ b/man/pops_multirun.Rd @@ -114,14 +114,17 @@ percent_natural_dispersal, anthropogenic_dispersal_distance, natural kappa, anth network_min_distance, and network_max_distance) Should be 8x8 matrix.} \item{pest_host_table}{The file path to a csv that has these columns in this order: host, -susceptibility, mortality rate, and mortality time lag as columns with each row being the -species. Host species must be in the same order in the host_file_list, infected_file_list, -pest_host_table rows, and competency_table columns. The host column is only used for metadata -and labeling output files.} +susceptibility_mean, susceptibility_sd, mortality_rate, mortality_rate_mean, +and mortality_time_lag as columns with each row being the species. Host species +must be in the same order in the host_file_list, infected_file_list, +pest_host_table rows, and competency_table columns. The host column is character +string of the species name, and is only used for metadata and labeling output files. +Susceptibility and mortality_rate values must be between 0 and 1.} \item{competency_table}{A csv with the hosts as the first n columns (n being the number of hosts) and the last column being the competency value. Each row is a set of Boolean for host presence -and the competency value (between 0 and 1) for that combination of hosts in a cell.} +and the competency value (between 0 and 1) for that combination of hosts in a cell. +#'} \item{temp}{boolean that allows the use of temperature coefficients to modify spread (TRUE or FALSE)} diff --git a/man/validate.Rd b/man/validate.Rd index 722853f2..06875f93 100644 --- a/man/validate.Rd +++ b/man/validate.Rd @@ -106,14 +106,17 @@ If not set uses the # of CPU cores - 1. must be an integer >= 1} calibration function (posterior covariance matrix)} \item{pest_host_table}{The file path to a csv that has these columns in this order: host, -susceptibility, mortality rate, and mortality time lag as columns with each row being the -species. Host species must be in the same order in the host_file_list, infected_file_list, -pest_host_table rows, and competency_table columns. The host column is only used for metadata -and labeling output files.} +susceptibility_mean, susceptibility_sd, mortality_rate, mortality_rate_mean, +and mortality_time_lag as columns with each row being the species. Host species +must be in the same order in the host_file_list, infected_file_list, +pest_host_table rows, and competency_table columns. The host column is character +string of the species name, and is only used for metadata and labeling output files. +Susceptibility and mortality_rate values must be between 0 and 1.} \item{competency_table}{A csv with the hosts as the first n columns (n being the number of hosts) and the last column being the competency value. Each row is a set of Boolean for host presence -and the competency value (between 0 and 1) for that combination of hosts in a cell.} +and the competency value (between 0 and 1) for that combination of hosts in a cell. +#'} \item{infected_file_list}{paths to raster files with initial infections and standard deviation for each host can be based in 2 formats (a single file with number of hosts or a single file with diff --git a/renv.lock b/renv.lock index 55844db0..5c6b2354 100644 --- a/renv.lock +++ b/renv.lock @@ -1,6 +1,6 @@ { "R": { - "Version": "4.3.0", + "Version": "4.4.1", "Repositories": [ { "Name": "CRAN", @@ -11,7 +11,7 @@ "Packages": { "MASS": { "Package": "MASS", - "Version": "7.3-60.0.1", + "Version": "7.3-61", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -22,11 +22,11 @@ "stats", "utils" ], - "Hash": "b765b28387acc8ec9e9c1530713cb19c" + "Hash": "0cafd6f0500e5deba33be22c46bf6055" }, "Matrix": { "Package": "Matrix", - "Version": "1.6-5", + "Version": "1.7-0", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -39,7 +39,7 @@ "stats", "utils" ], - "Hash": "8c7115cd3a0e048bda2a7cd110549f7a" + "Hash": "1920b2f11133b12350024297d8a4ff4a" }, "Metrics": { "Package": "Metrics", @@ -70,18 +70,18 @@ }, "Rcpp": { "Package": "Rcpp", - "Version": "1.0.12", + "Version": "1.0.13-1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "methods", "utils" ], - "Hash": "5ea2700d21e038ace58269ecdbeb9ec0" + "Hash": "6b868847b365672d6c1677b1608da9ed" }, "RcppArmadillo": { "Package": "RcppArmadillo", - "Version": "0.12.8.1.0", + "Version": "14.2.2-1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -91,11 +91,11 @@ "stats", "utils" ], - "Hash": "e78bbbb81a5dcd71a4bd3268d6ede0b1" + "Hash": "9da7c242d94a8419d045f6b3a64b9765" }, "XML": { "Package": "XML", - "Version": "3.99-0.16.1", + "Version": "3.99-0.17", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -103,17 +103,17 @@ "methods", "utils" ], - "Hash": "da3098169c887914551b607c66fe2a28" + "Hash": "bc2a8a1139d8d4bd9c46086708945124" }, "askpass": { "Package": "askpass", - "Version": "1.2.0", + "Version": "1.2.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "sys" ], - "Hash": "cad6cf7f1d5f6e906700b9d3e718c796" + "Hash": "c39f4155b3ceb1a9a2799d700fbd4b6a" }, "aws.s3": { "Package": "aws.s3", @@ -155,23 +155,24 @@ }, "brio": { "Package": "brio", - "Version": "1.1.4", + "Version": "1.1.5", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R" ], - "Hash": "68bd2b066e1fe780bbf62fc8bcc36de3" + "Hash": "c1ee497a6d999947c2c224ae46799b1a" }, "bslib": { "Package": "bslib", - "Version": "0.6.2", + "Version": "0.8.0", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "base64enc", "cachem", + "fastmap", "grDevices", "htmltools", "jquerylib", @@ -182,18 +183,18 @@ "rlang", "sass" ], - "Hash": "7fb73102553e0c8e478bfaa0ccebe479" + "Hash": "b299c6741ca9746fb227debcb0f9fb6c" }, "cachem": { "Package": "cachem", - "Version": "1.0.8", + "Version": "1.1.0", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "fastmap", "rlang" ], - "Hash": "c35768291560ce302c0a6589f92e837d" + "Hash": "cd9a672193789068eb5a2aad65a0dedf" }, "callr": { "Package": "callr", @@ -210,28 +211,28 @@ }, "cli": { "Package": "cli", - "Version": "3.6.2", + "Version": "3.6.3", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "utils" ], - "Hash": "1216ac65ac55ec0058a6f75d7ca0fd52" + "Hash": "b21916dd77a27642b447374a5d30ecf3" }, "codetools": { "Package": "codetools", - "Version": "0.2-19", + "Version": "0.2-20", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R" ], - "Hash": "c089a619a7fae175d149d89164f8c7d8" + "Hash": "61e097f35917d342622f21cdc79c256e" }, "colorspace": { "Package": "colorspace", - "Version": "2.1-0", + "Version": "2.1-1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -241,14 +242,14 @@ "methods", "stats" ], - "Hash": "f20c47fd52fae58b4e377c37bb8c335b" + "Hash": "d954cb1c57e8d8b756165d7ba18aa55a" }, "commonmark": { "Package": "commonmark", - "Version": "1.9.1", + "Version": "1.9.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "5d8225445acb167abf7797de48b2ee3c" + "Hash": "14eb0596f987c71535d07c3aff814742" }, "covr": { "Package": "covr", @@ -272,17 +273,17 @@ }, "cpp11": { "Package": "cpp11", - "Version": "0.4.7", + "Version": "0.5.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R" ], - "Hash": "5a295d7d963cc5035284dcdbaf334f4e" + "Hash": "9df43854f1c84685d095ed6270b52387" }, "crayon": { "Package": "crayon", - "Version": "1.5.2", + "Version": "1.5.3", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -290,28 +291,28 @@ "methods", "utils" ], - "Hash": "e8a1e41acf02548751f45c718d55aa6a" + "Hash": "859d96e65ef198fd43e82b9628d593ef" }, "curl": { "Package": "curl", - "Version": "5.2.1", + "Version": "6.0.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R" ], - "Hash": "411ca2c03b1ce5f548345d2fc2685f7a" + "Hash": "e8ba62486230951fcd2b881c5be23f96" }, "data.table": { "Package": "data.table", - "Version": "1.15.2", + "Version": "1.16.4", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "methods" ], - "Hash": "536dfe4ac4093b5d115caed7a1a7223b" + "Hash": "38bbf05fc2503143db4c734a7e5cab66" }, "desc": { "Package": "desc", @@ -343,14 +344,14 @@ }, "digest": { "Package": "digest", - "Version": "0.6.35", + "Version": "0.6.37", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "utils" ], - "Hash": "698ece7ba5a4fa4559e3d537e7ec3d31" + "Hash": "33698c4b3127fc9f506654607fb73676" }, "doParallel": { "Package": "doParallel", @@ -368,7 +369,7 @@ }, "downlit": { "Package": "downlit", - "Version": "0.4.3", + "Version": "0.4.4", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -384,18 +385,17 @@ "withr", "yaml" ], - "Hash": "14fa1f248b60ed67e1f5418391a17b14" + "Hash": "45a6a596bf0108ee1ff16a040a2df897" }, "evaluate": { "Package": "evaluate", - "Version": "0.23", + "Version": "1.0.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ - "R", - "methods" + "R" ], - "Hash": "daf4a1246be12c1fa8c7705a0935c1a0" + "Hash": "3fd29944b231036ad67c3edb32e02201" }, "fansi": { "Package": "fansi", @@ -411,21 +411,21 @@ }, "farver": { "Package": "farver", - "Version": "2.1.1", + "Version": "2.1.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "8106d78941f34855c440ddb946b8f7a5" + "Hash": "680887028577f3fa2a81e410ed0d6e42" }, "fastmap": { "Package": "fastmap", - "Version": "1.1.1", + "Version": "1.2.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "f7736a18de97dea803bde0a2daaafb27" + "Hash": "aa5e1cd11c2d15497494c5292d7ffcc8" }, "fontawesome": { "Package": "fontawesome", - "Version": "0.5.2", + "Version": "0.5.3", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -433,7 +433,7 @@ "htmltools", "rlang" ], - "Hash": "c2efdd5f0bcd1ea861c2d4e2a883a67d" + "Hash": "bd1297f9b5b1fc1372d19e2c4cd82215" }, "foreach": { "Package": "foreach", @@ -450,14 +450,14 @@ }, "fs": { "Package": "fs", - "Version": "1.6.3", + "Version": "1.6.5", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "methods" ], - "Hash": "47b5f30c720c23999b913a1a635cf0bb" + "Hash": "7f48af39fa27711ea5fbd183b399920d" }, "generics": { "Package": "generics", @@ -472,7 +472,7 @@ }, "ggplot2": { "Package": "ggplot2", - "Version": "3.5.0", + "Version": "3.5.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -493,22 +493,22 @@ "vctrs", "withr" ], - "Hash": "52ef83f93f74833007f193b2d4c159a2" + "Hash": "44c6a2f8202d5b7e878ea274b1092426" }, "glue": { "Package": "glue", - "Version": "1.7.0", + "Version": "1.8.0", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "methods" ], - "Hash": "e0b3a53876554bd45879e596cdb10a52" + "Hash": "5899f1eaa825580172bb56c08266f37c" }, "gtable": { "Package": "gtable", - "Version": "0.3.4", + "Version": "0.3.6", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -517,24 +517,25 @@ "glue", "grid", "lifecycle", - "rlang" + "rlang", + "stats" ], - "Hash": "b29cf3031f49b04ab9c852c912547eef" + "Hash": "de949855009e2d4d0e52a844e30617ae" }, "highr": { "Package": "highr", - "Version": "0.10", + "Version": "0.11", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "xfun" ], - "Hash": "06230136b2d2b9ba5805e1963fa6e890" + "Hash": "d65ba49117ca223614f71b60d85b8ab7" }, "htmltools": { "Package": "htmltools", - "Version": "0.5.8", + "Version": "0.5.8.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -546,7 +547,7 @@ "rlang", "utils" ], - "Hash": "149431ee39aba5bdc264112c8ff94444" + "Hash": "81d371a9cc60640e74e4ab6ac46dcedc" }, "httr": { "Package": "httr", @@ -563,6 +564,27 @@ ], "Hash": "ac107251d9d9fd72f0ca8049988f1d7f" }, + "httr2": { + "Package": "httr2", + "Version": "1.0.7", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R", + "R6", + "cli", + "curl", + "glue", + "lifecycle", + "magrittr", + "openssl", + "rappdirs", + "rlang", + "vctrs", + "withr" + ], + "Hash": "5a76da345ed4f3e6430517e08441edaf" + }, "isoband": { "Package": "isoband", "Version": "0.2.7", @@ -597,17 +619,17 @@ }, "jsonlite": { "Package": "jsonlite", - "Version": "1.8.8", + "Version": "1.8.9", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "methods" ], - "Hash": "e1b9c55281c5adc4dd113652d9e26768" + "Hash": "4e993b65c2c3ffbffce7bb3e2c6f832b" }, "knitr": { "Package": "knitr", - "Version": "1.45", + "Version": "1.49", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -619,7 +641,7 @@ "xfun", "yaml" ], - "Hash": "1ec462871063897135c1bcbe0fc8f07d" + "Hash": "9fcb189926d93c636dea94fbe4f44480" }, "labeling": { "Package": "labeling", @@ -634,7 +656,7 @@ }, "landscapemetrics": { "Package": "landscapemetrics", - "Version": "2.1.1", + "Version": "2.1.4", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -648,7 +670,7 @@ "terra", "tibble" ], - "Hash": "eefed06a6d4472f65ae0a990928db27d" + "Hash": "8ff90268cbc15a3c7ed5bddf7c53c600" }, "lattice": { "Package": "lattice", @@ -690,7 +712,7 @@ }, "lubridate": { "Package": "lubridate", - "Version": "1.9.3", + "Version": "1.9.4", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -699,7 +721,7 @@ "methods", "timechange" ], - "Hash": "680ad542fbcf801442c83a6ac5a2126c" + "Hash": "be38bc740fc51783a78edb5a157e4104" }, "magrittr": { "Package": "magrittr", @@ -713,7 +735,7 @@ }, "markdown": { "Package": "markdown", - "Version": "1.12", + "Version": "1.13", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -722,7 +744,7 @@ "utils", "xfun" ], - "Hash": "765cf53992401b3b6c297b69e1edb8bd" + "Hash": "074efab766a9d6360865ad39512f2a7e" }, "memoise": { "Package": "memoise", @@ -764,14 +786,14 @@ }, "munsell": { "Package": "munsell", - "Version": "0.5.0", + "Version": "0.5.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "colorspace", "methods" ], - "Hash": "6dfe8bf774944bd5595785e3229d8771" + "Hash": "4fd8900853b746af55b81fda99da7695" }, "nlme": { "Package": "nlme", @@ -789,13 +811,13 @@ }, "openssl": { "Package": "openssl", - "Version": "2.1.1", + "Version": "2.2.2", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "askpass" ], - "Hash": "2a0dc8c6adfb6f032e4d4af82d258ab5" + "Hash": "d413e0fef796c9401a4419485f709ca1" }, "pillar": { "Package": "pillar", @@ -816,7 +838,7 @@ }, "pkgbuild": { "Package": "pkgbuild", - "Version": "1.4.4", + "Version": "1.4.5", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -827,7 +849,7 @@ "desc", "processx" ], - "Hash": "a29e8e134a460a01e0ca67a4763c595b" + "Hash": "30eaaab94db72652e72e3475c1b55278" }, "pkgconfig": { "Package": "pkgconfig", @@ -841,7 +863,7 @@ }, "pkgdown": { "Package": "pkgdown", - "Version": "2.0.7", + "Version": "2.1.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -852,11 +874,11 @@ "desc", "digest", "downlit", + "fontawesome", "fs", - "httr", + "httr2", "jsonlite", - "magrittr", - "memoise", + "openssl", "purrr", "ragg", "rlang", @@ -867,28 +889,29 @@ "xml2", "yaml" ], - "Hash": "16fa15449c930bf3a7761d3c68f8abf9" + "Hash": "df2912d5873422b55a13002510f02c9f" }, "pkgload": { "Package": "pkgload", - "Version": "1.3.4", + "Version": "1.4.0", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "cli", - "crayon", "desc", "fs", "glue", + "lifecycle", "methods", "pkgbuild", + "processx", "rlang", "rprojroot", "utils", "withr" ], - "Hash": "876c618df5ae610be84356d5d7a5d124" + "Hash": "2ec30ffbeec83da57655b850cf2d3e0e" }, "praise": { "Package": "praise", @@ -922,14 +945,14 @@ }, "ps": { "Package": "ps", - "Version": "1.7.6", + "Version": "1.8.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "utils" ], - "Hash": "dd2b9319ee0656c8acf45c7f40c59de7" + "Hash": "b4404b1de13758dea1c0484ad0d48563" }, "purrr": { "Package": "purrr", @@ -948,7 +971,7 @@ }, "qpdf": { "Package": "qpdf", - "Version": "1.3.3", + "Version": "1.3.4", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -956,18 +979,18 @@ "askpass", "curl" ], - "Hash": "479ebe6dca0a57c63d306e1b6a4bf30c" + "Hash": "949ca99a6d20c4ff8212026b3b5559af" }, "ragg": { "Package": "ragg", - "Version": "1.3.0", + "Version": "1.3.3", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "systemfonts", "textshaping" ], - "Hash": "082e1a198e3329d571f4448ef0ede4bc" + "Hash": "0595fe5e47357111f29ad19101c7d271" }, "rappdirs": { "Package": "rappdirs", @@ -1001,16 +1024,6 @@ ], "Hash": "8f25ebe2ec38b1f2aef3b0d2ef76f6c4" }, - "rematch2": { - "Package": "rematch2", - "Version": "2.1.2", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "tibble" - ], - "Hash": "76c9e04c712a05848ae7a23d2f170a40" - }, "remotes": { "Package": "remotes", "Version": "2.5.0", @@ -1047,14 +1060,14 @@ }, "rlang": { "Package": "rlang", - "Version": "1.1.3", + "Version": "1.1.4", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "utils" ], - "Hash": "42548638fae05fd9a9b5f3f437fbbbe2" + "Hash": "3eec01f8b1dee337674b2e34ab1f9bc1" }, "rlist": { "Package": "rlist", @@ -1072,7 +1085,7 @@ }, "rmarkdown": { "Package": "rmarkdown", - "Version": "2.26", + "Version": "2.29", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -1091,7 +1104,7 @@ "xfun", "yaml" ], - "Hash": "9b148e7f95d33aac01f31282d49e4f44" + "Hash": "df99277f63d01c34e95e3d2f06a79736" }, "rprojroot": { "Package": "rprojroot", @@ -1152,25 +1165,26 @@ }, "sys": { "Package": "sys", - "Version": "3.4.2", + "Version": "3.4.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "3a1be13d68d47a8cd0bfd74739ca1555" + "Hash": "de342ebfebdbf40477d0758d05426646" }, "systemfonts": { "Package": "systemfonts", - "Version": "1.0.6", + "Version": "1.1.0", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", - "cpp11" + "cpp11", + "lifecycle" ], - "Hash": "6d538cff441f0f1f36db2209ac7495ac" + "Hash": "213b6b8ed5afbf934843e6c3b090d418" }, "terra": { "Package": "terra", - "Version": "1.7-71", + "Version": "1.8-5", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -1178,11 +1192,11 @@ "Rcpp", "methods" ], - "Hash": "e8611881ab70a4fb7a1f629b31e6fcff" + "Hash": "6e3f2c53b58161327fe5787fe1a63d02" }, "testthat": { "Package": "testthat", - "Version": "3.2.1", + "Version": "3.2.2", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -1207,19 +1221,20 @@ "waldo", "withr" ], - "Hash": "4767a686ebe986e6cb01d075b3f09729" + "Hash": "6773967afbe2f74c87021e72c1bb05c0" }, "textshaping": { "Package": "textshaping", - "Version": "0.3.7", + "Version": "0.4.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "cpp11", + "lifecycle", "systemfonts" ], - "Hash": "997aac9ad649e0ef3b97f96cddd5622b" + "Hash": "573e0d015b7fc3e555f83e254cad7533" }, "tibble": { "Package": "tibble", @@ -1253,13 +1268,13 @@ }, "tinytex": { "Package": "tinytex", - "Version": "0.50", + "Version": "0.54", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "xfun" ], - "Hash": "be7a76845222ad20adb761f462eed3ea" + "Hash": "3ec7e3ddcacc2d34a9046941222bf94d" }, "utf8": { "Package": "utf8", @@ -1297,21 +1312,18 @@ }, "waldo": { "Package": "waldo", - "Version": "0.5.2", + "Version": "0.6.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "cli", "diffobj", - "fansi", "glue", "methods", - "rematch2", - "rlang", - "tibble" + "rlang" ], - "Hash": "c7d3fd6d29ab077cbac8f0e2751449e6" + "Hash": "52f574062a7b66e56926988c3fbdb3b7" }, "whisker": { "Package": "whisker", @@ -1322,7 +1334,7 @@ }, "withr": { "Package": "withr", - "Version": "3.0.0", + "Version": "3.0.2", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -1330,19 +1342,20 @@ "grDevices", "graphics" ], - "Hash": "d31b6c62c10dcf11ec530ca6b0dd5d35" + "Hash": "cc2d62c76458d425210d1eb1478b30b4" }, "xfun": { "Package": "xfun", - "Version": "0.43", + "Version": "0.49", "Source": "Repository", "Repository": "CRAN", "Requirements": [ + "R", "grDevices", "stats", "tools" ], - "Hash": "ab6371d8653ce5f2f9290f4ec7b42a8e" + "Hash": "8687398773806cfff9401a2feca96298" }, "xml2": { "Package": "xml2", @@ -1359,21 +1372,21 @@ }, "xopen": { "Package": "xopen", - "Version": "1.0.0", + "Version": "1.0.1", "Source": "Repository", "Repository": "CRAN", "Requirements": [ "R", "processx" ], - "Hash": "6c85f015dee9cc7710ddd20f86881f58" + "Hash": "423df1e86d5533fcb73c6b02b4923b49" }, "yaml": { "Package": "yaml", - "Version": "2.3.8", + "Version": "2.3.10", "Source": "Repository", "Repository": "CRAN", - "Hash": "29240487a071f535f5e5d5a323b7afbd" + "Hash": "51dab85c6c98e50a18d7551e9d49f76c" } } } diff --git a/renv/activate.R b/renv/activate.R index a0639113..9b2e7f18 100644 --- a/renv/activate.R +++ b/renv/activate.R @@ -3,10 +3,26 @@ local({ # the requested version of renv version <- "1.0.5" + attr(version, "sha") <- NULL # the project directory project <- getwd() + # use start-up diagnostics if enabled + diagnostics <- Sys.getenv("RENV_STARTUP_DIAGNOSTICS", unset = "FALSE") + if (diagnostics) { + start <- Sys.time() + profile <- tempfile("renv-startup-", fileext = ".Rprof") + utils::Rprof(profile) + on.exit({ + utils::Rprof(NULL) + elapsed <- signif(difftime(Sys.time(), start, units = "auto"), digits = 2L) + writeLines(sprintf("- renv took %s to run the autoloader.", format(elapsed))) + writeLines(sprintf("- Profile: %s", profile)) + print(utils::summaryRprof(profile)) + }, add = TRUE) + } + # figure out whether the autoloader is enabled enabled <- local({ @@ -15,6 +31,14 @@ local({ if (!is.null(override)) return(override) + # if we're being run in a context where R_LIBS is already set, + # don't load -- presumably we're being run as a sub-process and + # the parent process has already set up library paths for us + rcmd <- Sys.getenv("R_CMD", unset = NA) + rlibs <- Sys.getenv("R_LIBS", unset = NA) + if (!is.na(rlibs) && !is.na(rcmd)) + return(FALSE) + # next, check environment variables # TODO: prefer using the configuration one in the future envvars <- c( @@ -34,9 +58,22 @@ local({ }) - if (!enabled) + # bail if we're not enabled + if (!enabled) { + + # if we're not enabled, we might still need to manually load + # the user profile here + profile <- Sys.getenv("R_PROFILE_USER", unset = "~/.Rprofile") + if (file.exists(profile)) { + cfg <- Sys.getenv("RENV_CONFIG_USER_PROFILE", unset = "TRUE") + if (tolower(cfg) %in% c("true", "t", "1")) + sys.source(profile, envir = globalenv()) + } + return(FALSE) + } + # avoid recursion if (identical(getOption("renv.autoloader.running"), TRUE)) { warning("ignoring recursive attempt to run renv autoloader") @@ -60,25 +97,75 @@ local({ # load bootstrap tools `%||%` <- function(x, y) { - if (is.environment(x) || length(x)) x else y + if (is.null(x)) y else x } - `%??%` <- function(x, y) { - if (is.null(x)) y else x + catf <- function(fmt, ..., appendLF = TRUE) { + + quiet <- getOption("renv.bootstrap.quiet", default = FALSE) + if (quiet) + return(invisible()) + + msg <- sprintf(fmt, ...) + cat(msg, file = stdout(), sep = if (appendLF) "\n" else "") + + invisible(msg) + + } + + header <- function(label, + ..., + prefix = "#", + suffix = "-", + n = min(getOption("width"), 78)) + { + label <- sprintf(label, ...) + n <- max(n - nchar(label) - nchar(prefix) - 2L, 8L) + if (n <= 0) + return(paste(prefix, label)) + + tail <- paste(rep.int(suffix, n), collapse = "") + paste0(prefix, " ", label, " ", tail) + + } + + startswith <- function(string, prefix) { + substring(string, 1, nchar(prefix)) == prefix } bootstrap <- function(version, library) { + friendly <- renv_bootstrap_version_friendly(version) + section <- header(sprintf("Bootstrapping renv %s", friendly)) + catf(section) + # attempt to download renv - tarball <- tryCatch(renv_bootstrap_download(version), error = identity) - if (inherits(tarball, "error")) - stop("failed to download renv ", version) + catf("- Downloading renv ... ", appendLF = FALSE) + withCallingHandlers( + tarball <- renv_bootstrap_download(version), + error = function(err) { + catf("FAILED") + stop("failed to download:\n", conditionMessage(err)) + } + ) + catf("OK") + on.exit(unlink(tarball), add = TRUE) # now attempt to install - status <- tryCatch(renv_bootstrap_install(version, tarball, library), error = identity) - if (inherits(status, "error")) - stop("failed to install renv ", version) + catf("- Installing renv ... ", appendLF = FALSE) + withCallingHandlers( + status <- renv_bootstrap_install(version, tarball, library), + error = function(err) { + catf("FAILED") + stop("failed to install:\n", conditionMessage(err)) + } + ) + catf("OK") + + # add empty line to break up bootstrapping from normal output + catf("") + return(invisible()) } renv_bootstrap_tests_running <- function() { @@ -108,13 +195,6 @@ local({ if (!inherits(repos, "error") && length(repos)) return(repos) - # if we're testing, re-use the test repositories - if (renv_bootstrap_tests_running()) { - repos <- getOption("renv.tests.repos") - if (!is.null(repos)) - return(repos) - } - # retrieve current repos repos <- getOption("repos") @@ -158,33 +238,34 @@ local({ renv_bootstrap_download <- function(version) { - # if the renv version number has 4 components, assume it must - # be retrieved via github - nv <- numeric_version(version) - components <- unclass(nv)[[1]] - - # if this appears to be a development version of 'renv', we'll - # try to restore from github - dev <- length(components) == 4L - - # begin collecting different methods for finding renv - methods <- c( - renv_bootstrap_download_tarball, - if (dev) - renv_bootstrap_download_github - else c( - renv_bootstrap_download_cran_latest, - renv_bootstrap_download_cran_archive + sha <- attr(version, "sha", exact = TRUE) + + methods <- if (!is.null(sha)) { + + # attempting to bootstrap a development version of renv + c( + function() renv_bootstrap_download_tarball(sha), + function() renv_bootstrap_download_github(sha) ) - ) + + } else { + + # attempting to bootstrap a release version of renv + c( + function() renv_bootstrap_download_tarball(version), + function() renv_bootstrap_download_cran_latest(version), + function() renv_bootstrap_download_cran_archive(version) + ) + + } for (method in methods) { - path <- tryCatch(method(version), error = identity) + path <- tryCatch(method(), error = identity) if (is.character(path) && file.exists(path)) return(path) } - stop("failed to download renv ", version) + stop("All download methods failed") } @@ -248,8 +329,6 @@ local({ type <- spec$type repos <- spec$repos - message("* Downloading renv ", version, " ... ", appendLF = FALSE) - baseurl <- utils::contrib.url(repos = repos, type = type) ext <- if (identical(type, "source")) ".tar.gz" @@ -266,13 +345,10 @@ local({ condition = identity ) - if (inherits(status, "condition")) { - message("FAILED") + if (inherits(status, "condition")) return(FALSE) - } # report success and return - message("OK (downloaded ", type, ")") destfile } @@ -329,8 +405,6 @@ local({ urls <- file.path(repos, "src/contrib/Archive/renv", name) destfile <- file.path(tempdir(), name) - message("* Downloading renv ", version, " ... ", appendLF = FALSE) - for (url in urls) { status <- tryCatch( @@ -338,14 +412,11 @@ local({ condition = identity ) - if (identical(status, 0L)) { - message("OK") + if (identical(status, 0L)) return(destfile) - } } - message("FAILED") return(FALSE) } @@ -368,7 +439,7 @@ local({ if (!file.exists(tarball)) { # let the user know we weren't able to honour their request - fmt <- "* RENV_BOOTSTRAP_TARBALL is set (%s) but does not exist." + fmt <- "- RENV_BOOTSTRAP_TARBALL is set (%s) but does not exist." msg <- sprintf(fmt, tarball) warning(msg) @@ -377,10 +448,7 @@ local({ } - fmt <- "* Bootstrapping with tarball at path '%s'." - msg <- sprintf(fmt, tarball) - message(msg) - + catf("- Using local tarball '%s'.", tarball) tarball } @@ -407,8 +475,6 @@ local({ on.exit(do.call(base::options, saved), add = TRUE) } - message("* Downloading renv ", version, " from GitHub ... ", appendLF = FALSE) - url <- file.path("https://api.github.com/repos/rstudio/renv/tarball", version) name <- sprintf("renv_%s.tar.gz", version) destfile <- file.path(tempdir(), name) @@ -418,26 +484,105 @@ local({ condition = identity ) - if (!identical(status, 0L)) { - message("FAILED") + if (!identical(status, 0L)) return(FALSE) - } - message("OK") + renv_bootstrap_download_augment(destfile) + return(destfile) } + # Add Sha to DESCRIPTION. This is stop gap until #890, after which we + # can use renv::install() to fully capture metadata. + renv_bootstrap_download_augment <- function(destfile) { + sha <- renv_bootstrap_git_extract_sha1_tar(destfile) + if (is.null(sha)) { + return() + } + + # Untar + tempdir <- tempfile("renv-github-") + on.exit(unlink(tempdir, recursive = TRUE), add = TRUE) + untar(destfile, exdir = tempdir) + pkgdir <- dir(tempdir, full.names = TRUE)[[1]] + + # Modify description + desc_path <- file.path(pkgdir, "DESCRIPTION") + desc_lines <- readLines(desc_path) + remotes_fields <- c( + "RemoteType: github", + "RemoteHost: api.github.com", + "RemoteRepo: renv", + "RemoteUsername: rstudio", + "RemotePkgRef: rstudio/renv", + paste("RemoteRef: ", sha), + paste("RemoteSha: ", sha) + ) + writeLines(c(desc_lines[desc_lines != ""], remotes_fields), con = desc_path) + + # Re-tar + local({ + old <- setwd(tempdir) + on.exit(setwd(old), add = TRUE) + + tar(destfile, compression = "gzip") + }) + invisible() + } + + # Extract the commit hash from a git archive. Git archives include the SHA1 + # hash as the comment field of the tarball pax extended header + # (see https://www.kernel.org/pub/software/scm/git/docs/git-archive.html) + # For GitHub archives this should be the first header after the default one + # (512 byte) header. + renv_bootstrap_git_extract_sha1_tar <- function(bundle) { + + # open the bundle for reading + # We use gzcon for everything because (from ?gzcon) + # > Reading from a connection which does not supply a 'gzip' magic + # > header is equivalent to reading from the original connection + conn <- gzcon(file(bundle, open = "rb", raw = TRUE)) + on.exit(close(conn)) + + # The default pax header is 512 bytes long and the first pax extended header + # with the comment should be 51 bytes long + # `52 comment=` (11 chars) + 40 byte SHA1 hash + len <- 0x200 + 0x33 + res <- rawToChar(readBin(conn, "raw", n = len)[0x201:len]) + + if (grepl("^52 comment=", res)) { + sub("52 comment=", "", res) + } else { + NULL + } + } + renv_bootstrap_install <- function(version, tarball, library) { # attempt to install it into project library - message("* Installing renv ", version, " ... ", appendLF = FALSE) dir.create(library, showWarnings = FALSE, recursive = TRUE) + output <- renv_bootstrap_install_impl(library, tarball) + + # check for successful install + status <- attr(output, "status") + if (is.null(status) || identical(status, 0L)) + return(status) + + # an error occurred; report it + header <- "installation of renv failed" + lines <- paste(rep.int("=", nchar(header)), collapse = "") + text <- paste(c(header, lines, output), collapse = "\n") + stop(text) + + } + + renv_bootstrap_install_impl <- function(library, tarball) { # invoke using system2 so we can capture and report output bin <- R.home("bin") exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R" - r <- file.path(bin, exe) + R <- file.path(bin, exe) args <- c( "--vanilla", "CMD", "INSTALL", "--no-multiarch", @@ -445,19 +590,7 @@ local({ shQuote(path.expand(tarball)) ) - output <- system2(r, args, stdout = TRUE, stderr = TRUE) - message("Done!") - - # check for successful install - status <- attr(output, "status") - if (is.numeric(status) && !identical(status, 0L)) { - header <- "Error installing renv:" - lines <- paste(rep.int("=", nchar(header)), collapse = "") - text <- c(header, lines, output) - writeLines(text, con = stderr()) - } - - status + system2(R, args, stdout = TRUE, stderr = TRUE) } @@ -667,34 +800,62 @@ local({ } - renv_bootstrap_validate_version <- function(version) { + renv_bootstrap_validate_version <- function(version, description = NULL) { - loadedversion <- utils::packageDescription("renv", fields = "Version") - if (version == loadedversion) - return(TRUE) + # resolve description file + # + # avoid passing lib.loc to `packageDescription()` below, since R will + # use the loaded version of the package by default anyhow. note that + # this function should only be called after 'renv' is loaded + # https://github.com/rstudio/renv/issues/1625 + description <- description %||% packageDescription("renv") - # assume four-component versions are from GitHub; - # three-component versions are from CRAN - components <- strsplit(loadedversion, "[.-]")[[1]] - remote <- if (length(components) == 4L) - paste("rstudio/renv", loadedversion, sep = "@") + # check whether requested version 'version' matches loaded version of renv + sha <- attr(version, "sha", exact = TRUE) + valid <- if (!is.null(sha)) + renv_bootstrap_validate_version_dev(sha, description) else - paste("renv", loadedversion, sep = "@") + renv_bootstrap_validate_version_release(version, description) + + if (valid) + return(TRUE) + + # the loaded version of renv doesn't match the requested version; + # give the user instructions on how to proceed + remote <- if (!is.null(description[["RemoteSha"]])) { + paste("rstudio/renv", description[["RemoteSha"]], sep = "@") + } else { + paste("renv", description[["Version"]], sep = "@") + } + + # display both loaded version + sha if available + friendly <- renv_bootstrap_version_friendly( + version = description[["Version"]], + sha = description[["RemoteSha"]] + ) fmt <- paste( "renv %1$s was loaded from project library, but this project is configured to use renv %2$s.", - "Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.", - "Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.", + "- Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.", + "- Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.", sep = "\n" ) - - msg <- sprintf(fmt, loadedversion, version, remote) - warning(msg, call. = FALSE) + catf(fmt, friendly, renv_bootstrap_version_friendly(version), remote) FALSE } + renv_bootstrap_validate_version_dev <- function(version, description) { + expected <- description[["RemoteSha"]] + is.character(expected) && startswith(expected, version) + } + + renv_bootstrap_validate_version_release <- function(version, description) { + expected <- description[["Version"]] + is.character(expected) && identical(expected, version) + } + renv_bootstrap_hash_text <- function(text) { hashfile <- tempfile("renv-hash-") @@ -718,7 +879,7 @@ local({ hooks <- getHook("renv::autoload") for (hook in hooks) if (is.function(hook)) - tryCatch(hook(), error = warning) + tryCatch(hook(), error = warnify) # load the project renv::load(project) @@ -859,6 +1020,40 @@ local({ } + renv_bootstrap_version_friendly <- function(version, shafmt = NULL, sha = NULL) { + sha <- sha %||% attr(version, "sha", exact = TRUE) + parts <- c(version, sprintf(shafmt %||% " [sha: %s]", substring(sha, 1L, 7L))) + paste(parts, collapse = "") + } + + renv_bootstrap_exec <- function(project, libpath, version) { + if (!renv_bootstrap_load(project, libpath, version)) + renv_bootstrap_run(version, libpath) + } + + renv_bootstrap_run <- function(version, libpath) { + + # perform bootstrap + bootstrap(version, libpath) + + # exit early if we're just testing bootstrap + if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA))) + return(TRUE) + + # try again to load + if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) { + return(renv::load(project = getwd())) + } + + # failed to download or load renv; warn the user + msg <- c( + "Failed to find an renv installation: the project will not be loaded.", + "Use `renv::activate()` to re-initialize the project." + ) + + warning(paste(msg, collapse = "\n"), call. = FALSE) + + } renv_json_read <- function(file = NULL, text = NULL) { @@ -867,7 +1062,7 @@ local({ # if jsonlite is loaded, use that instead if ("jsonlite" %in% loadedNamespaces()) { - json <- catch(renv_json_read_jsonlite(file, text)) + json <- tryCatch(renv_json_read_jsonlite(file, text), error = identity) if (!inherits(json, "error")) return(json) @@ -876,7 +1071,7 @@ local({ } # otherwise, fall back to the default JSON reader - json <- catch(renv_json_read_default(file, text)) + json <- tryCatch(renv_json_read_default(file, text), error = identity) if (!inherits(json, "error")) return(json) @@ -889,14 +1084,14 @@ local({ } renv_json_read_jsonlite <- function(file = NULL, text = NULL) { - text <- paste(text %||% read(file), collapse = "\n") + text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n") jsonlite::fromJSON(txt = text, simplifyVector = FALSE) } renv_json_read_default <- function(file = NULL, text = NULL) { # find strings in the JSON - text <- paste(text %||% read(file), collapse = "\n") + text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n") pattern <- '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' locs <- gregexpr(pattern, text, perl = TRUE)[[1]] @@ -944,14 +1139,14 @@ local({ map <- as.list(map) # remap strings in object - remapped <- renv_json_remap(json, map) + remapped <- renv_json_read_remap(json, map) # evaluate eval(remapped, envir = baseenv()) } - renv_json_remap <- function(json, map) { + renv_json_read_remap <- function(json, map) { # fix names if (!is.null(names(json))) { @@ -978,7 +1173,7 @@ local({ # recurse if (is.recursive(json)) { for (i in seq_along(json)) { - json[i] <- list(renv_json_remap(json[[i]], map)) + json[i] <- list(renv_json_read_remap(json[[i]], map)) } } @@ -998,35 +1193,9 @@ local({ # construct full libpath libpath <- file.path(root, prefix) - # attempt to load - if (renv_bootstrap_load(project, libpath, version)) - return(TRUE) - - # load failed; inform user we're about to bootstrap - prefix <- paste("# Bootstrapping renv", version) - postfix <- paste(rep.int("-", 77L - nchar(prefix)), collapse = "") - header <- paste(prefix, postfix) - message(header) - - # perform bootstrap - bootstrap(version, libpath) - - # exit early if we're just testing bootstrap - if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA))) - return(TRUE) - - # try again to load - if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) { - message("* Successfully installed and loaded renv ", version, ".") - return(renv::load()) - } - - # failed to download or load renv; warn the user - msg <- c( - "Failed to find an renv installation: the project will not be loaded.", - "Use `renv::activate()` to re-initialize the project." - ) + # run bootstrap code + renv_bootstrap_exec(project, libpath, version) - warning(paste(msg, collapse = "\n"), call. = FALSE) + invisible() })