Functions from my own developed R library not working - r

I created some functions and try to build a package/library, but run into some problems. I hoped to get some advise here on this support site.
This is how I tried to built my package.
I had some functions in an R file, including roxygen2 comments.
Then in R
library("devtools")
library(roxygen2)
setwd("C:to/my/directory")
create("myPackage")
Then I put my functions file in the R folder and changed the DESCRIPTION file.
setwd("./myPackage")
document()
setwd("..")
install("myPackage")
library(myPackage)
Then, when I want to see how a function works
?function1
I see the documentation in my browser.
But when I want to use my function I get an error.
function1
Error: object 'function1' not found
function1()
Error: could not find function "function1"
Does anyone know what I am doing wrong? I am using R 3.3.1 on Windows 7.
Your help or comments are appreciated, thanks in advance!
Ben

Related

How to load self-written package documentation with '?my_package_name'?

I made a package in R called "my_package_name". When I run ?my_package_name or ??_my_package_name, no results are found. I want a help file to be loaded in the same way that ?ggplot2 loads a package help file.
I can run ?my_function_name to obtain the help files for my functions. However, this does not work with my package name, even though the description file is complete. I found that help(package = my_package_name) loads a page that contains the description file and help pages, but I would like load a page with ?my_package_name.
I would say that the easiest way is probably to run the following from the usethis package. It will create the file needed to have your package documentation
usethis::use_package_doc()
I suggest reading the documentation of the function to understand what's going on under the hood.

Why can R not find function "getStates"

I am trying to create a character map and was recommended to use Phylotools and Ape packages in R.
I have installed the packages but when I try to do function getStates it comes up with an error message:
x<-getStates(nexdata,"tips")
Error in getStates(nexdata, "tips") : could not find function "getStates"
I have installed the right packages (so I think) and I am quite stuck. Any help would be really appreciated. If you need me to explain anything in more detail let me know.
The function library() loads and attaches add-on packages. You are using phytools and ape, so you should have in your code
library(phytools)
library(ape)
You can read more in the documentation, e.g. at rdocumentation.org/packages/base/versions/3.6.1/topics/library.

Created R Package, Unable to Display Photo

Update
Issue resolved
Update, still not working
Tried the following in R file
(1) deleted both library(...) packages
(2) Added #import jpeg before ShowPalettePhoto() and #import tidyverse before RanglaPunjab() so roxygen automatically adds to NAMESPACE.
After running devtools::document(), ran devtools::use_package("jpeg") and devtools::use_package("tidyverse") to automatically add to DESCRIPTION.
Unfortunately, even in testing, I cannot get JPEG photo.
Here is GitHub repository, https://github.com/ArtieLadie/RanglaPunjab
I created R package according to this tutorial
It worked and I was able to execute all commands, including a function to display photo in another directory.
I uploaded to my GitHub account. Anyone can install package in R environment with install_github("ArtieLadie/RanglaPunjab")
I am able to run functions by adding RanglaPunjab:: in front of it, i.e.
RanglaPunjab::PaintPalette("Jutti")
?RanglaPunjab::MergePalette
However, when I try to run ?RanglaPunjab::ShowPalettePhoto("Teej") I get
Error in readJPEG(x, native = TRUE) : could not find function "readJPEG"
Before creating the package I added function to set working directory to file location, but it was creating errors when I ran install("RanglaPunjab"), i.e. "Cannot execute"
Here are the exact commands I had, which I had to delete from code
library(rstudioapi)
current_path <- getActiveDocumentContext()$path
setwd(dirname(current_path ))
Please help
Your dependencies are not handled correctly. Here you explicitly load packages with library(...). That is not how one does that in an R package. You should add your dependencies to the Imports: section of the DESCRIPTION file and use the package::function() syntax when calling the function. c.f. http://r-pkgs.had.co.nz/description.html#dependencies.
In addition, if you want the images to be installed with your package, you should place them for example in inst/pics. You can then get the path to these files with
system.file("pics", <file-name>, package = "RanglaPunjab")

Create main help page (index) for R package using devtools

I am building a R package using devtools. All documentation is built using roxygen2. For the functions this works all fine, but how can I provide a help page for the whole package, that lists all the available functions.
In other packages there's always a link in the bottom of each help page which leads to the index page:
Screenshot from dplyr package (exemplary index link)
How can I built/link this index page with devtools?
EDIT: If I access a help page by "?functionName", there will be also the following output printed to the console "Using development documentation for functionName". From the github repository of devtools I find the function dev-help.R that gives this output. In its comments it's stated that links won't work with this development help.
Note that this only renders a single documentation file, so that links to other files within the package won't work.
So how can I use the normal documentation instead of dev-help?
Found the solution. If you have the following workflow:
create()
document()
build()
install.packages()
library(<pkg-name>)
the documentation will be loaded in the namespace of R during document(). Accordingly, a later call of ?functionName will refer to the development stage of the documentation and not the one provided by the compiled package.
Thus, creating a fresh R session after installation just solves the issue!

How to know where a R package has been installed

Good moorning
I would like to know if there is a way to find where a package is installed.
Actually, I am currently documenting a package. In my package, I have a function called "read.myfile" which reads a specific kind of file (roughly like read.table).
I have an instance of this kind of file named "myfile.txt" in my package's folder. On my documentation, I want to run an executable example of this function.
That's why I need the path, where the user has installed the package. So with this path, I can obtain the path of the file "myfile.txt" and use the function "read.myfile" in the .Rd help file, which gives help about the function "read.myfile".
Thus my example will be executable wherever the user has installed the package.
I hope my message was clear.
I don't know if it's possible to do that, but if anyone knows, thanks for helping me.
Use the function system.file.
For example:
system.file(package="ggplot2")
[1] "C:/Users/Andrie/Documents/R/win-library/3.0/ggplot2"
You can use installed.packages and subset to get the only the location of the library in which it is installed:
installed.packages()["tools","LibPath"]
[1] "C:/Program Files/R/R-2.15.2/library"

Resources