pb with str_count

hello
the following instruction works very fine on my RStudio<->R 4.2.2 under MacOS Catalina :

install.packages("stringr")
library(stringr)
str_count("ABCD","A")

but on our students, on all the Ubuntu RStudio<->R 4.2.0
the same instruction returns a "input must be a vector, not en environment"

could it be that stringr be bad installed ? how could it be possible ?
a bug problem inside R 4.2.0 ?
a problem with ubuntu ?

any help welcome because we want to teach them how to sample some simulation of cards games and we need this instruction to count, say, the number of Kings or Hearts obtained

Vinz

Here's what I get on my install, a System76 version of Ubuntu 22.04 with version details as shown, using your example and the stringr help(str_count) examples. All appears as it should. What is very odd is that one or both of the two arguments, both shown as strings, which are vectors, are being parsed as environments. In a clean R session there should only be the .Global environment and, within the str_count() function a local environment. I tried giving an environment object as an argument, though, and got a different message.

e1 <- new.env(parent = baseenv()) 
assign("a", 3, envir = e1)
ls(e1)
#> [1] "a"
class(e1)
#> [1] "environment"
stringr::str_count(e1,"a")
#> Error in stri_count_regex(string, pattern, opts_regex = opts(pattern)): argument `str` should be a character vector (or an object coercible to)

library(stringr)
str_count("ABCD","A")
#> [1] 1

fruit <- c("apple", "banana", "pear", "pineapple")
str_count(fruit, "a")
#> [1] 1 3 1 1
str_count(fruit, "p")
#> [1] 2 0 1 3
str_count(fruit, "e")
#> [1] 1 0 1 2
str_count(fruit, c("a", "b", "p", "p"))
#> [1] 1 1 1 3

str_count(c("a.", "...", ".a.a"), ".")
#> [1] 2 3 4
str_count(c("a.", "...", ".a.a"), fixed("."))
#> [1] 1 3 2

sessionInfo()
#> R version 4.2.0 (2022-04-22)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Pop!_OS 22.04 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/local/lib/R/lib/libRblas.so
#> LAPACK: /usr/local/lib/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] stringr_1.4.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] rstudioapi_0.13   knitr_1.39        magrittr_2.0.3    R.cache_0.15.0   
#>  [5] rlang_1.0.4       fastmap_1.1.0     fansi_1.0.3       styler_1.7.0     
#>  [9] highr_0.9         tools_4.2.0       xfun_0.32         R.oo_1.24.0      
#> [13] utf8_1.2.2        cli_3.3.0         withr_2.5.0       htmltools_0.5.3  
#> [17] yaml_2.3.5        digest_0.6.29     tibble_3.1.8      lifecycle_1.0.1  
#> [21] purrr_0.3.4       R.utils_2.10.1    vctrs_0.4.1       fs_1.5.2         
#> [25] glue_1.6.2        evaluate_0.16     rmarkdown_2.15    reprex_2.0.1     
#> [29] stringi_1.7.8     compiler_4.2.0    pillar_1.8.0      R.methodsS3_1.8.1
#> [33] pkgconfig_2.0.3

Created on 2023-01-31 by the reprex package (v2.0.1)

When I use your example with constructed e1; I get the original posters error message.

Error in `stringr::str_count()`:
! `string` must be a vector, not an environment.

I'm using R4.22 on a windows machine; and the stringr version is 1.5

on second look; this is subtly different; as depending on whether e1 is in the 1st param or 2nd; corresponding to expected parameters string or pattern that shows up in the error; rather than input of the original poster. not sure if theres been a version of str_count where any param has had the input name

Hello the problem is solved in our classrooms even if i have not yet totally understood

the fact is that ìnstall.packages()doesn't install the same place where the native packages are

since each student has its own personal space on the cloud/on the computer, something was parameted so that ìnstall.packages(stringr) created a "stringr" a library in the top of Packages pane of RStudio, under the title "packages of user" or something like that

whereas…

stringr was still installated, visible on the bottom of this same pane under the title "native packages" or something like that

this coexistence of two same packages created the bug

so we have told the student to type remove.packages("stringr")and restart RStudio (and therefore R), and it is solved

what i still dont well undesrtand is :

  • is stringr natively installed on every R installation ?
  • the distinction between "personal packages" and "native packages" does not appear on my personal computer : is it because it is my personnal computer and i have access to something like the native libraries of R ?

Vinz

1 Like

Use libPaths() to see where R looks for libraries and in what order. Libraries come in two flavors: installed with R by a user with OS permission to install program and related resources such as .so files for any user who is using the system in contrast to those installed within the home directory of a specific user. In some situations, such as macOS, which are effectively single user systems, everything winds up in one place. In a true multi user system all users share the same set of libraries installed as part of R and each user has an individual folder where to add optional libraries.

ok very clear thansk very much

1 Like

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.