Goland: Install package "assert" without leaving Goland? - goland

I want to install the well-known "assert" package from https://pkg.go.dev/github.com/stretchr/testify/assert
I can remember "assert", but not the full URL.
I could leave the IDE and use my favorite search engine,
but maybe there is way to install and import the "assert" package
without leaving Goland?
Expected result:
import (
+ "github.com/stretchr/testify/assert"
"testing"
)
And go.mod and go.sum get updated.
With other words: I want to get to this string ("github.com/stretchr/testify/assert") without leaving Goland.

Related

Creating R package using code from script file

I’ve written some R functions and dropped them into a script file using RStudio. These are bits of code that I use over and over, so I’m wondering how I might most easily create an R package out of them (for my own private use).
I’ve read various “how to” guides online but they’re quite complicated. Can anyone suggest an “idiot’s guide” to doing this please?
I've been involved in creating R packages recently, so I can help you with that. Before proceeding to the steps to be followed, there are some pre-requisites, which include:
RStudio
devtools package (for most of the functions involved in creation of a package)
roxygen2 package (for roxygen documentation)
In case you don't have the aforementioned packages, you can install them with these commands respectively:
install.packages("devtools")
install.packages("roxygen2")
Steps:
(1) Import devtools in RStudio by using library(devtools).
(devtools is a core package that makes creating R packages easier with its tools)
(2) Create your package by using:
create_package("~/directory/package_name") for a custom directory.
or
create_package("package_name") if you want your package to be created in current workspace directory.
(3) Soon after you execute this function, it will open a new RStudio session. You will observe that in the old session some lines will be auto-generated which basically tells R to create a new package with required components in the specified directory.
After this, we are done with this old instance of RStudio. We will continue our work on the new RStudio session window.
By far the package creation part is already over (yes, that simple) however, a package isn't directly functionable just by its creation plus the fact that you need to include a function in it requires some additional aspects of a package such as its documentation (where the function's title, parameters, return types, examples etc as mentioned using #param, #return etc - you would be familiar if you see roxygen documentation like in some github repositories) and R CMD checks to get it working.
I'll get to that in the subsequent steps, but just in case you want to verify that your package is created, you can look at:
The top right corner of the new RStudio session, where you can see the package name that you created.
The console, where you will see that R created a new directory/folder in the path that we specified in create_package() function.
The files panel of RStudio session, where you'll notice a bunch of new files and directories within your directory.
(4) As you mentioned in your words, you drop your functions in a script file - hence you will need to create the script first, which can be done using:
use_r("function_name")
A new R script will pop up in your working session, ready to be used.
Now go ahead and write your function(s) in it.
(5) After your done, you need to load the function(s) you have written for your package. This is accomplished by using the devtools::load_all() function.
When you execute load_all() in the console, you'll get to know that the functions have been loaded into your package when you'll see Loading package_name displayed in console.
You can try calling your functions after that in the console to verify that they work as a part of the package.
(6) Now that your function has been written and loaded into your package, it is time to move onto checks. It is a good practice to check the whole package as we make changes to our package. The function devtools::check() offers an easy way to do this.
Try executing check() in the console, it will go through a number of procedures checking your package for warnings/errors and give details for the same as messages on the screen (pertaining to what are the errors/warnings/notes). The R CMD check results at the end will contain the vital logs for you to see what are the errors and warnings you got along with their frequency.
If the functions in your package are written well, (with additional package dependencies taken care of) it will give you two warnings upon execution of check:
The first warning will be regarding the license that your package uses, which is not specified for a new pacakge.
The second should be the one for documentation, warning us that our code is not documented.
To resolve the first issue which is the license, use the use_mit_license("license_holder_name") command (or any other license which suits your package - but then for private use as you mentioned, it doesn't really matter what you specify if only your going to use it or not its to be distributed) with your name as in place of license_holder_name or anything which suits a license name.
This will add the license field in the .DESCRIPTION file (in your files panel) plus create additional files adding the license information.
Also you'll need to edit the .DESCRIPTION file, which have self-explanatory fields to fill-in or edit. Here is an example of how you can have it:
Package: Your_package_name
Title: Give a brief title
Version: 1.0.0.0
Authors#R:
person(given = "Your_first_name",
family = "Your_surname/family_name",
role = c("package_creator", "author"),
email = "youremailaddress#gmail.com",
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: Give a brief description considering your package functionality.
License: will be updated with whatever license you provide, the above step will take care of this line.
Encoding: UTF-8
LazyData: true
To resolve the documentation warning, you'll need to document your function using roxygen documentation. An example:
#' #param a parameter one
#' #param b parameter two
#' #return sum of a and b
#' #export
#'
#' #examples
#' yourfunction(1,2)
yourfunction <- function(a,b)
{
sum <- a+b
return(sum)
}
Follow the roxygen syntax and add attributes as you desire, some may be optional such as #title for specifying title, while others such as #import are required (must) if your importing from other packages other than base R.
After your done documenting your function(s) using the Roxygen skeleton, we can tell our package that we have documented our functions by running devtools::document(). After you execute the document() command, perform check() again to see if you get any warnings. If you don't, then that means you're good to go. (you won't if you follow the steps)
Lastly, you'll need to install the package, for it to be accessible by R. Simply use the install() command (yes the same one you used at the beginning, except you don't need to specify the package here like install("package") since you are currently working in an instance where the package is loaded and is ready to be deployed/installed) and you'll see after a few lines of installation a statement like "Done (package_name)", which indicates the installation of our package is complete.
Now you can try your function by first importing your package using library("package_name") and then calling your desired function from the package. Thats it, congrats you did it!
I've tried to include the procedure in a lucid way (the way I create my R packages), but if you have any doubts feel free to ask.

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")

Suppress mFilter onLoad message

I'm creating an R package and it cannot show any kind of message from imported packages when it loads. I'm having a problem with an specific package, mFilter. If I import it, I always get
‘mFilter’ version: 0.1-3
‘mFilter’ is a package for time
series filtering
See ‘library(help="mFilter")’ for
details
Author: Mehmet Balcilar,
mbalcilar#yahoo.com
when the user loads my package, regardless of adding suppressMessages('mFilter') in the .onLoad file.
I really need to use mFilter. So removing it from Imports list doesn't help. Does anyone know what should I do?
I don't think you can. In the mFilter package, instead of using message() in .onLoad(), the authors have incorrectly used
if(interactive() || getOption("verbose"))
writeLines(strwrap(txt, indent = 4, exdent = 4))
If you are using the package interactively this will always execute and won't be suppressed.
If you can limit your use of mFilter to just a few functions, you could Suggest mFilter rather than Import or Depend on it. Then, in the functions that need it, you could capture.output(require(mFilter, quietly = TRUE)) to load the package (and stop with a message that mFilter needs to be installed if the load is unsuccessful).
Alternately, you could take the same approach, but have the loading of mFilter take place in your package's .onLoad.
You may even be able to do something tricky where mFilter is listed in DESCRIPTION Imports (to guarantee it gets installed) but isn't imported in the NAMESPACE file. It would probably (at least) throw a warning during check, but it would probably work just fine.

character(0) warnings when running devtools::load_all(".") in RStudio

I have an R package that I've been building in RStudio, let's called it my_pkg. When I run devtools::load_all(".") within RStudio (specifically using the Ctrl + Shift + L shortcut), I get the following message:
Loading my_pkg
Warning messages:
1: character(0)
2: character(0)
3: character(0)
4: character(0)
5: character(0)
All of the functions in the package work fine. My NAMESPACE and DESCRIPTION files are complete with no syntax errors. When I run ?my_pkg, however, the help file does not match the specifications provided in the DESCRIPTION file. When I remove the Imports from DESCRIPTION, there is no more character(0) warning message. Of course, I need those imports. When I change Imports to Suggests, there is character(0) warning message.
Here is the description file content, with some stuff changed to protect IP.
Package: scoutdroid
Title: This is where the title is.
Version: 0.1
Authors#R: "Ben Hanowell <benjamin.hanowell#redfin.com> [aut, cre]"
Description: This is where the description is.
Depends:
R (>= 3.1.0)
Imports:
dplyr,
lubridate,
mboost,
randomForestSRC,
RODBC,
stringr
License: file LICENSE
LazyData: true
And here is NAMESPACE.
# Generated by roxygen2 (4.0.1): do not edit by hand
import(RODBC)
import(dplyr)
import(lubridate)
import(mboost)
import(parallel)
import(randomForestSRC)
import(stringr)
When I use the RStudio Build & Reload button in the Build tab, I get the following warnings:
** preparing package for lazy loading
Warning: replacing previous import by 'lubridate::intersect' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::setdiff' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::union' when loading 'scoutdroid'
edit Added some more details to help folks understand what might be going on.
edit 2 I also added the DESCRIPTION file, although I don't provide the full package, which is proprietary.
edit 3 Added NAMESPACE.
edit 4 Added warnings that occur when using RStudio Build & Reload button in the Build tab.
After some dialoge in the comments, we figured out that the empty warnings that load_all is giving you are actually initiated when loading the package because of function name conflicts.
The issue is that you are importing a function from a package, then overwriting that function. When that happens R throws warnings as you saw when you clicked "Build & Reload" in RStudio:
Warning: replacing previous import by 'lubridate::intersect' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::setdiff' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::union' when loading 'scoutdroid'
It looks like load_all may be attempting to muffle those warnings (just a guess) which is why you see character(0) instead of the actual warnings. (These particular warnings are difficult to silence.)
It is generally not a good idea to import an entire package's namespace. You should instead import only the symbols you need. See this post of mine for more.
The solution is to use importFrom instead of import in your NAMESPACE file.
It can also be due to a broken link in the roxygen2 documentation. For example when you link to a function external to your package with the wrong name, say \link[stringi]{STRI_C} instead of \link[stringi]{stri_c}

How do I load a package without installing it in R?

I have built an R package but I do not want my users to have to install it before using it.
Is there a way to load a package without having to install it?
For example, if I have a package mypackage.tar.gz, is there something like
library("mypackage.tar.gz")
?
I'll join in "the chorus" of suggesting you should really install the package.
That having been said, you can take a look at Hadley's devtools package, which will let you load packages into the workspace without dumping in your global workspace.
The package will have to be untar'd/unzipped and follow the standard R package structure.
In order for this to work, though, your users would have to have the devtools package installed, so ... I'm not sure that this is any type of win for you.
If you only need the code to be loaded without it being installed, take the raw R script and source it:
source(myScript.R)
If you have different functions, you can create an R script that just loads all the necessary source files. What I sometimes do when developing, is name all my functions F_some_function.R and my classes Class_some_function.R. This allows me to source a main file containing following code :
funcdir <- "C:/Some/Path"
files <- dir(funcdir)
srcfiles <- c(grep("^Class_",dir(funcdir),value=T),
grep("^F_",dir(funcdir),value=T)
)
for( i in paste(funcdir,srcfiles,sep="/")) source(i)
If you present them with the tarred file, they can untar themselves using untar() before sourcing the main file.
But honestly, please use a package. You load everything in the global environment (or in a specified environment if you use local=T), but you lose all functionality of a package. Installing a package is no hassle, and removing neither.
If it's a matter of writing rights on the C drive (which is the only possible reason not to use a package that I met in my carreer), one can easily set another library location. R 2.12 actually does this by itself on Windows. See ?.libPaths()

Resources