purrr::map behavies different in different environment

Hi,

My issue is that purrr::map behavies differently in different environments. I enclose two examples for this to present the issue.
(1) using read_html function inside a map {purrr}, and nest the XML into the tibble, it works if you run the script on your console, but in other environments it fails, just like in case of the closed reprex.
(2) using LogLik function to an LDA model works on console or as a script. but fails if I use this code in a Rmarkdown file.

I wonder why map behavies differently in different environments.
Thank you for your answer in advance,
M

library(tidyverse)

# Web scraping --------------------------------------------------------------------------

tibble(url = c("https://ingatlan.com/lista/elado+lakas+budapest")) %>% 
  mutate(page = map(url, xml2::read_html)) # works on console, but fails as run in local job/from cmd/as a reprex
#> Error: Problem with `mutate()` input `page`.
#> x HTTP error 403.
#> i Input `page` is `map(url, xml2::read_html)`.

# Topic model ---------------------------------------------------------------------------
library(topicmodels)
#> Warning: package 'topicmodels' was built under R version 4.0.4

data("AssociatedPress")


tibble(k = c(2)) %>% 
  mutate(
    model = map(k, ~ LDA(AssociatedPress, k = .)),
    loglike = map_dbl(model, logLik) # << works on console or as a script, but fails while knitting .Rmd
  )
#> # A tibble: 1 x 3
#>       k model       loglike
#>   <dbl> <list>        <dbl>
#> 1     2 <LDA_VEM> -3638550.

Created on 2021-04-24 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  Hungarian_Hungary.1250      
#>  ctype    Hungarian_Hungary.1250      
#>  tz       Europe/Prague               
#>  date     2021-04-24                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.3)
#>  backports     1.2.0   2020-11-02 [1] CRAN (R 4.0.3)
#>  broom         0.7.3   2020-12-16 [1] CRAN (R 4.0.3)
#>  cachem        1.0.1   2021-01-21 [1] CRAN (R 4.0.3)
#>  callr         3.5.1   2020-10-13 [1] CRAN (R 4.0.3)
#>  cellranger    1.1.0   2016-07-27 [1] CRAN (R 4.0.3)
#>  cli           2.3.0   2021-01-31 [1] CRAN (R 4.0.3)
#>  colorspace    2.0-0   2020-11-11 [1] CRAN (R 4.0.3)
#>  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.3)
#>  curl          4.3     2019-12-02 [1] CRAN (R 4.0.3)
#>  DBI           1.1.1   2021-01-15 [1] CRAN (R 4.0.3)
#>  dbplyr        2.0.0   2020-11-03 [1] CRAN (R 4.0.3)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.3)
#>  devtools      2.3.2   2020-09-18 [1] CRAN (R 4.0.3)
#>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.3)
#>  dplyr       * 1.0.4   2021-02-02 [1] CRAN (R 4.0.3)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.3)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.3)
#>  fansi         0.4.2   2021-01-15 [1] CRAN (R 4.0.3)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.0.3)
#>  forcats     * 0.5.0   2020-03-01 [1] CRAN (R 4.0.3)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.3)
#>  generics      0.1.0   2020-10-31 [1] CRAN (R 4.0.3)
#>  ggplot2     * 3.3.3   2020-12-30 [1] CRAN (R 4.0.3)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.3)
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.3)
#>  haven         2.3.1   2020-06-01 [1] CRAN (R 4.0.3)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.3)
#>  hms           1.0.0   2021-01-13 [1] CRAN (R 4.0.3)
#>  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3)
#>  httr          1.4.2   2020-07-20 [1] CRAN (R 4.0.3)
#>  jsonlite      1.7.2   2020-12-09 [1] CRAN (R 4.0.3)
#>  knitr         1.30    2020-09-22 [1] CRAN (R 4.0.3)
#>  lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.3)
#>  lubridate     1.7.9.2 2020-11-13 [1] CRAN (R 4.0.3)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
#>  memoise       2.0.0   2021-01-26 [1] CRAN (R 4.0.3)
#>  modelr        0.1.8   2020-05-19 [1] CRAN (R 4.0.3)
#>  modeltools    0.2-23  2020-03-05 [1] CRAN (R 4.0.3)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.3)
#>  NLP           0.2-1   2020-10-14 [1] CRAN (R 4.0.3)
#>  pillar        1.4.7   2020-11-20 [1] CRAN (R 4.0.3)
#>  pkgbuild      1.2.0   2020-12-15 [1] CRAN (R 4.0.3)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.3)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.3)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.3)
#>  processx      3.4.5   2020-11-30 [1] CRAN (R 4.0.3)
#>  ps            1.5.0   2020-12-05 [1] CRAN (R 4.0.3)
#>  purrr       * 0.3.4   2020-04-17 [1] CRAN (R 4.0.3)
#>  R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.3)
#>  Rcpp          1.0.6   2021-01-15 [1] CRAN (R 4.0.3)
#>  readr       * 1.4.0   2020-10-05 [1] CRAN (R 4.0.3)
#>  readxl        1.3.1   2019-03-13 [1] CRAN (R 4.0.3)
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.3)
#>  reprex        0.3.0   2019-05-16 [1] CRAN (R 4.0.3)
#>  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.3)
#>  rmarkdown     2.7     2021-02-19 [1] CRAN (R 4.0.4)
#>  rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.3)
#>  rvest         0.3.6   2020-07-25 [1] CRAN (R 4.0.3)
#>  scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.3)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.3)
#>  slam          0.1-48  2020-12-03 [1] CRAN (R 4.0.3)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.3)
#>  stringr     * 1.4.0   2019-02-10 [1] CRAN (R 4.0.3)
#>  testthat      3.0.1   2020-12-17 [1] CRAN (R 4.0.3)
#>  tibble      * 3.0.6   2021-01-29 [1] CRAN (R 4.0.3)
#>  tidyr       * 1.1.2   2020-08-27 [1] CRAN (R 4.0.3)
#>  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.3)
#>  tidyverse   * 1.3.0   2019-11-21 [1] CRAN (R 4.0.3)
#>  tm            0.7-8   2020-11-18 [1] CRAN (R 4.0.3)
#>  topicmodels * 0.2-12  2021-01-29 [1] CRAN (R 4.0.4)
#>  usethis       2.0.0   2020-12-10 [1] CRAN (R 4.0.3)
#>  utf8          1.1.4   2018-05-24 [1] CRAN (R 4.0.3)
#>  vctrs         0.3.6   2020-12-17 [1] CRAN (R 4.0.3)
#>  withr         2.4.0   2021-01-16 [1] CRAN (R 4.0.3)
#>  xfun          0.22    2021-03-11 [1] CRAN (R 4.0.4)
#>  xml2          1.3.2   2020-04-23 [1] CRAN (R 4.0.3)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.3)
#> 
#> [1] C:/Users/Marci/Documents/R/win-library/4.0
#> [2] C:/Program Files/R/R-4.0.3/library

It's difficult to say what is happening exactly, but my bet is that it has little to do with map itself.
At least I've tried running your first example in RStudio Cloud and it worked:

library(tidyverse)

# Web scraping --------------------------------------------------------------------------

tibble(url = c("https://ingatlan.com/lista/elado+lakas+budapest")) %>% 
  mutate(page = map(url, xml2::read_html)) # works on console, but fails as run in local job/from cmd/as a reprex
#> # A tibble: 1 x 2
#>   url                                             page      
#>   <chr>                                           <list>    
#> 1 https://ingatlan.com/lista/elado+lakas+budapest <xml_dcmn>

Created on 2021-04-24 by the reprex package (v2.0.0)

But I also tried running it on my local machine and it didn't work.

That said, 403 is "Forbidden", so I would investigate what would be the reason to get this response from a server.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.