R package: is there a method to generate a diagram of methods and dependencies in a package? - r

I'm writing a package in R, defining a few S4 classes with inheritance and associated methods. I'd like to know if there's a package or method that would allow to automatically generate a flowchart summarizing classes, attributes and methods? Like some sort of ERD diagram?
Thank you.

Related

Self-authored package: load plot method for spatialPolygonsDataFrame

I'm writing my own R package and would like to plot a spatialPolygonsDataFrame object. If I were writing it as a script I would simply load the necessary packages (maptools, rgdal, and rgeos) with library() and plot with plot(x).
When writing a package to build using library() is not advised, instead it is usual to load the package by adding it to Imports: in the NAMESPACE. If I do this I receive the following error:
Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'
This is is corrected by loading the maptools package with library() if writing a script.
I know you can load individual methods with ImportMethodsFrom in the NAMESPACE so have tried to import a plot method from maptools using this approach but have had no luck. When I looked in the NAMESPACE of the maptools package I couldn't find a plot method exported. I've seen there is a plot.Spatial function which I have tried to import to my NAMESPACE without success:
No methods found in "maptools" for requests: plot.Spatial
Finally, I have tried adding maptools to Depends: instead of Imports: in my NAMESPACE and this does work. Is this the canonical way to do this? It seems overkill to attach a whole package for one method (plus I don't know what functions have been masked, etc.). What is the best way to load the necessary tools to plot maps within a self-authored function?
Edit 1: In response to #Hack-R's question, I don't know if plot.Spatial is the only method I need, or even if it's the correct one. It's my educated guess that this will enable me to plot spatial objects.
plot.Spatial is internal and is in sp and not maptools, which I think is the answer here. You are looking at the wrong package.
As discussed in the comments, you can simply use sp::plot.
For developing a package, there's a bit more to it.
If you import the methods for plot so that your functions can use it internally, but it won't be available to users unless they library(sp). You could re-export it, so your users don't have to attach sp - but you'll need to document it and perhaps explain why, and also check there's no issues if sp is attached.
This is a bit of a challenging topic that is well explained here: http://r-pkgs.had.co.nz/namespace.html I was pretty comfortable with namespaces but only recently realized you could re-export a function that you import from another - so you could provide sp's plot.Spatial without Depends: sp.
I override the print methods for Spatial in a package I use, and that in in turn overrides the overrides that raster provides - there's no stopping you doing this, it's a matter of managing the user expectations and hopefully not making things hard/er. You probably don't want to override a generic like plot for normal use, it's clearer if you have a myPlot that does that specifically, or add your own classes.
It's another level complicated though, since plot.Spatial is internal, and it's source is used to define an S4 method for plot. You can see the methods with showMethods("plot") and then get the internal functions that provide those with findMethods("plot")[["Spatial#missing"]] or findMethods("plot")[["SpatialPolygons#missing"]].
#mdsumner's answer pointed me in the right direction and was a useful discussion in its own right.
The answer to my specific query to plot spatialPolygonsDataFrame objects was to add sp to Imports: and call sp::plot()

Convert party object to pmml

I am currently trying to convert a decision tree created using the R package partykit (party object) to a pmml-format. Are there packages that allow for this conversion? I am aware of the existance of the pmml-package, but this only supports rpart objects, created using the R package rpart. As I want to create decision trees myself and not retrieve them from a dataset, simply using rpart instead of partykit is not a solution.
Thank you for your suggestions,
Niels
At the moment the partykit package does not provide this feature. The package has many converters from other objects to party objects, including a converter from PMML. However, there are not many options to convert from party objects to other classes. A PMML export would certainly nice to have (at least for the special case of constparty objects) but so far I didn't look at what needs to be done for this.

How to design a flowchart in R?

is there any way to write a flowchart in R markdown to have it as an introduction in the html_output_file?
Yes, there are number of packages which can help do this. DiagrammeR and DiagrammeRsvg are two I have used successfully.
You might like to refer to the fairly simple source for the package PRISMAstatement which implements the standard PRISMA flow chart for systematic reviews using an intermediate dot format.

Using stargazer with panelAR

Is it possible to export regression tables created with the panelAR package using stargazer? When I try I get
% Error: Unrecognized object type.
Creating a data frame first would probably do the trick, but as I am fairly new to R I have no clue how to do it.
The short answer is no, you can't use the stargazer package to create tables when you use panelAR.
This reason for this, as the error message suggest, is that stargazer does not support objects whose class is panelAR. See https://cran.r-project.org/web/packages/stargazer/stargazer.pdf for a comprehensive list of the model supported by stargazer
R is an object oriented language, since you are new to R you might find it advantageous to better understand what is meant by this.
Loosely speaking, object oriented means that everything in R is an object. Each object has metadata attached to it. Arguably the most important piece of metadata is the object's class. To determine an object's class, you can call the class function. Again loosely speaking, when you pass an object to a function, the function will usually first consider what class the object has and then preform the necessary analysis.

Does "gbm" package in R has basis functions other than decision tree?

if not, which packages implement multiple basis functions for boosting methods. Thanks a lot.
It doesn't look like it since you need to specify the number of trees in the gbm call. You can try the 'mboost' package to specify different types of base learners.

Resources