I've been working on this school project...
We have a very specific task. If the conditions arent true, I need to stop the code and print a custom message.
First I did this:
This one works, but I can't add a custom warning message I have to include.
alpha <- function(pstar, p){
stopifnot(is.numeric(pstar), length(pstar) == 1, is.numeric(p), is.vector(p), p > 0)
n <- length(p)
((sqrt((sum((pstar - p[1:n])^2)) / n)) / pstar) * 100
}
I thought I´d use if and stop instead.
alpha <- function(pstar, p){
if (!(is.numeric(pstar) && length(pstar) == 1 && is.numeric(p) && is.vector(p) && p > 0))
return(stop("Bad input"))
n <- length(p)
((sqrt((sum((pstar - p[1:n])^2)) / n)) / pstar) * 100
}
But I always get this warning message back:
Warning message:
In is.numeric(pstar) && length(pstar) == 1 && is.numeric(p) && is.vector(p) && :
'length(x) = 5 > 1' in coercion to 'logical(1)'
I can't find where the problem is.
Thank you for helping!
stopifnot() allows a message to be attached to each of the tests, for example the first two tests could be messaged like:
stopifnot("Pstar must be numerical" = is.numeric(pstar),
"pstar must be of length 1" = length(pstar) == 1,
is.numeric(p), is.vector(p), p > 0)
> alpha("a", 20)
Error in alpha("a", 20) : Pstar must be numerical
> alpha(c(2,2), 20)
Error in alpha(c(2, 2), 20) : pstar must be of length 1 > alpha("a", 20)
Does this solve your problem?
Incidentally, your second function appears to work:
alpha("a", 20)
Error in alpha("a", 20) : Bad input
alpha(c(2,2), 20)
Error in alpha(c(2, 2), 20) : Bad input
alpha(2, 1)
[1] 50
Using R 4.1.3 I observe:
var <- 0
> if(is.data.frame(var) || is.vector(var)) var <- as.matrix(var)
> is.null(var) || (!is.matrix(var) && var == 0) || (dim(var)==c(1,1) && var[1,1]==0)
[1] TRUE
However, with R 4.2.1 I observe the warning on this same code
> var <- 0
> if(is.data.frame(var) || is.vector(var)) var <- as.matrix(var)
> is.null(var) || (!is.matrix(var) && var == 0) || (dim(var)==c(1,1) && var[1,1]==0)
[1] TRUE
Warning message:
In dim(var) == c(1, 1) && var[1, 1] == 0 :
'length(x) = 2 > 1' in coercion to 'logical(1)'
Having a hard time finding the root cause here. Any thoughts on a reason this occurs now and a good fix?
dim(var) == c(1,1) gives two TRUE. See R News below. Possible replacements are
identical(dim(var), c(1L, 1L))
all(dim(var) == c(1,1))
Previously we were told that && and || are safe to use in if () as they silently return a single TRUE or FALSE. But now it will warn you. This actually makes it easier to detect bugs, so I am happy with this change.
Unfortunately, this change has affected a few R packages (see for example: Package ceases to work due to "if" error: condition has length > 1). As a result, the code that used to work smoothly suddenly throws warnings or even errors.
I am using my R studio via an EC2 instance in AWS.
I tried to install package =tidyverse in R stduio. but while installation i am getting an error
Warning in install.packages : installation of package ‘tidyverse’
had non-zero exit status
The downloaded source packages are in
‘/tmp/RtmpcH9Dy9/downloaded_packages’
Could anyone tell me how I could figure this out? Any lead would be helpful
EDIT: Console output
> install.packages("tidyverse")
> Installing package into ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4’
> (as ‘lib’ is unspecified)
> also installing the dependencies ‘mnormt’, ‘psych’, ‘broom’, ‘haven’, ‘modelr’, ‘readr’
>
> trying URL 'https://cran.rstudio.com/src/contrib/mnormt_1.5-5.tar.gz'
> Content type 'application/x-gzip' length 37169 bytes (36 KB)
> ==================================================
> downloaded 36 KB
>
> trying URL 'https://cran.rstudio.com/src/contrib/psych_1.7.8.tar.gz'
> Content type 'application/x-gzip' length 3311758 bytes (3.2 MB)
> ==================================================
> downloaded 3.2 MB
>
> trying URL 'https://cran.rstudio.com/src/contrib/broom_0.4.3.tar.gz'
> Content type 'application/x-gzip' length 1397648 bytes (1.3 MB)
> ==================================================
> downloaded 1.3 MB
>
> trying URL 'https://cran.rstudio.com/src/contrib/haven_1.1.1.tar.gz'
> Content type 'application/x-gzip' length 189925 bytes (185 KB)
> ==================================================
> downloaded 185 KB
>
> trying URL 'https://cran.rstudio.com/src/contrib/modelr_0.1.1.tar.gz'
> Content type 'application/x-gzip' length 83629 bytes (81 KB)
> ==================================================
> downloaded 81 KB
>
> trying URL 'https://cran.rstudio.com/src/contrib/readr_1.1.1.tar.gz'
> Content type 'application/x-gzip' length 233793 bytes (228 KB)
> ==================================================
> downloaded 228 KB
>
> trying URL 'https://cran.rstudio.com/src/contrib/tidyverse_1.2.1.tar.gz'
> Content type 'application/x-gzip' length 61647 bytes (60 KB)
> ==================================================
> downloaded 60 KB
>
> * installing *source* package ‘mnormt’ ...
> ** package ‘mnormt’ successfully unpacked and MD5 sums checked
> ** libs
> gfortran -m64 -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/lib64/gfortran/modules -c biv-nt.f -o biv-nt.o
> f951: Warning: Nonexistent include directory ‘/usr/lib64/gfortran/modules’ [-Wmissing-include-dirs]
> biv-nt.f:371:11:
>
> hs = sign( one, dh - r*dk )
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> biv-nt.f:372:11:
>
> ks = sign( one, dk - r*dh )
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> gfortran -m64 -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/lib64/gfortran/modules -c sadmvnt.f -o sadmvnt.o
> f951: Warning: Nonexistent include directory ‘/usr/lib64/gfortran/modules’ [-Wmissing-include-dirs]
> sadmvnt.f:976:3:
>
> 10 END DO
> 1
> Warning: Label 10 at (1) defined but not used
> sadmvnt.f:80:15:
>
> INFORM = MVNNIT( N, CORREL, LOWER, UPPER, INFIN, INFIS, D, E )
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:217:38:
>
> IF ( MINCLS .LT. 0 ) SBRGNS = WORK(LENWRK)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:275:21:
>
> RGNCLS = RGNCLS*(MESH(DIVAXN)+1)/MESH(DIVAXN)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:276:21:
>
> NWRGNS = NWRGNS*(MESH(DIVAXN)+1)/MESH(DIVAXN)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1274:24:
>
> POINTS = PONTRS(SUBTMP)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1275:24:
>
> POINTP = PONTRS(SUBTMP+1)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1283:21:
>
> POINTS = PONTRS(SUBTMP)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1303:21:
>
> POINTS = PONTRS(SUBTMP)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:324:15:
>
> TOP = PONTRS(1)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:331:21:
>
> RGNCLS = RGNCLS*MESH(I)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:336:18:
>
> RGNCLS = RGNCLS*(MESH(DIVAXN)+1)/MESH(DIVAXN)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1585:3:
>
> 10 END DO
> 1
> Warning: Label 10 at (1) defined but not used
> sadmvnt.f:1432:15:
>
> INFORM = MVTNIT( N, NU, CORREL, LOWER, UPPER, INFIN, INFIS, D, E )
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1661:0:
>
> & /( 1 + BMIN**2/(NU+I-1) )**( (NU+I-2)/TWO )
>
> Warning: ‘bmin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:1658:0:
>
> & /( 1 + AMIN**2/(NU+I-1) )**( (NU+I-2)/TWO )
>
> Warning: ‘amin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:1127:0:
>
> INTEGER S, IXCHNG, LXCHNG, I, L
>
> Warning: ‘lxchng’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:804:0:
>
> END
>
> Warning: ‘__result_bvn’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:762:0:
>
> DOUBLE PRECISION FUNCTION BVN ( LOWER, UPPER, INFIN, CORREL )
>
> note: ‘__result_bvn’ was declared here
> sadmvnt.f:1046:0:
>
> IF ( INFI(I) .EQ. 2 ) Y(I) = ( AMIN + BMIN )/2
>
> Warning: ‘bmin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:1046:0: Warning: ‘amin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> gcc -m64 -std=gnu99 -shared -L/usr/lib64/R/lib -L/usr/local/lib64 -o mnormt.so biv-nt.o sadmvnt.o -lgfortran -lm -lquadmath -L/usr/lib64/R/lib -lR
> /usr/bin/ld: cannot find -lgfortran
> /usr/bin/ld: cannot find -lquadmath
> collect2: error: ld returned 1 exit status
> make: *** [mnormt.so] Error 1
> ERROR: compilation failed for package ‘mnormt’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/mnormt’
> Warning in install.packages :
> installation of package ‘mnormt’ had non-zero exit status
> * installing *source* package ‘readr’ ...
> ** package ‘readr’ successfully unpacked and MD5 sums checked
> ** libs
> g++ -m64 -I/usr/include/R -DNDEBUG -I"/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include" -I"/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include" -I/usr/local/include -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -c Collector.cpp -o Collector.o
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:28:0,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/detail/shared_count.hpp:355:33:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:256:65:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:471:31:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:484:22:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:567:34:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> shared_ptr & operator=( std::auto_ptr<Y> & r )
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:576:34:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> shared_ptr & operator=( std::auto_ptr<Y> && r )
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:
> In member function ‘boost::shared_ptr<T>&
> boost::shared_ptr<T>::operator=(std::auto_ptr<_Up>&&)’:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:578:38:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> virtual memory exhausted: Cannot allocate memory
> make: *** [Collector.o] Error 1
> ERROR: compilation failed for package ‘readr’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/readr’
> Warning in install.packages :
> installation of package ‘readr’ had non-zero exit status
> ERROR: dependency ‘mnormt’ is not available for package ‘psych’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/psych’
> Warning in install.packages :
> installation of package ‘psych’ had non-zero exit status
> ERROR: dependency ‘readr’ is not available for package ‘haven’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/haven’
> Warning in install.packages :
> installation of package ‘haven’ had non-zero exit status
> ERROR: dependency ‘psych’ is not available for package ‘broom’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/broom’
> Warning in install.packages :
> installation of package ‘broom’ had non-zero exit status
> ERROR: dependency ‘broom’ is not available for package ‘modelr’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/modelr’
> Warning in install.packages :
> installation of package ‘modelr’ had non-zero exit status
> ERROR: dependencies ‘broom’, ‘haven’, ‘modelr’, ‘readr’ are not available for package ‘tidyverse’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/tidyverse’
> Warning in install.packages :
> installation of package ‘tidyverse’ had non-zero exit status
>
> The downloaded source packages are in
> ‘/tmp/RtmpcH9Dy9/downloaded_packages’
i used following tutorial for learning R programming language
R programming language
v <- c("Hello","loop")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5) {
break
}
}
but when i run this code, it gave me the following error
> if(cnt > 5) {
+ break
+ }
Error: object 'cnt' not found
>
this example was taken from tutorial itself, what is wrong in given code?
everybody, this is probably has a simple answer which I am overlooking (I am still learning).
I am trying to scrape data from a website, I am specific after particular p elements, which are nested inside different elements, this is what the nested elements look like.
#ctl00_body_divSearchResult > div:nth-child(5) > div.expandable-box-content.expanded > p:nth-child(2)
#ctl00_body_divSearchResult > div:nth-child(16) > div.expandable-box-content.expanded > p:nth-child(2)
#ctl00_body_divSearchResult > div:nth-child(27) > div.expandable-box-content.expanded > p:nth-child(2)
#ctl00_body_divSearchResult > div:nth-child(38) > div.expandable-box-content.expanded > p:nth-child(2)
#ctl00_body_divSearchResult > div:nth-child(49) > div.expandable-box-content.expanded > p:nth-child(2)
Here are five examples from the same page, the first div:nth-child has different numbers, but the rest is consistent. I am after the individual p:nth-child(2) elements.
Using this code I can get the individual p elements,
numbers= agent.get(urlanzctr).css('#ctl00_body_divSearchResult > div:nth-child(5) > div.expandable-box-content > p:nth-child(2)').text
But I think it would be sloppy coding to go through and repeat this for each individual instances.
Your agent.get(url).css(selector) approach is right to return an array.
Looking at your selectors, they're all of this structure:
#ctl00_body_divSearchResult >
div:nth-child(N) >
div.expandable-box-content.expanded >
p:nth-child(2)
The only variable being the N in div:nth-child.
You have the values 5, 16, 27, 38, 49
which is 11x + 5
So you could do something like this
def make_selector(n)
"#ctl00_body_divSearchResult > " +
"div:nth-child(#{n}) > " +
"div.expandable-box-content.expanded > " +
"p:nth-child(2)"
end
def get_matches(n)
agent.get(url).css(make_selector(n))
end
starting_idx = 5
current_matches = get_matches(starting_idx)
all_matches = []
until current_matches.empty?
all_matches.concat(current_matches)
current_matches = get_matches(starting_idx + 11)
end
puts all_matches.length
You might also be able to skip out the intermediary selectors
i.e. maybe just .expandable-box-content.exampled > p would work; i have no idea what the page structure looks like.