"markovchain" not a defined class

Hi,
I have installed the latest markovchain package but I cannot get the following code to run:

weatherStates <- c("sunny", "cloudy", "rain")
byRow <- TRUE
weatherMatrix <- matrix(data = c(0.70, 0.2, 0.1,
0.3, 0.4, 0.3,
0.2, 0.45, 0.35), byrow = byRow, nrow = 3,
dimnames = list(weatherStates, weatherStates))

mcWeather <- new("markovchain", states = weatherStates, byrow = byRow,
transitionMatrix = weatherMatrix, name = "Weather")

The line line gives the following error:
"Error in getClass(Class, where = topenv(parent.frame())) :
“markovchain” is not a defined class"

How do i resolve this? Thanks.

Hi @ej257,

Can I ask that you post a reprex? (FAQ: How to do a minimal reproducible example ( reprex ) for beginners) . That will just make it easier to help you!

If your code needs a specific R package, it is not sufficient to just install that. You will also have to load that package before using it.

Try to load using library(markovchain) and then run your codes.

If you still come across same problem, please share a reproducible example as GreyMerchant suggested.


Edit (reply to post 5)

First of all, use triple backticks (```) instead of quotes in future to format code blocks, and single backtick for small one line code snippets. And please use > to block quote a paragraph, like an error message.

Regarding the error you posted, it is highly likely to that package was installed successfully. What I mean by that it may be the case that it has some other packages as dependencies, which were not installed.

Can you try to install it again? If you install by clicking RStudio buttons, there should be one where you can tick whether to install dependencies or not. If you use install.packages, specify dependencies as TRUE.

I hope that will solve the problems with library command. If not, you can post back again and someone from our community will try to help you solve this. Good luck!


Edit (reply to post 6)

You got the formatting almost correct. For backticks you have to do this:

```
First line of code
Second line of code
...
Last line of code
```

No need to specify 3 backticks in every line. The problem with that is it can no longer be copy pasted directly in console. There's a FAQ for code formatting for the community, you can check that out.

Regarding your post, you didn't include full error. If Rtools is not installed, your install command should fail and return messages like non zero exit status.

Rtools is a different software, and not a R package. You can download relevant version from CRAN, depending on your R version, and then you have to add it to path. Afterwards, try to install again. Amd if you fail again, no worries and post back.

Hope this helps!

1 Like

Hi GreyMerchant,

If I have understood the link correctly, then you're asking for the code I'm running, which is everything i have provided above. The example is given the markovchain help pdf file. There is no data here.

Hi Yarnabrina,

So I typed in library(markovchain) and that failed to run; here is my output:

"> library(markovchain)
Error: package or namespace load failed for ‘markovchain’ in namespaceExport(ns, exports):
undefined exports: setThreadOptions, defaultNumThreads, RcppParallelLibs, CxxFlags, LdFlags, RcppParallel.package.skeleton"

Simply put, I cannot make markovchain objects despite having the package installed.

Thanks for the help, I tried your suggestion and it installed a bunch of dependencies but alas it still gives the same error. Hopefully I got the triple backtick thing right:

``` WARNING: Rtools is required to build R packages but is not currently installed. Please download 
``` and install the appropriate version of Rtools before proceeding:

``` https://cran.rstudio.com/bin/windows/Rtools/
``` Installing package into ‘C:/Documents/R/win-library/4.0’ (as ‘lib’ is unspecified)
``` also installing the dependencies ‘sys’, ‘askpass’, ‘stringi’, ‘generics’, ‘tidyselect’, ‘hms’, ‘clipr’, ‘BH’, ‘cpp11’, ‘gridExtra’, ‘xml2’, ‘plyr’, ‘bibtex’, ‘lubridate’, ‘openssl’, ‘highr’, ‘markdown’, ‘stringr’, ‘yaml’, ‘xfun’, ‘shape’, ‘dplyr’, ‘downloader’, ‘htmltools’, ‘htmlwidgets’, ‘influenceR’, ‘purrr’, ‘readr’, ‘tidyr’, ‘viridis’, ‘visNetwork’, ‘mvtnorm’, ‘truncnorm’, ‘RefManageR’, ‘httr’, ‘base64enc’, ‘jsonlite’, ‘mime’, ‘tinytex’, ‘coda’, ‘numDeriv’, ‘knitr’, ‘diagram’, ‘DiagrammeR’, ‘msm’, ‘Rsolnp’, ‘knitcitations’, ‘rmarkdown’, ‘ctmcd’, ‘bookdown’, ‘rticles’



``` > z=new("markovchain",transitionMatrix=y);z
``` Error in getClass(Class, where = topenv(parent.frame())) : 
``` “markovchain” is not a defined class

Apologies, here is the whole thing. The other packages installing seemed unecessary.
I cannot find Rtools, it seems to be linking meback to the R website where I downloaded the software originally.

> install.packages(c("backports", "callr", "glue", "lmtest", "processx", "Rcpp", "RcppArmadillo", "TTR", "vctrs", "withr", "xts"))
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing packages into ‘C:/Users/Documents/R/win-library/4.0’
(as ‘lib’ is unspecified)

  There is a binary version available but the source version is later:
      binary source needs_compilation
withr  2.2.0  2.3.0             FALSE

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/backports_1.1.10.zip'
Content type 'application/zip' length 90803 bytes (88 KB)
downloaded 88 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/callr_3.4.4.zip'
Content type 'application/zip' length 377366 bytes (368 KB)
downloaded 368 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/glue_1.4.2.zip'
Content type 'application/zip' length 155596 bytes (151 KB)
downloaded 151 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/lmtest_0.9-38.zip'
Content type 'application/zip' length 411909 bytes (402 KB)
downloaded 402 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/processx_3.4.4.zip'
Content type 'application/zip' length 1378560 bytes (1.3 MB)
downloaded 1.3 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/Rcpp_1.0.5.zip'
Content type 'application/zip' length 3265820 bytes (3.1 MB)
downloaded 3.1 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/RcppArmadillo_0.9.900.3.0.zip'
Content type 'application/zip' length 2658582 bytes (2.5 MB)
downloaded 2.5 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/TTR_0.24.2.zip'
Content type 'application/zip' length 529523 bytes (517 KB)
downloaded 517 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/vctrs_0.3.4.zip'
Content type 'application/zip' length 1194970 bytes (1.1 MB)
downloaded 1.1 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/xts_0.12.1.zip'
Content type 'application/zip' length 942953 bytes (920 KB)
downloaded 920 KB

package ‘backports’ successfully unpacked and MD5 sums checked
package ‘callr’ successfully unpacked and MD5 sums checked
package ‘glue’ successfully unpacked and MD5 sums checked
package ‘lmtest’ successfully unpacked and MD5 sums checked
package ‘processx’ successfully unpacked and MD5 sums checked
package ‘Rcpp’ successfully unpacked and MD5 sums checked
package ‘RcppArmadillo’ successfully unpacked and MD5 sums checked
package ‘TTR’ successfully unpacked and MD5 sums checked
package ‘vctrs’ successfully unpacked and MD5 sums checked
package ‘xts’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\AppData\Local\Temp\RtmpUBnMU8\downloaded_packages
installing the source package ‘withr’

trying URL 'https://cran.rstudio.com/src/contrib/withr_2.3.0.tar.gz'
Content type 'application/x-gzip' length 91443 bytes (89 KB)
downloaded 89 KB

* installing *source* package 'withr' ...
** package 'withr' successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
  converting help for package 'withr'
    finding HTML links ... done
    defer                                   html  
    devices                                 html  
    makevars_user                           html  
    set_makevars                            html  
    with_                                   html  
    with_collate                            html  
    with_connection                         html  
    with_db_connection                      html  
    with_dir                                html  
    with_envvar                             html  
    with_file                               html  
    with_gctorture2                         html  
    with_libpaths                           html  
    with_locale                             html  
    with_makevars                           html  
    with_options                            html  
    with_package                            html  
Rd warning: C:/Users/AppData/Local/Temp/RtmpGygcJF/R.INSTALLa0877af5461/withr/man/with_package.Rd:68: file link '.libPaths' in package 'base' does not exist and so has been treated as a topic
    with_par                                html  
    with_path                               html  
    with_rng_version                        html  
    with_seed                               html  
    with_sink                               html  
    with_temp_libpaths                      html  
    with_tempfile                           html  
    with_timezone                           html  
    withr                                   html  
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
*** arch - i386
*** arch - x64
** testing if installed package can be loaded from final location
*** arch - i386
*** arch - x64
** testing if installed package keeps a record of temporary installation path
* DONE (withr)

The downloaded source packages are in
	‘C:\Users\AppData\Local\Temp\RtmpUBnMU8\downloaded_packages’

> z=new("markovchain",transitionMatrix=y);z
Error in getClass(Class, where = topenv(parent.frame())) : 
  “markovchain” is not a defined class

Have solved this now by reverting to an earlier version of R. Not sure why it works but it does.

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.