R: annotate() gives error in R - r

I am new to R. I have to use POSTagger in my code. I am using openNLP with R. While trying following sample code (in Sample.R file):
library("NLP")
library("openNLP")
s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
"nonexecutive director Nov. 29.\n",
"Mr. Vinken is chairman of Elsevier N.V., ",
"the Dutch publishing group."),
collapse = "")
s <- as.String(s)
sent_token_annotator <- Maxent_Sent_Token_Annotator()
a1 <- annotate(s, sent_token_annotator)
s[a1]
And running this code from R Console (Using source("Sample.R"))
I am getting following error:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class "c("Simple_POS_Tag_Annotator", "Annotator")" to a data.frame
Following is the output of traceback() command :
14: stop(gettextf("cannot coerce class \"%s\" to a data.frame", deparse(class(x))),
domain = NA)
13: as.data.frame.default(x[[i]], optional = TRUE)
12: as.data.frame(x[[i]], optional = TRUE)
11: data.frame(x = function (s, a = Annotation())
{
s <- as.String(s)
y <- f(s)
n <- length(y)
id <- .seq_id(next_id(a$id), n)
type <- rep.int("sentence", n)
if (is.Annotation(y)) {
y$id <- id
y$type <- type
}
else if (is.Span(y)) {
y <- as.Annotation(y, id = id, type = type)
}
else stop("Invalid result from underlying sentence tokenizer.")
if (length(i <- which(a$type == "paragraph"))) {
a <- a[i]
a$features <- lapply(annotations_in_spans(y, a), function(e) list(constituents = e$id))
y <- c(y, a)
}
y
}, check.names = FALSE, stringsAsFactors = FALSE)
10: eval(expr, envir, enclos)
9: eval(as.call(c(expression(data.frame), x, check.names = !optional,
stringsAsFactors = stringsAsFactors)))
8: as.data.frame.list(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors)
7: as.data.frame(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors)
6: data.frame(position)
5: annotate(s, sent_token_annotator) at sample.R#11
4: eval(expr, envir, enclos)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("sample.R")
What can be possibly wrong? I am using Rx64 3.1.1 on Windows 7. Any help will be much appreciated. Thanks in advance.

I have the same problem and i fixed it by removing/detach the ggplot2 package. There is a function called Annotate in ggplot2 and it is the same name in both packages. I suggest you make sure that it is looking at the correct function in the library... in my case it was looking at the Annotate function of ggplot2 and not the NLP package.

I don't have an exact answer but suffered the same error using NLP, openNLP, tm, qdap. I worked backward restarting R and loading (library) one package, running code, then loading another package and running code, until I ran across the "cannot coerce to a dataframe" error. I found, in my case that qdap interferes with the openNLP annotate() function call -- which is actually using an NLP wrapper.
openNLP version 0.2-3 imports NLP (≥ 0.1-2), openNLPdata (≥ 1.5.3-1), and rJava (≥ 0.6-3). Because you loaded NLP explicitly, it may be a case of two instances of NLP running in memory interfering with each other. Try just loading openNLP and running your code

Multiple packages have same name. If you specifically tell R which package to use it will probably resolve the issue. For example, instead of Arrange(...), try using openNLP::Arrange(...)

Related

I get Error: invalid version specification ‘0,2’ when I use the function dm_draw() in r

I want to use the function dm_draw() to visualize a dm object but when I ran the command I get the error message " Error: invalid version specification ‘0,2’". I've tried the code included in the vignette "Visualizing dm objects" (https://cran.r-project.org/web/packages/dm/vignettes/tech-dm-draw.html) and I get the same error message when I run the dm_draw() function.
library(dm)
library(dplyr)
flights_dm_w_many_keys <- dm_nycflights13(color = FALSE)
dm_draw(flights_dm_w_many_keys)
I'm using dm version 0.2.7 and DiagrammeR 1.0.8. R version 4.1.2
I'm looking for a solution to visualize a dm object, it can be also different from dm_draw().
I hope someone can help me to get that done. Sorry for my broken English and thanks for your time, any type of help is appreciated.
You can use this code:
library(dm)
library(dplyr)
library(DiagrammeR)
library(DiagrammeRsvg)
# Use this function
dm_draw_svg = function(dm,...) {
if (!requireNamespace("DiagrammeRsvg", quietly = TRUE)) {
stop(
"Package \"DiagrammeRsvg\" must be installed to use this function.",
call. = FALSE
)
}
dm::dm_draw(dm = dm, ...) %>%
DiagrammeRsvg::export_svg() %>%
htmltools::HTML() %>%
htmltools::html_print()
}
flights_dm_w_many_keys <- dm_nycflights13(color = FALSE)
# plot
dm_draw_svg(flights_dm_w_many_keys)
Output:

R - Parallel Processing and ldply error

I am trying to use the below code to make API calls in a parallel process to speed up the API calls. (I know this isn't the best way to speed up API calls but it works)
It only fails when I try to use parallel, otherwise it works. In the ldply function I am getting the below error:
Error in do.ply(i) :
task 1 failed - "object of type 'closure' is not subsettable"
In addition:
Warning messages:
1: : ... may be used in an incorrect context: ‘.fun(piece, ...)’
2: : ... may be used in an incorrect context: ‘.fun(piece, ...)’
any help would be appreciated!
One <- 26
cl<-makeCluster(4)
registerDoSNOW(cl)
func.time <- Sys.time()
## API CALL ONE FOR "kline"
url <- "https://api.binance.com"
path <- paste("/api/v1/klines?symbol=",pairs[1],"&interval=1m&limit=1", sep = "")
raw.results <- GET(url = url, path = path)
text_content <- content(raw.results, as = "text", encoding = "UTF-8")
kline <- data.frame(text_content %>% fromJSON())
kline$symbol <- pairs[1]
## API FUNCTION TO BE APPLIED FOR REST
loopfunction <- function(i){
url <- "https://api.binance.com"
path <- paste("/api/v1/klines?symbol=",pairs[i],"&interval=1m&limit=1", sep = "")
raw.results <- GET(url = url, path = path)
text_content <- content(raw.results, as = "text", encoding = "UTF-8")
kline_temp <- data.frame(text_content %>% fromJSON())
kline_temp$symbol <- pairs[i]
kline <- rbind(kline,kline_temp)
return(kline)
}
## DPLY PARALLEL FUNCTION
kline2 <- data.frame(ldply(2:(One - 1), .fun = loopfunction, .parallel = T, .paropts = c("httr", "jsonlite", "dplyr"))) ##"ONE" is a list varriable created earlier
stopCluster(cl)
func.end.time <- Sys.time()
func.tot.time <- func.end.time - func.time
Your question isn't fully reproducible, so the following is an educated guess.
Your loopfunction() references an object called pairs. It seems from your script that a variable called pairs is defined somewhere in your local environment. However, when loopfunction() is passed to ldply(), it no longer has access to that variable (ordinarily, it would, but parallelization requires fresh R environments to be created). Having failed to find an object called pairs in the environment, R continues searching, and finds a match in stats::pairs(). This is a plotting function, not a subsettable object like a vector or data frame. Hence the error message, "object of type 'closure' is not subsettable".
I'm not especially familiar with how ldply implements parallel processing, but you could probably modify your function definition like this:
loopfunction <- function(i, pairs) {
...[body of function]...
}
And pass pairs as an extra parameter in your ldply call:
kline2 <- data.frame(ldply(2:(One - 1), .fun = loopfunction, pairs = pairs, .parallel = T, .paropts = list(.packages = c("httr", "jsonlite", "dplyr"))))

Issue when loading LDA function in R

I'm using the text-mining tm library for R.
I'm running on R version 3.3.1
I have this code:
lda <- LDA(docterm,k = 3,method = 'Gibbs')
lda.topics <- as.matrix(topics(lda))
lda.terms <- as.matrix(terms(lda,5))
topic.terms <- c()
topic.terms[1] <- paste(c(lda.terms[,1],'\n'),collapse = '\n')
topic.terms[2] <- paste(c(lda.terms[,2],'\n'),collapse = '\n')
topic.terms[3] <- paste(c(lda.terms[,3],'\n'),collapse = '\n')
tw.df <- tw.df %>%
mutate(topico = topic.terms[lda.topics])
But every time I try to run it with source file.r
It throws me this:
Error in eval(expr, envir, enclos) : could not find function "LDA"
I don't get it, the tm package is installed.
Has anybody encountered this kind of behaviour before?
Any ideas on how to solve it?
Thanks in advance!
Try installing and using the package 'topicmodels'.
install.packages('topicmodels')
It should work.

gWidgets + tcltk - creating a simple window returns a error

I'm trying to make a small GUI to make it easier for other people to run a script.
I'm using gWidgets with tcltk on a Windows machine.
I create a simple window like this:
require(gWidgets)
require(gWidgetstcltk)
options(guiToolkit="tcltk")
win <- gwindow(title="This is a window!")
grp <- ggroup(container=win)
lbl <- glabel("Here you can write stuff:", container=grp)
txt <- gedit(text="Stuff", container=grp)
When I run it on a new session i get the error message:
Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :
‘no_items’ is not a valid field or method name for reference class “Entry”
If i rerun after the error i get this:
<simpleError in envRefInferField(x, what, getClass(class(x)), selfEnv): ‘no_items’ is
not a valid field or method name for reference class “Entry”>
Anyone can explain what is going on?
EDIT:
The problem seems to only show up on RStudio and not on RGui.exe.
I'm not such an expert programmer, but I guess it is somehow related with the way RStudio manages the environments.
I guess the question now is more: How do i make this work normally in RStudio?
Traceback:
> traceback()
11: stop(gettextf("%s is not a valid field or method name for reference class %s",
sQuote(field), dQuote(thisClass#className)), domain = NA)
10: envRefInferField(x, what, getClass(class(x)), selfEnv)
9: r5_widget$no_items
8: r5_widget$no_items
7: .length(x#widget, x#toolkit)
6: .length(x#widget, x#toolkit)
5: FUN(X[[3L]], ...)
4: FUN(X[[3L]], ...)
3: lapply(X = X, FUN = FUN, ...)
2: sapply(globalValues, length, USE.NAMES = FALSE) at SessionWorkspace.R#166
1: (function ()
{
globals = ls(envir = globalenv())
globalValues = lapply(globals, function(name) {
get(name, envir = globalenv(), inherits = FALSE)
})
types = sapply(globalValues, .rs.getSingleClass, USE.NAMES = FALSE)
lengths = sapply(globalValues, length, USE.NAMES = FALSE)
values = sapply(globalValues, .rs.valueAsString, USE.NAMES = FALSE)
extra = sapply(globalValues, .rs.valueDescription, USE.NAMES = FALSE)
result = list(name = globals, type = types, len = lengths,
value = values, extra = extra)
result
})()

Why can't I vectorize source_url in knitr?

I am trying to vectorize this call to source_url, in order to load some functions from GitHub:
library(devtools)
# Find ggnet functions.
fun = c("ggnet.R", "functions.R")
fun = paste0("https://raw.github.com/briatte/ggnet/master/", fun)
# Load ggnet functions.
source_url(fun[1], prompt = FALSE)
source_url(fun[2], prompt = FALSE)
The last two lines should be able to work in a lapply call, but for some reason, this won't work from knitr: to have this code work when I process a Rmd document to HTML, I have to call source_url twice.
The same error shows up with source_url from devtools and with the one from downloader: somehwere in my code, an object of type closure is not subsettable.
I suspect that this has something to do with SHA; any explanation would be most welcome.
It has nothing to do with knitr or devtools or vectorization. It is just an error in your(?) code, and it is fairly easy to find it out using traceback().
> library(devtools)
> # Find ggnet functions.
> fun = c("ggnet.R", "functions.R")
> fun = paste0("https://raw.github.com/briatte/ggnet/master/", fun)
> # Load ggnet functions.
> source_url(fun[1], prompt = FALSE)
SHA-1 hash of file is 2c731cbdf4a670170fb5298f7870c93677e95c7b
> source_url(fun[2], prompt = FALSE)
SHA-1 hash of file is d7d466413f9ddddc1d71982dada34e291454efcb
Error in df$Source : object of type 'closure' is not subsettable
> traceback()
7: which(df$Source == x) at file34af6f0b0be5#14
6: who.is.followed.by(df, "JacquesBompard") at file34af6f0b0be5#19
5: eval(expr, envir, enclos)
4: eval(ei, envir)
3: withVisible(eval(ei, envir))
2: source(temp_file, ...)
1: source_url(fun[2], prompt = FALSE)
You used df in the code, and df is a function in the stats package (density of the F distribution). I know you probably mean a data frame, but you did not declare that in the code.

Resources