r-lib / pingr

ICMP and TCP ping and related tools
http://r-lib.github.io/pingr/
Other
34 stars 8 forks source link

Error while utilizing `nsl` within a data.frame #26

Closed lvanden closed 1 year ago

lvanden commented 1 year ago

It would be nice to be able to utilize these functions within a data.frame but I think some of the checks are causing it to fail. Here is an example.

data <- data.frame(
  id = 1:3,
  dmarc_string = c("_dmarc.google.com", "_dmarc.shopify.com", "_dmarc.walmart.com")
)

update_df <- data |>
  dplyr::mutate(
    dmarc_record = pingr::nsl(domain=dmarc_string, type=16L)[["answer"]]["data"]
  )

Error: Problem with `mutate()` column `dmarc_record`.
i `dmarc_record = pingr::nsl(domain = dmarc_string, type = 16L)[["answer"]]["data"]`.
x is_string(domain) is not TRUE
gaborcsardi commented 1 year ago

I think this is because nsl() is not vectorized. You can write a helper function that makes it vectorized, or Vectorize() seems to work as well:

❯ Vectorize(pingr::nsl)(data$dmarc_string, type = 16L)
       _dmarc.google.com _dmarc.shopify.com _dmarc.walmart.com
answer data.frame,5      data.frame,5       data.frame,5
flags  logical,6         logical,6          logical,6

We could make nsl() vectorized, to return a data frame, but that's a breaking change, so having a new function with a different name instead is better.

lvanden commented 1 year ago

Ah, yes. This makes sense. I’ll vectorize it for now. Thank you.