Creating a package in R using RStudio - r

I've created a bunch of files:
init.r
auth.r
class.r
modules/status.r
modules/mgmt.r
modules/core.r
modules/mcf.r
The source of the init.r file is:
# initiation of package
# include libraries
library(RCurl);
library(rjson);
# include files
source('auth.r');
source('class.r');
# extend class
source('modules/status.r');
source('modules/mgmt.r');
source('modules/core.r');
source('modules/mcf.r');
How do I go about creating a package out of this? The init.r file obviously needs to be initiated first.

Start with following the steps in this video:
Build an R Package in under 2 minutes with RStudio
Then read more about RStudio's Package Development feature, and also Hadley Wickam's Package basics.

See Writing R Extensions for the process of making a package. You might want to use package.skeleton to get started.
But essentially,
get rid of your init.r file,
put all your other .R files in the R directory
write Depends: RCurl, rjson in your DESCRIPTION file.

1. Build the prerequisites:
To build R Packages using RStudio, you should have the following prerequisites like
R,
RStudio,
Rtools,
Basic MikTex,
roxygen2 and devtools packages.
2. Create the RPackage project in RStudio and add all your R files (init.r,auth.r,class.r,modules/status.r,modules/mgmt.r,modules/core.r,modules/mcf.r) under the R folder of the project.
3. Add the documentation by editing the package description file. Build the project and now your package is ready to use.
it won't take more than 15 minutes to build a simple package.
If you wish to have step by step explanation to create a simple package, please visit this blog.
https://veeramaninatarajanmca.wordpress.com/2017/02/10/how-to-create-a-simple-package-in-r-using-rstudio/

Related

how can I install unpublished package from source code

A colleague of mine has just built a shiny app for 16s rRNA amplicon. for that he has built a helper package for shiny app which contains all other packages needed for the app to work for example: dada2, dplyr, plyr, DECIPHER, phangorn etc etc. He has installed it in R but now I have to change some code in that helper package to add some of the new features in the app but I can’t find a way to install that package after modifications This package does not have any tar.gz or any zip file only a folder containing R scripts.
NOTE: I am using ubuntu.
You can use e.g. remotes::install_local(path = "src") to install the R package with its source code stored in directory src.

Link to vignette from readme.rda

I am building an R package. I have several vignettes that I would like to include links to in my README.Rmd.
I know that vignettes are to be built optionally when installing the package.
I do not really understand where I should start. I am in the process of building the package in R studio. I would the user to be able to see the vignette just by clicking at link in the readme on GitHub. Is this possible? How?
The following obviously does not work.
[The main vignette](vignettes/Vignette.html)
You can do this, but it might be more trouble than it's worth.
The problems are
Your package directories are different in the source on Github than they are when your package is installed in R. The link you give would be fine if you actually put Vignette.html in the vignettes directory, but when your package is installed, it will be in doc.
RStudio won't put the processed vignette in either of those locations by default if you just knit Vignette.Rmd.
You don't normally commit output files on Github.
So here's what you could do to work around this. Make the link look like
[The main vignette](doc/Vignette.html)
To make sure that file is there on Github, in RStudio create the doc directory and run
rmarkdown::render("vignettes/Vignette.Rmd", output_file="doc/Vignette.html")
You'll need to commit the output file and push it to Github, but you don't want to include it when you build the .tar.gz file, so you'll also need to add the lines
^doc$
^doc/Vignette.html$
to the .Rbuildignore file in the main package directory.
With all these changes I think your vignette will be visible on Github and also after you install the package in R.
A much simpler approach is just to tell the user to run
vignette("Vignette", package = "yourpackagename")
after installing the package, but this won't make it visible on Github.

R -- What exactly does install_github do?

never used R before. I need to integrate R into continuous build.
The script I got has the line
RUN Rscript -e "devtools::install_github('my-repo', auth_token = '"$Github_Token"')"
I know this command will download the entire repo, but how is the package installed? Is it looking for the .R files, or is it looking for the .rba files?
My goal is to integrate a build process with a CI. I found a way to construct these rba files through a docker container, but these will not be checked into github. I need to make sure the install doesn't require these rba files, then I can move these files somewhere else.
The function install_github installs a package the same as any other method of package installation, but automates the download from github for you. The remote repository needs to be an R package, meaning it needs to have at least an R/ directory containing R code, a DESCRIPTION file containing the package metadata, and a NAMESPACE file describing the package imports and exports.
For install_github to work, it should not inherently require that your rba files are present.
For more information on packages, I suggest reading R packages.

R: Error in install.packages : cannot open the connection

I was trying to install the package RINDSEL but I am unable to install it and I keep getting the following error:
Error in install.packages : cannot open the connection
I downloaded the package from:
rindsel_1.0_2.zip | Integrated Breeding Platform
and loaded it from the directory. Other packages from the directory can be installed but just not this one.
Is the package corrupt or could there be any other error?
I would really be grateful for any help. Thanks in advance
Rename the zip file RinSel Software into Rindsel. That's the name specified in the discription file.
Then, you can install the package in R with the command
install.packages("C:/path/to/Rindsel.zip",repos=NULL,type="win.binary")
That works fine... at first (!!!).
Problem with the Rindsel package is. It is quite old. It was build with R 2.13.1. Therefore, if you want to load the library which would be the next step to use the package in R you will get the error:
Problems building package (Error: "package has been build before R-3.0.0")
My suggestion: Contact the authors of the package and ask them if they can either provide the source file that you can build the package by yourself or if they can bundle the Rindsel package with a newer R version.
(Or you could try to hunt down an old R version and see if you can get the thing running with an old R... However, I would not seriously suggest to do that. It would probably result in conflicting dependencies with the other required packages...)
EDIT 15-02-2018: OP asked if one can build an R package with sources that are presumeably the Rindsel source files.
Yes, basically, you could do that. You would have to make a your own description and namespace file and put the source file in the R folder than invoke the command in R to build it....
But it's not neccessary with the script files provided by the link the OP posted.
OP, just run the scripts in R! It's quite easy.
Download the zip-file and extract it on your machine.
Go to that directory. The R command would be
setwd('path/to/your/directory')
Than run the R script, e.g, the KNIndex.r. It's simple:
source('KNIndex.r')
Then the script will run and produce some output / prompts.
For future readers,
I was able to fix the error by running RStudio with administrative privileges to get the command to work.
If that does not fix it, you might wish to try
Installing "r tools" if that is not installed already. That can be downloaded from
https://cran.r-project.org/bin/windows/Rtools/
Download a relevant package that you are trying to install (e.g., tidyverse_1.3.0.zip) from https://cran.rstudio.com/
and install that from local path
It can also be installed directly from the web using install.packages("https://cran.rstudio.com/bin/windows/contrib/4.0/tidyverse_1.3.0.zip")
I had the same problem. R was not able to extract and compile the package files to the default installation directory for some system-specific reasons (not R related).
I was able to fix this by specifying the installation directory of the package lib using:
install.packages("your package", lib = 'path/to your/required/installation/directory')
You can then load the package by specifying the lib.loc option while loading it:
library('your package', lib.loc='path/to your/required/installation/directory')
A better solution:
Create a new environment variable (if you are using windows) R_LIBS_USER with the following directory path/to your/required/installation/directory.
This will change the default installation directory of the packages and make it easier to load and install them without specifying the location everytime.

How do I fix a bug in an open source R package? [duplicate]

This question already has answers here:
How to edit and debug R library sources
(3 answers)
Closed 6 years ago.
I'd like to fix a package, but can't figure out how to edit it. I can download the source from R-Forge as a .tar.gz and unzip it. There is an "R" directory with the source and also a "tests" directory.
How do I include the sources in my own project to test my edits?
How do I run the tests? The tests each start with "library(blotter)". How do I make that load the library from the sources I've downloaded.
The recommended process is described in some detail in the manual 'Writing R Extensions' that came with your R installation.
There are also numerous tutorials all over the web.
No need to re-zip the source. Just load the package again from the source on your drive:
install.packages(/path/to/package, repos = NULL, type="source")
your method of doing the for loop to loop across R files will work in some situations but in others it may not. For example, if there is compiled non R code then looping across the *.R files may not work properly. So it's generally better to just install.packages() again.
BTW, if you clone a version control repo to your hard drive you generally don't get the zip file. So the same method is used to install.
What I have done is the following (IIRC) remove the old package remove.packages. Re zip your new package to a tar.gz file again (with your changed sources). Install the new package with install.packages with pkg = path to your zipped library, and repos=NULL.
When I'm doing testing on a package, I use a little bash script that looks like this:
#!/bin/bash
#build the package from source
R CMD build ../pkgdirectory/
#remove the old version and install the new one
R CMD REMOVE pkgname
R CMD INSTALL pkgname_0.7.tar.gz
Replace package name with the appropriate names. Save it as "make" and then run it whenever you finish a major edit. Then your testing scripts can use library(pkgname) just like normal.
I found that I could load all the sources to save me from the ridiculous repackaging by doing:
for (file in dir("../R", pattern="*.R", full.names=TRUE)) {
source(file)
}

Resources