Error in R with highcharter library - r

I have a problem with this code , what is wrong with this code ?
library(highcharter)
hchart(mtcars,x=mpg,y=disp)
and the results is this :
Error: !is.null(type) is not TRUE

The interface for this package doesn't seem very intuitive (and that error needs better handling) but try:
hchart(mtcars, "scatter", x=mpg, y=disp)
The highcharter docs for hchart show examples with code that might be useful.

Related

How to debug a "hidden" function in an R package?

can someone please help me understand this:
I encountered an error when calling a function from a library, specifically "steinertree" from the "SteinerNet" package. When stepping into the function with debug(steinertree), I see that the error occurs, when the function in turn calls "steinertree3". When I try debug(steinertree3), I get "object 'steinertree3' not found". Similarly, I can get the code for 'steinertree' by typing it in the terminal, but not for 'steinertree3'.
So it seems to me that there are some "higher-level" functions and "hidden" functions in packages. I did eventually find the error by finding a file "steinertree.R" in the package at CRAN, which contains both 'steinertree' and 'steinertree3', but I`m wondering how to properly go about debugging such "hidden" functions.
Here is a simple example:
library(igraph)
library(SteinerNet)
set.seed(1)
g= erdos.renyi.game(n=10,p.or.m=0.2)
plot(g)
steinertree(type= 'KB', terminals= c(1,3), graph= g)
Thank you!
Use triple colon ::: to execute a function that is not exported by the package/namespace:
package:::hidden_function()

Unable to draw graphs with plot_ly function

Today I've been asked to draw some charts using plotly package for a Shiny application but i can't figure out how to solve my problem, so i've decided to ask for some help !
Everytime I try to use the function "plot_ly" I get the same error :
Error in arrange_impl(.data, dots) :
Evaluation error: as_dictionary() is defunct as of rlang 0.3.0.
Please use as_data_pronoun() instead.
I tried to remake the example from the official website of plotly and the result stay the same !
For instance, when i try the following code I get the message above :
library(plotly)
USPersonalExpenditure <-
data.frame("Categorie"=rownames(USPersonalExpenditure),
USPersonalExpenditure)
data <- USPersonalExpenditure[,c('Categorie', 'X1960')]
p <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie')
I also re-installed the package, updated every package i had installed but their is nothing to do i'm still stucked with that error.
I didn't found anything revelant on google neither.
Thank you in advance and happy new year !
I solved my issue ! I didn't had the package dplyr installed actually...
The answer that helped me was given by cderv (https://community.rstudio.com/t/plotly-unable-to-draw-graphs/20639) :
This seems like a compatibility issue between :package: version. rlang version seems to be newer than your installed dplyr can deal with

In R, how do I save a data.tree plot to a file?

I'm unable to save a plot generated by the plot.Node function in data.tree. I've tried the following:
### Create tree object and plot it
data(acme);
plot(acme);
This works fine, showing the plot, as one would expect.
### Try saving it as png
png(filename='file.png', type='cairo-png');
plot(acme);
dev.off();
This creates an empty file. ggsave does the same. Apparantly, plot.Node uses DiagrammeR under the hood, so I looked into that package. It has a function to export graphs:
export_graph(acme, file_name="file.png");
This gives the error:
Error in file.exists(diagram) : invalid 'file' argument
When I transform to GraphViz first, I get a different error:
export_graph(ToGraphViz(acme), file_name="file.png");
Error in graph$dot_code : $ operator is invalid for atomic vectors
Clearly, exporting to GraphViz doesn't quite export to what DiagrammeR expects.
I'm in RStudio and so could in theory just save the plot using the GUI, but I need this for in a script.
Apparently, plot.Node doesn't actually plot anything - instead it seems to generate html/js. Does this mean that that result cannot be stored as a graphic? Or is there some export/conversion function somewhere that I'm completely missing? it certainly feels like I'm missing something obvious - I assume the need to store plotted data.trees as images is quite common. But I have no idea which potential solutions I can still explore.
I'd be very grateful for any pointers anybody has!
As sebastian-c suggested, things work now a bit differently than suggested by Matherion, as of R 3.3.3 with data.tree 0.7.0 and DiagrammeR 0.9.0
Pre-requisite: DiagrmmeRsvg and dependencies need to be installed. Depending on your OS, for this to work you might have to install V8. For example on ubuntu:
apt-get install libv8-3.14-delibv8-3.14-dev
And then in R:
install.packages("DiagrammeRsvg")
On Windows, I didn't have to install anything (maybe because Chrome is installed?).
Once DiagrammeRsvg is available, run:
library(data.tree)
data(acme)
library(DiagrammeR)
export_graph(ToDiagrammeRGraph(acme), "export.pdf")
I've found the answer, at least, partially. There exists a package dedicated to exporting GraphViz diagrams to SVG: DiagrammeRsvg.
So this works:
treeAsSVG <- export_svg(grViz(ToGraphViz(acme)));
writeLines(treeAsSVG, "filename.svg"));
The grViz is necessary to actually convert the ToGRaphViz output to something that can be interpreted by export_svg. I'm still not really sure (yet) what all goes on under the hood - for example, this does not work:
export_graph(grViz(ToGraphViz(acme)), file_name="filename.svg");
But, if anybody else has a similar problem and stumbles upon this question, perhaps this partial answer can help them to at least export something that can then be integrated in e.g. html pages.
By converting acme with as.phylo() it works, but it looks a little boring:
plot(as.phylo(acme),
show.node.label=TRUE,
node.pos=2,
no.margin=TRUE
)
# adding edge labels
edgelabels(as.vector(acme$Get("cost"))[-1],
adj = c(0,-0.5),
frame = "none")
solution with as.phylo()
I also tried as.igraph. However, the nodes overlap and it looks even less pretty:
plot(0, type="n", ann=FALSE, axes=FALSE,
xlim=extendrange(ig[,1]),
ylim=extendrange(ig[,2]))
plot(ig,
layout=layout_as_tree(ig,root=1),
vertex.shape="rectangle",
vertex.size=(strwidth(V(ig)$name) + strwidth("oo")) * 100,
#vertex.size2=strheight("I") * 2 * 100,
edge.label=acme$Get("p",traversal = "level")[-1]
)
solution with as.igraph()

Color error in ggplot2 (Error in grDevices::col2rgb(colour, TRUE) : invalid RGB specification)

If I run:
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
bp<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
bp
I see the error: Error in grDevices::col2rgb(colour, TRUE) : invalid RGB specification.
If I run the identical code on my other computer, I get the expected plot. I am guessing that some plotting parameter is the culprit, but I have no idea how to find it. What's going wrong?
The computer that behaves strangely is Mac OS X 10_11_3 running R 3.2.2 via RStudio 0.99.489
Sorry I am late, but the problem is that the function alpha is masked form ggplot2, that's why if you restart R and happen to call alpha after sourcing psych but before ggplot2 it works.
Anyway, the way to solve it is to make explicit that we want the psych function:
reliability = psych::alpha(df)
Restarting R was sufficient to fix the weird behavior. Obviously should have tried that before posting...

Julia Gadfly error: add_plot_element not found

Using Julia 0.3.10 with Juno as IDE and Gadfly latest one. Tried running a sample code, but got stuck with add_plot_element not defined error message. The last line throws the error. Running on Win8 64bit. I'm sure I'm missing something.
using Gadfly
xs = [0:0.1:pi]
k = layer(x=xs, y=sin(xs))
p = plot(x=xs, y=sin(xs))
add_plot_element(k, Guide.title("Now it has a title"))
First, add_plot_element is modifying, so you need the ! like:
add_plot_element!(k,Guide.title(...))
This function is also not exported from Gadfly, so you would really need to write:
Gadfly.add_plot_element!(k, Guide.title("Now it has a title"))
except add_plot_element! doesn't work on Gadfly layers! It does, however, work on plots. What should work:
Gadfly.add_plot_element!(p, Guide.title("Now it has a title"))
since the layer itself doesn't have Guide.Title elements, but the plot does.

Resources