I am quite new to PLSQL programming so I am still facing general problems like this one.
I have developed some standalone functions (Oracle PLSQL functions) and I want to group them under the same package.
So I created a new package, but I have few questions about what to do next:
Is there any possible way to link directly functions to my package? I don't think so, but maybe...
Am I allowed to name functions within the package in the same way as the external functions or I will cause a scope problem?
Thank you for your help.
Marcello
You can't link stand-alone function to a package, you simply have to copy the code of the function into the package body, and the function specification into the package specification.
For example if you have:
create or replace function my_function
return number
is
begin
return 42;
end;
... then you could copy it into the package like this:
create or replace package my_package is
function my_function
return number;
end;
and
create or replace package body my_package is
function my_function
return number
is
begin
return 42;
end;
end;
You can give them the same name as the stand-alone functions without any issues. If you call one of these functions from within the package unqualified (just my_function) then you will be calling the package version; if you really wanted to call the stand-alone function from the package then you'd need to add the schema prefix: my_schmea.my_function. But hopefully you wouldn't really want to do that!
Related
In R, I'm getting an error "could not find function...". The function is present inside the package. Still when I run the package, getting the error.
I am getting this error in the ChainLadder package while running MackChainLadderFunctions.R. For example, the function checktriangle is present inside the package in Chainladder.R. Still, R is not able to recognize the function or call the function.
Two problems here.
function names are case-sensitive (checkTriangle, not checktriangle)
checkTriangle is not exported from the package (i.e., it's a private function intended for use within the package only), so you need ::: to access it ... try ChainLadder:::checkTriangle.
Using private functions is "at your own risk/programmer beware"; private functions are undocumented, may change in future versions, etc.. If you can find a way to do what you need to do with public functions, that is generally preferred.
AFAICT you're running into this problem because you're trying to source() (or cut-and-paste) package code in your R session. This shouldn't happen if you load the package with library("ChainLadder") and use the public functions (if it does, please edit your question to give a little more context about how you're using the package ...)
In my work I develop R packages that export R data objects (.RData). The name of these .RData files is always the same (e.g. files.RData). These packages also define and export a function that uploads the data to my database, say upload_data(). Inside upload_data() I first load the data using data(files, package = "PACKAGE NAME") and then push it into my database.
Let's say I have two packages, package1 and package2, which live on my file system. Given a vector of the package names (c("package1", "package2")), how would I go about to call 'upload_data()' programatically? Specifically, inside a script, how would I construct a call using "::" notation that constructs and evaluates a call like this: package1::upload_data()). I tried 'call' but couldn't get it right.
You could go the route of constructing the call using :: notation and evaluating that - but it's probably just easier to directly use get and specify the package you want to grab from.
get("upload_data", envir = asNamespace("package1"))
will return the function the same as using package1::upload_data would but is much easier to deal with programatically.
I would like to include a closure with the functions of an R package we are writing. The function (and its siblings) will have data in its environment, perform a comparison of input with the data, and return the result. To illustrate, think of a function with an inbuilt telephone directory: you query with a number and the function returns a name.
This function will be called as a helper by several other functions in our R package, so it has to exist once the package is loaded. And we want the function to be available in the package environment, just like any other function.
Should I create it via its factory function in .onLoad() and assign() it to the package environment? Could I ship it as an .RDS? Or RData, or does this violate CRAN policy on "binary executable code"? Or is there a different, canonical way? And where would the code and the data (or the RDS/RData) go in the package directory structure?
(I see that the question of how to document a closure has been discussed here).
For the benefit of anyone stumbling on this question. The solution I finally worked out involved a few steps but is "clean" as far as I can tell.
Put the factory function in a file R/aaa.R to ensure it gets loaded before the closure.
Put the data that the closure uses into the standard inst/extdata/ folder.
Put a file with the closure's name and proper docstring into R/: define the closure as a normal function that just returns nothing. This is necessary so the function is properly exported and known in the package namespace. Immediately call the factory function to create the closure and overwrite the original definition. Note: it's not enough to just bring the data into the factory function as an argument, it actually needs to be accessed before defining the closure. Why? That's because lazy loading won't actually have loaded the data into the environment you need it in unless you access it.
That's all. Summary: create a stub for your closure, then overwrite that with the return value of the factory function.
If the factory function is called later by the package user
but we still want the returned closure to be inside the package (for example if we don't want it to be changed by anything other than the factory, reliably accessible from within the package, documented etc..):
# exported function (visible to user)
# everything this function does is 'outsourced'
# to a non-exported function that we can overwrite with the factory:
visible_function(...){
hidden_function(...)
}
# not exported function (invisible to the user)
# called by the visible function
# fails unless factory is called first
hidden_function(x){
stop("call factory_fun() before you can use visible_function()")
}
# exported function, visible to the user.
# changes the hidden function called by the visible function
factory_function(x){
produced_function<-function(){
print(paste(x, "is an object forever stored in my namespace!"))
}
assignInNamespace("hidden_function",
produced_function,
ns="myPackageName")
}
Note that R CMD check throws a NOTE on assignInNamespace so CRAN won't easily accept this solution
I am writing an R package. Generally, I have some functions that they are not useful for external uses. So when I put them in Namespace file, it causes an error about documentation of functions. On the other hand, if I remove them from Namespace file, it causes another problem, Function not found. So, is there any way of calling a function without a need of writing documentations?
As Andrie commented if you want to include the function in the R package you need to put it inside a folder (e.g. packageparent/R/) and declare in NAMESPACE. You do not put a function in NAMESPACE.
IF you do not want to include it in your package, none of your functions in your package shall call this function, otherwise the package does not compile. You still can include this function in your package and not write any documentation for it.
To use this function outside your package just source it
I am new to PL/SQL so just trying to figure out the general flow of creating a package
CREATE OR REPLACE PACKAGE P1 AS
PROCEDURE PROC1
(
);
END P1;
CREATE OR REPLACE PACKAGE BODY P1 AS
//package definition
END P1;
Is this the correct way to define the package?
Basically, I am trying to find out whether I can declare the package and define the package body in the same file or would i need to create 2 separate files?
When I try to execute it, I get the error Encountered the word 'PROCEDURE' when expecting one of the following
Generally, you have the specification of the package in a file, and the body in another. Why are you trying to put them in the same file?
It doesn't matter for an individual package, but you must declare the specification before the body.
Where you are creating multiple packages it is best to create all of the specifications first because the bodies can then compile even if they reference a different package for which the body has not yet been created.