# Troubleshooting TERR and Spotfire Packages

If you remove a package accidentally, cannot install a package, get different results than you expect in your analysis, or cannot distribute an SPK, try these techniques to solve the problem.

## I generated a new SPK using `writeLines` but it is not being distributed.

It is most likely that the SPK version number is not being properly revised. Review the rules for how the SPK version is determined in SpotfireSPK versioning for more information.

## I have copied a package from CRAN but it is not working with TERR

Always make sure to use the version of open-source R that was tested with the version of TERR you are using.

Due to changes in open-source R version 3.5 and resulting compatibility changes in TERR 5.0, packages that are built with a version of TERR prior to 5.0 must be rebuilt.

- To install a binary package from a repository, always call `install.packages(pkgname)` from TERR. The `install.packages` function finds the correct binary version in the repository for your version of TERR. Manually downloading the binary package from CRAN can result in errors when you use it with TERR.
- To install a package from source, try installing it first with TERR (with `install.packages` in TERR or with `TERR CMD INSTALL` from a command line).
- To install a package from source that you cannot build with TERR, install the package with the version of open-source R tested with TERR.

## I accidentally removed a package from my Spotfire installation that I need. How do I get it back?

We recommend never removing a package from Spotfire TERR Tools, unless you have downloaded and installed the package yourself from a repository. You must be very careful to not remove a package that is installed with TERR or distributed by the Spotfire Server using the SPK mechanism. However, should you accidentally remove a needed package, you can recover.

If you deleted a distributed package, follow these steps.

1. Open Windows Explorer and browse to %SPOTFIRE_HOME%\Modules.

   For example, C:\Program Files (x86)\Spotfire\<version#>\Modules.

2. Delete the folder Spotfire Enterprise Runtime for R Packages_<version#> .
3. Restart Spotfire and accept the prompt to update your installation.

All of the TERR packages contained in the SPK on the Spotfire Server are redistributed.

If you deleted a package required by TERR to operate, follow these steps.

1. Open Windows Explorer and browse to %SPOTFIRE_HOME%\Modules.

   For example, C:\Program Files (x86)\Spotfire\<version#>\Modules.

2. Delete the folder Spotfire Enterprise Runtime for R_<version#> .
3. Restart Spotfire and accept the prompt to update your installation.

The package containing TERR is redistributed.

## I am generating a different result from those seen by others. What could cause this?

Check your package version numbers to make sure everyone is using the same package version. See your package curator for more information.

## I am trying to use a package on Spotfire Statistics Services but it's not working.

Be aware that by default, certain functions are restricted by Spotfire Statistics Services. If you see the error `"Error: restricted call to Native[tempfile]"`, and execution of the expression is terminated, then the default restrictions have not been changed by your server administrator. Restricted behavior includes the following non-exhaustive list of operations.

- Performing any I/O to the file system or the internet.
- Loading new packages, except for the libraries included with TERR (for example, stats).
- Spawning new OS processes (calling `system`).
- Calling `.Call`, which is used to call Rapi code in CRAN packages.
- Calling `.C` or `.Fortran`.
- Calling into Java using the terrJava package (which allows executing arbitrary Java methods).
- Calling any functions in the parallel package (which uses terrJava).
- Accessing any function environments in the stack above the call to `evalREX` using `sys.frame` or `parent.frame`. (This prevents malicious code from installing functions or expressions that could be executed after leaving restricted execution mode.)
- Changing the variable lookup path by setting `parent.env` of an environment, or reading or setting the environment of a closure.
- Defining S4 classes and methods using `setClass` or `setMethod`.

Be sure you are loading the package in your TERR script. Although the package is on Spotfire Statistics Services, it is not loaded into the engine. With each call to Spotfire Statistics Services, the engine is started anew. To use the package, you can either load the library as part of your function scripts, or you can include it in the `Packages` field of your data function.

## I cannot install a package from TRAN on Linux because it requires another package that is not installing correctly.

Some packages customized and placed on TRAN require other packages not available on TRAN. Some of these packages cannot be installed using the TERR function `install.packages`, so the TRAN package cannot be successfully installed. If you encounter this situation, try building and installing the package using open-source R.

If you get a warning that the rinclude package is not installed, try installing that package from TRAN and trying again.

Note: When you install a package using TERR, by default, TERR first checks for the package on TRAN, and then checks on Posit, and then CRAN. TERR installs the first version it finds. This is different than open-source R, which installs packages according to the newest version number available on CRAN. This difference is by design, because occasionally a CRAN package update causes a break with TERR compatibility, so we make available a tested version of the package on TRAN. If you need to install one of these packages using open-source R, you can install the CRAN package from Posit or CRAN, and then set `options()$repos` to install from only TRAN before reinstalling the package. See Specifying an older package on TRAN for more information.