geocompx / geocompr

Geocomputation with R: an open source book
https://r.geocompx.org/
Other
1.59k stars 585 forks source link

Code fails in section 12.5.1 #1086

Closed biscotty666 closed 7 months ago

biscotty666 commented 7 months ago

In 12.5.1, the following code:

# 1. create task
task = mlr3spatiotempcv::as_task_classif_st(
  id = "ecuador_lsl",
  backend = mlr3::as_data_backend(lsl), 
  target = "lslpts", 
  positive = "TRUE",
  coordinate_names = c("x", "y"),
  coords_as_features = FALSE,
  crs = "EPSG:32717"
  )

produces

Error in UseMethod("as_task_classif_st") : 
  no applicable method for 'as_task_classif_st' applied to an object of class "character"

which seems to be due to the ordering of the arguments (and backend/target are positional according to the docs), so this works:

task = mlr3spatiotempcv::as_task_classif_st(
  mlr3::as_data_backend(lsl), 
  "lslpts", 
  id = "ecuador_lsl",
  positive = "TRUE",
  coordinate_names = c("x", "y"),
  coords_as_features = FALSE,
  crs = "EPSG:32717"
  )

Thank you for your work.

Nowosad commented 7 months ago

Thanks @biscotty666 -- could you test if the following works:

task = mlr3spatiotempcv::as_task_classif_st(
  id = "ecuador_lsl",
  x = mlr3::as_data_backend(lsl), 
  target = "lslpts", 
  positive = "TRUE",
  coordinate_names = c("x", "y"),
  coords_as_features = FALSE,
  crs = "EPSG:32717"
  )

?

biscotty666 commented 7 months ago

It seemed happy with that! TY.

I prefer using keywords, but the way the documentation is written suggested that the first two (x and target) in the documentation were different (positional) than the others with the = syntax (thinking args/kwargs from Python).

Nowosad commented 7 months ago

Thanks @biscotty666.

(In general, R is very flexible when it comes to the way users provide arguments: you can easily mix giving them as named arguments with giving them in some positions)

Nowosad commented 7 months ago

I just updated the code in the book.

jannes-m commented 7 months ago

Excellent, thanks a lot!!