Rcpp can't find nloptrAPI.h header - r

I'm trying to find an Rcpp replacement for the base optimize function. This link
https://github.com/eddelbuettel/rcppnloptexample/blob/master/src/nlopt.cpp
is a potential solution but I can't get past the sourceCpp error
Error in Rcpp::sourceCpp("R/nlopt.cpp") :
Error 1 occurred building shared library.
> library('nloptr')
> Rcpp::sourceCpp("R/nlopt.cpp")
nlopt.cpp:4:10: fatal error: 'nloptrAPI.h' file not found
The header file is in fact on my computer at /Library/Frameworks/R.framework/Versions/4.1/Resources/library/nloptr/include
I can include the whole path to the header and it seems to work fine but that seems a bit kludgy.
What do I need to do to tell R or Rcpp where to look?

This link as some useful discussion about the issue of finding headers.
https://stackoverflow.com/questions/13995266/using-3rd-party-header-files-with-rcpp has useful information.
To find out where your 'missing' header is located, the /Library/Frameworks ... link above is useful, replacing nloptr with the name of the package that has the header you are looking for.

Related

devtools::check() creating warning that only contains NULL

I'm currently doing some work on a package, and calling devtools::check() is producing the following warning:
❯ checking Rd files ... WARNING
NULL
However, when manually inspecting each of the .Rd files created by devtools::document() and re-reading the documentation, I don't see why this warning is appearing. I know it's only a warning and doesn't impact the functionality of the package, but I can't figure out where this is coming from.
Any help is greatly appreciated.
This rig-related bug has been fixed with version 0.5.2, see r-lib/rcmdcheck#184 on GitHub. #rossdrucker9 I am well aware that you already know about this.. just want to write it down for future generations that end up here first (like I did).

R: Error in as.environment(pos) / Error during wrapup: no item called "package:[package]" on the search list

This is similar to this but with a followup question:
I'm preparing my package for submission to CRAN. In one script, it gives the titular error:
Error in as.environment(pos): no item called "package:shapefiles" on the search list
or
Error during wrapup: no item called "package:shapefiles" on the search list
If, like the similar thread linked above, I add
attachNamespace("shapefiles")
Then I no longer get that error, but instead I sometimes get the error or warning:
namespace(shapefiles) was already taken
I believe this happens if the function/script is run more than once. What's so weird about this to me is that
#' #import shapefiles
is in that script's markdown block,
Which means
import(shapefiles)
is in NAMESPACE, and
Imports:
shapefiles (>= 0.7),
is in DESCRIPTION. I have numerous other package dependencies which are imported the same way (correctly, I believe), and none of them produce this error. I don't see why this is behaving differently therefore, and would like to avoid having either issue before submitting to CRAN. The code that calls this package, in my script, is
shapefiles::read.shapefile(savename)
Script is here, for reference.
Thanks in advance for any ideas!
Edit: steps to reproduce:
setwd("/folder")
library(devtools)
install_github("SimonDedman/gbm.auto")
library(gbm.auto)
gbm.basemap(bounds = c(-81.7, -80.3, 24.7, 25.9),
savedir = "/folder")
I've just removed shapefiles:: from the code. The correct output is produced regardless.
Problem resolved itself after commenting out attachNamespace("shapefiles"). No idea why.

Text mining with tm in R antiword error

So I'm rather new to R, and I'm learning how to mine text from this handy website: https://eight2late.wordpress.com/2015/05/27/a-gentle-introduction-to-text-mining-using-r/
I do have my own text set of .doc, .docx, and .xlsx files and I'm trying to mine them. They're located in a folder in my working directory called 'files', but I have already encountered an error after simply writing a few lines of code.
The code I have so far is:
library(tm)
library(readtext)
data = readtext('files')
At this point, after waiting for 25 seconds or so, I get the error:
Error: System call to 'antiword' failed (1): The Big Block Depot is damaged
and the code stops running there.
I have tried searching online for solutions but it seems like a fairly rare error and so I only found 1 possible solution at https://github.com/ropensci/antiword/issues/1 but that did not work for me.
This solution suggested that one of my files were corrupt, and suggested using the code
fixInNamespace(antiword, pos="package:antiword")
to change the error to a warning to not interrupt the reading of the files. I tried that, and at first it raised the error of
Error in as.environment(pos):
no item called "package:antiword" on the search list
After which, I loaded the antiword library with a library(antiword) and changed the stop( to a warning(. However, when I ran the data = readtext('files') line again, it immediately raised the error
Error in is_windows() : could not find function "is_windows"
I'm at a loss here! Any help would be appreciated. Should I be using another package in this case?
I had the same problem with my code, where I tried to get a doc. file in R. I also used the readtext library. What helped me was converting the Word documents I was trying to get into R from doc. to docx. When I ran the same code after it worked.

"Warning message: package ‘ XLConnect ’ is not available (for R version 3.3.3)"

I receive this message when I try to install "XLConnect" in R. I am trying to use some data from excel, and I don't know how else to load it into the program.
I'm completely new to R and programming, so any help is greatly appreciated!
This usually happens when there is a typo in the package name. The code below should work. My guess is that the capitalization might have been off when you tried it?
install.packages("XLConnect")
I'm reasonably sure that you had spaces on either side of term "XLConnect". At least that is what your error message is telling us. If trying what Mallick suggested does not succeed, then [edit] to include exact copies of any error message in the body of the question.
Other ways to transfer data from Excel to R include copying to the Clipboard or exporting as a '.csv' file. There must be hundreds of questions and answers about the Excel - to - R Highway Eternal Resurfacing Project. One more recent addition is the readxl package (function is read_excel) and that choice doesn't have the Java version dependencies that cause trouble for some XLConnect useRs.

Package development: run_examples could not find function

I'm developing an R package, pk, say. I've successfully installed it and used it, and now I'm trying to clean up the examples with the help of the run_examples function in the devtools package. However, it crashes on the very first example:
> require(pk)
> require(devtools)
> run_examples("~/[full path]/pk")
Updating pk documentation
Running 45 example files in pk
--------------------------------------------------------------------------------
Loading pk
Running examples in pk-package.Rd
--------------------------------------------------------------------------------
1> ########################################################################
1> ## Simulate a dataset ... blah blah
1> set.seed(1)
1> x = my_pk_fun(a = 1)
Error in eval(expr, envir, enclos) : could not find function "my_pk_fun"
Loading pk
As I already loaded my package (using require), what more do I have to do to make my_pk_fun visible to run_examples? I've checked that my_pk_fun is indeed present in my R session.
UPDATE:
Following the comment by Dirk, I examined my namespace file, and found it to be completely empty. As suggested (though not recommended) by the documentation, I inserted exportPattern("^[^\\.]"), which, it seems, is supposed to export all of the functions, including my_pk_fun. However, upon repeating the experiment above, (a) I get the same error, and (b) the contents of the namespace file are deleted! Why does run_examples empty my namespace file?
I conjecture a resolution of my own question:
One detail that I omitted is that before I could get run_examples to do anything at all, it required me to first install the roxygen2 package (even though roxygen2 is listed only under "suggests" in the devtools documentation!).
When digging through the source code of run_examples, I did indeed find some use of roxygenize. It has been previously noted that roxygenizing a package can have the effect of rewriting the NAMESPACE file.
To be explicit, the reason that run_examples empties my NAMESPACE file is that (1) run_examples roxygenized my package while (2) I never included any # #export command (or any other roxygen2-speak, for that matter) in my source files.
Conclusion: Unless you are building your package within the roxygen2 framework, and including all of your .Rd documentation as comments in your source code, then do not use run_examples! It seems like there should be a warning about this in the run_examples documentation.
A way out: If you really must use run_examples, and you're willing to learn a little bit about roxygen, which is actually pretty cool, then start here.

Resources