writing first package; function not found - r

I'm working on my first package (GitHub). Pretty basic. Two functions.
R CMD INSTALL --no-multiarch --with-keep.source qualtricsR
library(qualtricsR)
Works fine as far as I can tell. Running check in RStudio results in a final message of R CMD check succeeded.
However, when I try to run my functions (e.g., template <- importTemplate("inst/doc/template.xlsx")), I get an error that R cannot find my function. ?importTemplate shows my help page.

#jablacalle and #mdsumner put me on the right track.
Adding export(importTemplate) and export(writeQtxt) to NAMESPACE did the trick. running devtools::document() automated this process once I added #' #export before the function in my .R file.

Related

Run all testthat tests for an R package?

I googled, found this; accordingly, ran test_dir() (it errors), tried test_dir(getwd()) from the package root (it also errors). I also tried test_package(), but it also errors if the argument is left NULL or returns nothing if the package name is provided as a string.
Question
How do you quickly run all tests in a package from the R console (assuming getwd() is at the package root)?
Note
I know you can R CMD check but I don't need the other checks, just the tests
You simply run
devtools::test()
Note there are also some other methods here

r: errors creating package with devtools & roxygen2

I'm writing a package containing several functions to make running and evaluating models more streamlined.
I have a function that I'm going to make the first function within my package detailed with roxygen2 comments, which I can include into this write-up as an edit if necessary, but my issue is more with Package Creation.
I've created a separate .R file for the function and it lives within the R folder in within my package folder. I've run R CMD build pkgname and R CMD INSTALL pkgname successfully.
At the document() stage I run it (from console or whether in my terminal using R -e 'library(devtools);document()', deleting the existing NAMESPACE file first) and I get the following error: Try removing ‘/Library/Frameworks/R.framework/Versions/ 3.5/Resources/library/00LOCK-pkgname.
I've already seen the [issue posted here][1] and haven't had success after deleting the 00LOCK-pkgname folder, for two reasons: when I run document(), even when it throws the above error, it doesn't stop running, it just keeps looping (that happens whether I run this in R or use the Terminal). Additionally, no matter how many times I delete the folder, it keeps re-appearing even though I've stopped running the function.
Any insight into why that error is being thrown and the document() function continually runs in a loop?
Best answer I've found is in this blog post: Hilary Parker R-Package Blog Post
The steps I follow to document and install are as follows:
Within the project that contains my package, open a new R Script and run setwd('..')
Run devtools::document()
Run devtools::install()
This works for me when initially installing my package and also updating it.

How does a typical Rcpp edit-compile-test cycle look like?

I can only find information on how to install a ready-made R extension package, but it is nowhere mentioned which commands a developer of an extension package has to use during daily development. I am using Rcpp and I am on Windows.
If this were a typical C++ project, it would go like this:
edit
make # oops, typo
edit # fix typo
make # oops, forgot an #include
edit
make # good; updates header dependencies for subsequent 'make' automatically
./fooreader # test it
make install # only now I'm ready
Which commands do I need for daily development of an Rcpp package project?
I've allocated a skeleton project using these commands from the R command line:
library(Rcpp)
Rcpp.package.skeleton("FooReader", example_code=FALSE,
author="My Name", email="my.email#example.com")
This allocated 3 files:
DESCRIPTION
NAMESPACE
man/FooReader-package.Rd
Now I dropped source code into
src/readfoo.cpp
with these contents:
#include <Rcpp.h>
#error here
I know I can run this from the R command line:
Rcpp::sourceCpp("D:/Projects/FooReader/src/readfoo.cpp")
(this does run the compiler and indicates the #error).
But I want to develop a package ultimately.
There is no universal answer for everybody, I guess.
For some people, RStudio is everything, and with some reason. One can use the package creation facility to create an Rcpp package, then edit and just hit the buttons (or keyboard shortcuts) to compile and re-load and test.
I also work a lot on a shell, so I do a fair amount of editing in Emacs/ESS along with R CMD INSTALL (where thanks to ccache recompilation of unchanged code is immediate) with command-line use via r of the littler package -- this allows me to write compact expressions loading the new package and evaluating: r -lnewpackage -esomeFunc(somearg) to test newpackage::someFunc() with somearg.
You can also launch the build and test from Emacs. As I said, it all depends.
Both those answers are for package, where I do real work. When I just test something in a single file, I do that in one Emacs buffer and sourceCpp() in an R session in another buffer of the same Emacs. Or sometimes I edit in Emacs and run sourceCpp() in RStudio.
There is no one answer. Find what works for you.
Also, the first part of your question describes the initial setup of a package. That is not part of the edit/compile/link/test cycle as it is a one off. And for that too do we have different approaches many of which have been discussed here.
Edit: The other main misunderstanding of your question is that once you have package you generally do not use sourceCpp() anymore.
In order to test an R package, it has to be installed into a (temporary) library such that it can be attached to a running R process. So you will typically need:
R CMD build . to build package_version.tar.gz
R CMD check <package_version.tar.gz> to test your package, including tests placed into the testsfolder
R CMD INSTALL <package_version.tar.gz> to install it into a library
After that you can attach the package and test it. Quite often I try to use a more TTD approach, which means I do not have to INSTALL the package. Running the unit tests (e.g. via R CMD check) is enough.
All that is independent of Rcpp. For a package using Rcpp you need to call Rcpp::compileAttributes() before these steps, e.g. with Rscript -e 'Rcpp::compileAttributes()'.
If you use RStudio for package development, it offers a lot of automation via the devtools package. I still find it useful to know what has to go on under the hood and it is by no means required.

How do I fix errors from R CMD check --as-cran to get my R package accepted on CRAN?

Executive Summary
I need assistance to fix the errors as I attempt to create a manual for a new R package. I have created an R package on my computer that I would like to distribute. At the moment, the package is available as a repository on GitHub (http://www.github.com/greenspb/poker), but I want to contribute it to the Comprehensive R Archive Network. I wrote the code years ago, but never shared it out of fear. This is my first repository on GitHub and my first submission to CRAN. I have researched how to get my package on CRAN by reading a blog post at http://kbroman.org/pkg_primer/pages/cran.html . I am stuck on step 1:
"Run R CMD check --as-cran and eliminate all problems. If there are any errors or warnings, your package will not be accepted at CRAN. And even a “Note” will likely disqualify you. So figure out what all of those errors, warnings, and notes mean and then revise your package so that they are no longer issued."
The report generated by "Run R CMD check --as-cran poker" for my "poker" package ( available as a repository on GitHub ) contains errors. My concern is failure to make the LaTeX manual.
Resolved Issues
I used roxygen2 comment tags in the .R file in the project's R folder. roxygen2::document() successfully translated \cr in a comment tag into a new line in the LaTeX file, however it unsuccessfully translated every empty \tab in a comment tag into {} [empty curly parentheses] in the LaTeX file. This happened in dozens and dozens of instances. The \tabular environment was used in an attempt to preserve formatting. I was able to clean up LaTeX errors whenever "There was no line here to end" by searching for {} and replacing appropriate instances with \\{}.
Unresolved Issues
I have no idea how to solve the LaTeX error "Rd2.tex: File Ended while scanning definition of \LT#xxiii", although I believe \LT stands for long table. I am only able to make pdf manuals of several individual functions using R CMD Rd2pdf on the .rd documentation files.
My code is documented using roxygenize tags. When I execute the Run R CMD check --as-cran command, R encounters errors and warnings and fails to make the pdf for the entire package. I need assistance to fix the errors in the report. I want to get my package on CRAN.
See Also
Question on r-package-devel#r-project.org mailing list.
Removing comments on the local variables was the answer to fixing the errors ( replying to #Dason). Thank you.

R CMD Check does not finish the step "Checking PDF version of manual ..."

I am creating an R package using R Studio version 0.99.489 together with the most current R development version on Windows 7. I created documentation .Rd files using roxygen2 and a .Rmd vignette using knitr. Until now everything worked out fine, but when I run the R CMD check within RStudio, the check gets stuck at the step "Checking PDF version of manual ...". I neither get an "OK" nor any error or warnings message. The step just does not proceed, even if I run it over night. I do not get any error or warning messages for the other steps as well, I have "OK"s everywhere.
Consequently, I do not find a PDF manual in the package.Rcheck folder. I already created the manual myself using R CMD Rd2pdf package, which worked out fine. I copied it to several locations within the package folder but this did not help at all.
I searched really a lot but I could not find any solution for this since I do not get any error message. I thought about administration, latex, or R version problems, but I cannot figure out if it is any of those or something else.
One thing I recognized is that when the package is build, it prints out:
CMD build "..PATH...\package" --no-resave-data \ --no-manual
although I did not specify the --no-manual option in the Build options from RStudio. Additionally, I did not find a way to change this. Might this be the problem?
Can anyone help me with this? I would really appreciate any hints. Please let me know if you need any output or any other information from me.
Thanks!
You could do it yourself using:
shell('R CMD Rd2pdf . --output=man/figures/manual.pdf --force --no-preview')
This will create the PDF and save it to yourpackage/man/figures/manual.pdf.
I use it, so I know it works before I send it to CRAN.
I faced the same problem. It was really frustrating that it didn't give any error message. But then I tried this:
install.packages("devtools")
Actually I don't really know how it works, but I guess it also install other requirements and now the checking process is done via devtools and not directly by roxygen2. Hope it helps.
I also come across with this problem, which I think it's a bug of RStudio. Try R CMD check <YOUR PROJECT NAME> in terminal, in my case, it finally generates an error,
* checking PDF version of manual without hyperrefs or index ... ERROR
Re-running with no redirection of stdout/stderr.

Resources