Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in read_xml.raw(res) : Start tag expected, '<' not found [4] #460

Closed
ramiromagno opened this issue Jan 23, 2024 · 4 comments
Closed

Comments

@ramiromagno
Copy link

While debugging tidysdm code from this vignette I stumbled on this error that only happens when wrapped in reprex::reprex({<code>}). If I run the code below not inside reprex() it runs fine. So that is why I am assuming this is a reprex bug.

This is an error I found following on EvolEcolGroup/tidysdm#29.

Code

library(tidysdm)
library(sf)
library(pastclim)
library(tidyterra)
library(ggplot2)

data(lacerta)
lacerta

lacerta <- st_as_sf(lacerta, coords = c("longitude", "latitude"))
st_crs(lacerta) <- 4326

# Iberia peninsula extension
iberia_poly <-
  terra::vect(
    "POLYGON((-9.8 43.3,-7.8 44.1,-2.0 43.7,3.6 42.5,3.8 41.5,1.3 40.8,0.3 39.5,0.9 38.6,-0.4 37.5,-1.6 36.7,-2.3 36.3,-4.1 36.4,-4.5 36.4,-5.0 36.1,-5.6 36.0,-6.3 36.0,-7.1 36.9,-9.5 36.6,-9.4 38.0,-10.6 38.9,-9.5 40.8,-9.8 43.3))"
  )

crs(iberia_poly) <- "lonlat"
gdal(warn = 3)
land_mask <- rast(system.file("extdata/lacerta_land_mask.nc",
                              package = "tidysdm"))

ggplot() +
  geom_spatraster(data = land_mask, aes(fill = land_mask_1985)) +
  geom_sf(data = lacerta)


# Thinning step

set.seed(1234567)
lacerta <- thin_by_cell(lacerta, raster = land_mask)
nrow(lacerta)

ggplot() +
  geom_spatraster(data = land_mask, aes(fill = land_mask_1985)) +
  geom_sf(data = lacerta)

set.seed(1234567)
lacerta_thin <- thin_by_dist(lacerta, dist_min = km2m(20))
nrow(lacerta_thin)


ggplot() +
  geom_spatraster(data = land_mask, aes(fill = land_mask_1985)) +
  geom_sf(data = lacerta_thin)

set.seed(1234567)
lacerta_thin <- sample_pseudoabs(
  lacerta_thin,
  n = 3 * nrow(lacerta_thin),
  raster = land_mask,
  method = c("dist_min", km2m(50))
)

ggplot() +
  geom_spatraster(data = land_mask, aes(fill = land_mask_1985)) +
  geom_sf(data = lacerta_thin, aes(col = class))

gdal(warn = 3)
climate_present <-
  rast(system.file("extdata/lacerta_climate_present_10m.nc",
                   package = "tidysdm"))
climate_vars <- names(climate_present)


lacerta_thin <- lacerta_thin %>%
  bind_cols(terra::extract(climate_present, lacerta_thin, ID = FALSE))

lacerta_thin %>% plot_pres_vs_bg(class)
lacerta_thin %>% dist_pres_vs_bg(class)

vars_to_keep <- lacerta_thin %>% dist_pres_vs_bg(class)
vars_to_keep <- names(vars_to_keep[vars_to_keep > 0.30])
lacerta_thin <-
  lacerta_thin %>% select(all_of(c(vars_to_keep, "class")))
vars_to_keep

suggested_vars <- c("bio05", "bio06", "bio13", "bio14", "bio15")
pairs(climate_present[[suggested_vars]])



climate_present <- climate_present[[suggested_vars]]
vars_uncor <- filter_high_cor(climate_present, cutoff = 0.7)
vars_uncor

lacerta_thin <-
  lacerta_thin %>% select(all_of(c(vars_uncor, "class")))
climate_present <- climate_present[[vars_uncor]]

lacerta_rec <- recipe(lacerta_thin, formula = class ~ .)
lacerta_rec

lacerta_thin %>% check_sdm_presence(class)

lacerta_models <-
  # create the workflow_set
  workflow_set(
    preproc = list(default = lacerta_rec),
    models = list(
      # the standard glm specs
      glm = sdm_spec_glm(),
      # rf specs with tuning
      rf = sdm_spec_rf(),
      # boosted tree model (gbm) specs with tuning
      gbm = sdm_spec_boost_tree(),
      # maxent specs with tuning
      maxent = sdm_spec_maxent()
    ),
    # make all combinations of preproc and models,
    cross = TRUE
  ) %>%
  # tweak controls to store information needed later to create the ensemble
  option_add(control = control_ensemble_grid())

set.seed(100)
lacerta_cv <- spatial_block_cv(lacerta_thin, v = 5)
autoplot(lacerta_cv)

set.seed(1234567)
lacerta_models <-
  lacerta_models %>%
  workflow_map(
    "tune_grid",
    resamples = lacerta_cv,
    grid = 3,
    metrics = sdm_metric_set(),
    verbose = TRUE
  )

autoplot(lacerta_models)



lacerta_ensemble <- simple_ensemble() %>%
  add_member(lacerta_models, metric = "boyce_cont")
lacerta_ensemble

autoplot(lacerta_ensemble)

lacerta_ensemble %>% collect_metrics()

Reprex error

This reprex appears to crash R.
See standard output and standard error for more details.

Standard output and error

Quitting from lines 21-163 [unnamed-chunk-2] (hurt-puma_reprex.spin.Rmd)
Error in read_xml.raw(res) : Start tag expected, '<' not found [4]
@jennybc
Copy link
Member

jennybc commented Jan 23, 2024

What happens when you put this code in an .Rmd file and render it? What I'm trying to get at is whether it's how this code interacts with knitr/rmarkdown or with the reprex package specifically.

@ramiromagno
Copy link
Author

I just did. It ran fine, i.e. generated the html output without any errors.

@jennybc
Copy link
Member

jennybc commented Jan 24, 2024

Can you whittle this example down to anything smaller?

@ramiromagno
Copy link
Author

I am sorry Jenny, I can't reproduce the bug anymore... I know I had it with tidysdm version 0.9.1. Now, with versions 0.9.3 (most recent) and 0.9.2 (oldest on CRAN) I can't reproduce it. So I guess we call it solved. :)

@jennybc jennybc closed this as completed Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants