Using functions from a package in R - r

I am using a R package Cat
In the help file an example for function data uses dataset head
data(head)
I have my own dataset and I want to try this function data within the package CAT on my dataset A
when I try
library(cat)
A = read.table("C:/A.csv",header=TRUE,sep=",")
data(A)
I get a warning
"Warning message: In data(A) : data set ‘A’ not found*
How do I use specific functions that are part of R packages on my dataset and not the examples in those packages>

As #cenka pointed out, when you are loading your own data, you do not need to use the data() function. That is only for loading data from packages. If you want to use functions from a specific package after you install it, be sure to call library(cat) to actually load the library into your current R session.

Related

Conflicted package in R package building

I am creating my first package, here I am making some var estimations, the functions are running, however, I use packages that have the same function names.
Before writing the package I made an R script with the function and tests if it works, but at the top of my script I used the following code:
invisible(lapply(c("tibble","readxl","dplyr","stringr", "tidyr", "vars", "conflicted","forecast", "lubridate"), library, character.only = T))
conflict_prefer("select","dplyr")
conflict_prefer("lag", "dplyr")
conflict_prefer("filter", "dplyr")
The conflicted package chose the functions select, lag, and filter comes from the dplyr package rather from the stats package.
So I have not figured out how to use the conflict_prefer function inside the package.
Should they be the first lines of my function?
There is a roxygen way to prefer same-name functions?
I ask this because I get this warning:
> devtools::load_all()
i Loading FAVAR.MEF
Warning messages:
1: replacing previous import ‘dplyr::filter’ by ‘stats::filter’ when loading ‘FAVAR.MEF’
2: replacing previous import ‘dplyr::lag’ by ‘stats::lag’ when loading ‘FAVAR.MEF’
3: replacing previous import ‘stats::filter’ by ‘dplyr::filter’ when loading ‘FAVAR.MEF’
4: In setup_ns_exports(path, export_all, export_imports) :
Objects listed as exports, but not present in namespace: favar_est
Thanks in advance!!
If you are writing your own package and using external dependencies, you should not load them through repeated calls to library.
The proper way to do it is to state your dependencies in the DECRIPTION file of your package, which will mean that your dependencies are put on the search path in the correct order when your package is loaded. In your case, this removes the need for conflict_prefer, as dplyr will be higher up on the search path than stats. It also makes your package portable, because anyone who installs your package will have any missing dependencies installed automatically according to the packages listed in your DESCRIPTION file. Furthermore, doing it this way allows you to specify a minimum version of the dependency, so that anyone who already has an older version of the dependency installed will not come up against an obscure error when they try to use your package.
The DESCRIPTION file resides in the root directory of your package. It is a simple text file.
You need only add:
Depends:
tibble,
readxl,
dplyr,
stringr,
tidyr,
vars,
conflicted,
forecast,
lubridate
within this file, and your dependencies will be loaded with your package.

How to use an R package's own data when writing a vignette

I have written most of an R package and now wish to write a vignette that uses my own data, that is already in the package. The data is correctly stored as my_data.Rda in the Data folder, and when the package is loaded I can access it in the Console, for instance by using data(my_data).
My problem comes when, using usethis::use_vignette("my_vignette") , I want to include something like this (much more complex in practice, of course) in the vignette:
The mean of my_data is given by
```{r} data(my_data)
mean(my_data)
```
When I knit the vignette I get the message
"Error in assert_engine(is_numeric, x, .xname = get_name_in_parent(x),
: object 'my_data' not found"
I have looked at this post: How to add external data file into developing R package? but that addresses external data.
What am I doing wrong?
I have created a minimal R package with the relevant Rmd file in the vignettes folder. link to Github
I think you are supposed to use
data(my_dataset, package = "my_package")
to load your package's data into the session where your vignette is built.
Could you confirm that your datasets are stored inside the ./data directory of your package as *.rda files

Conflict of Packages in RStudio, detach() fails to work

I am currently doing Logistic Regression on the 'birthwt' data set found within R. This data is found within the package 'MASS'.
However, when I use library(MASS) to retrieve the data, it masks the function of select() from the dplyr package. I use this function almost immediately in my analysis.
After loading the data, I attempt
detach("package:MASS", unload = TRUE)
but I am met with
‘MASS’ namespace cannot be unloaded:
namespace ‘MASS’ is imported by ‘pbkrtest’, ‘car’, ‘lme4’ so cannot be unloaded
I wold really love to sort this out as I have completed all my necessary analysis on the data, but was met with this issue when attempting to knit.
Thank you in advance for any help!
You shouldn't choose unload = TRUE. The default is unload = FALSE, and that's what you need.
Here's the explanation:
In R, packages can be "loaded", which makes them available to other packages that import functions from them. They can also be "attached", which puts them on the search list, so that they are available to the user in the console. If a package is attached, it needs to be loaded, but the reverse is not true.
So if you run detach("package:MASS"), you will remove it from the search list, and in the console, running select() will no longer find the function in MASS. It will still be loaded, so will be available to the other packages that need it.
By the way, using the prefix form MASS::select() or dplyr::select() will work regardless of whether either or both packages are in your search list.

How to prevent R from trying to load mgcv

I need to run 2 different scripts in R without restarting the session. In the first i use the package mgcvand in the second i need gam. I wrote a script which detaches and removes the package mgcv and installs and loads gam.
But still after i want to run the gam() function I get an error telling me that mgcv was not found. Which I interpret as R looking for the mgcv package for installing it...
Here's a MWE:
install.packages(paste(path.pkgs,'mgcv_1.8-7.zip',sep=''),repos=NULL)
require(mgcv)
## FIRST SCRIPT ##
detach(package:mgcv)
remove.packages('mgcv')
unloadNamespace('mgcv')
require(gam)
## SECOND SCRIPT ##
gam(as.formula(t.thr.fm),data=data)
which returns the error:
Error in get(method, envir = home) :
cannot open file 'H:/data/Documents/R/R-3.1.3/library/mgcv/R/mgcv.rdb': No such file or directory
Any ideas instead of restarting the session?
EDIT:
The solution suggested by Floo0 using package::function is unfortunately not an option.
you can tell R to take a function from a specific package via this syntax:
package::function
So in your case (do not detach mgtv) and use
mgcv::gam(...)
gam::gam(...)
If the function is not exported in the namespace of the function you can also try package:::function with 3 :

Error in reading SAS dataset in R 3.0.1

I am trying to read SAS dataset in R 3.0.1.
I have downloaded Hmisc package required to use sas.get function. But I am getting note as below:
Hmisc library by Frank E Harrell Jr
Type library(help='Hmisc'), ?Overview, or ?Hmisc.Overview')
to see overall documentation.
NOTE:Hmisc no longer redefines [.factor to drop unused levels when
subsetting. To get the old behavior of Hmisc type dropUnusedLevels().
Attaching package: ‘Hmisc’
Then I am using the following command:
sas.get(library = "C:\\SAS_dataset", member = "test", formats = FALSE, sasprog = sasprog)
Then the R goes in infinite loop and does not give output. Finally when i press "Esc", it terminates by giving an warning message saying
Warning message:
running command '"C:/program files/SAS/SAS 9.1/sas.exe" "C:\Users\TEJASW~1.ABH\AppData\Local\Temp\RtmpML87zC\SaS13c41642d38.3.sas" -log "_temp_.log"' had status 10708
I tried to find the reason for the same, but all in vain.
I don't understand the reason for this. Is it due to some note given by Hmisc package or something else?
Also I noted that I am facing this problem for latest version i.e. 3.0.1 only. Whereas I was able to read the SAS dataset with the same commands in version 2.15.1.
Can any one help me to solve this problem.
Thanks in advance.
Regards,
Tejasweeni
If you have SAS, you can always export your data to a CSV file and read in R using read.table() or read.csv(). I think this is often the best solution.

Resources