Error in file (con, "r"): cannot open with translateR - r

Im trying to translate a lot of text from Italian to English using translateR in R. My file also contains other languages and I have successfully used translateR to translate these. The Italian represents the biggest amount though with over 4000 rows of short text segments to translate. Any idea what the problem is, or if there is a problematic data entry how to find it? I split the dataframe into half and the second half of the df appeared to run okay for a longer time than the original df but then the error message appeared again.
The error message is
Error in file(con, "r") : cannot open the connection
which doesnt tell me much. A traceback() gave the following:
16: file(con, "r")
15: readLines(content)
14: paste(readLines(content), collapse = "\n")
13: withCallingHandlers(expr, warning = function(w)
invokeRestart("muffleWarning"))
12: suppressWarnings(paste(readLines(content), collapse = "\n"))
11: structure(x, class = unique(c("AsIs", oldClass(x))))
10: I(suppressWarnings(paste(readLines(content), collapse = "\n")))
9: fromJSON(getURL(api.url))
8: fromJSON(getURL(api.url))
7: googleTranslate(x, google.api.key, source.lang, target.lang)
6: FUN(X[[i]], ...)
5: lapply(X, FUN, ...)
4: mclapply(to.translate, function(x) googleTranslate(x, google.api.key,
source.lang, target.lang))
3: unlist(mclapply(to.translate, function(x) googleTranslate(x,
google.api.key, source.lang, target.lang)))
2: unname(unlist(mclapply(to.translate, function(x) googleTranslate(x,
google.api.key, source.lang, target.lang))))
1: translate(content.vec = magg.it.2$text, google.api.key = "foo",
source.lang = "it", target.lang = "en")

Related

Reading multiple excel files from folder in R, Error in file(con, "r") : invalid 'description' argument

I have this code that I run in R regularly to read excel files from T&E folder and put into dataframe, but suddenly it has stopped working. My excel files are not open, also I have updated my R and Rstudios.
setwd("C:\\My Directory")
inputFiles = list.files(path = "T&E/", pattern="*.xlsx")
inputFiles = paste0("T&E/", inputFiles)
loadData = function(sheetName) {
Files = lapply(inputFiles, read.xlsx, sheet = sheetName)
data = data.frame(stringsAsFactors = F)
for(i in 1:length(Files)) {
data = rbind(data, Files[[i]])
}
return(data)
}
d1 = loadData('Air')
This is the error that comes up after i call the function
Error in file(con, "r") : invalid 'description' argument
In addition: Warning message:
In unzip(xlsxFile, exdir = xmlDir) : error 1 in extracting from zip file
I also did traceback()
12: file(con, "r")
11: readLines(x, warn = FALSE, encoding = "UTF-8")
10: readUTF8(workbookRelsXML)
9: paste(readUTF8(workbookRelsXML), collapse = "")
8: read.xlsx.default(X[[i]], ...)
7: FUN(X[[i]], ...)
6: lapply(inputFiles, read.xlsx, sheet = sheetName) at filename.r#57
5: loadData("Air") at filename.r#67
4: eval(ei, envir)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("C:/Users/myname/Downloads/filename.r", echo = TRUE)

How to read IMF xls- or sdmx-data from url?

From the IMF I want to read a .xls file from an URL directly into R, but all attempts fail so far. Weirdly, I can download the file manually or by download.file() and open it without problems in Microsoft Outlook or in a text editor. However, even then I can't read the data into R.
I always try with both https and http.
myUrl <- "https://www.imf.org/external/pubs/ft/weo/2019/02/weodata/WEOOct2019all.xls"
myUrl2 <- "http://www.imf.org/external/pubs/ft/weo/2019/02/weodata/WEOOct2019all.xls"
1. Classic approach – fails.
imf <- read.table(file=myUrl, sep="\t", header=TRUE)
# Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
# line 51 did not have 55 elements
imf <- read.table(file=url(myUrl), sep="\t", header=TRUE)
# Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
# line 51 did not have 55 elements
2. Several packages – fails.
imf <- readxl::read_xls(myUrl)
# Error: `path` does not exist: ‘https://www.imf.org/external/pubs/ft/weo/2019/02/weodata/WEOOct2019all.xls’
imf <- readxl::read_xls(myUrl2)
# Error: `path` does not exist: ‘http://www.imf.org/external/pubs/ft/weo/2019/02/weodata/WEOOct2019all.xls’
imf <- gdata::read.xls(myUrl)
# Error in xls2sep(xls, sheet, verbose = verbose, ..., method = method, :
# Intermediate file 'C:\Users\jay\AppData\Local\Temp\RtmpUtW45x\file16f873be18e0.csv' missing!
# In addition: Warning message:
# In system(cmd, intern = !verbose) :
# running command '"C:\STRAWB~1\perl\bin\perl.exe"
# "C:/Program Files/R/R-3.6.1rc/library/gdata/perl/xls2csv.pl"
# "https://www.imf.org/external/pubs/ft/weo/2019/02/weodata/WEOOct2019all.xls"
# "C:\Users\jay\AppData\Local\Temp\RtmpUtW45x\file16f873be18e0.csv" "1"' had status 2
# Error in file.exists(tfn) : invalid 'file' argument
imf <- gdata::read.xls(myUrl2) # <---------------------------------------------- THIS DOWNLOADS SOMETHING AT LEAST!
# trying URL 'http://www.imf.org/external/pubs/ft/weo/2019/02/weodata/WEOOct2019all.xls'
# Content type 'application/vnd.ms-excel' length unknown
# downloaded 8.9 MB
#
# Error in xls2sep(xls, sheet, verbose = verbose, ..., method = method, :
# Intermediate file 'C:\Users\jay\AppData\Local\Temp\RtmpUtW45x\file16f87ded406b.csv' missing!
# In addition: Warning message:
# In system(cmd, intern = !verbose) :
# running command '"C:\STRAWB~1\perl\bin\perl.exe"
# "C:/Program Files/R/R-3.6.1rc/library/gdata/perl/xls2csv.pl"
# "C:\Users\jay\AppData\Local\Temp\RtmpUtW45x\file16f87f532cb3.xls"
# "C:\Users\jay\AppData\Local\Temp\RtmpUtW45x\file16f87ded406b.csv" "1"' had status 255
# Error in file.exists(tfn) : invalid 'file' argument
3. Tempfile approach – fails.
temp <- tempfile()
download.file(myUrl, temp) # THIS WORKS...
## BUT...
imf <- gdata::read.xls(temp)
# Error in xls2sep(xls, sheet, verbose = verbose, ..., method = method, :
# Intermediate file 'C:\Users\jay\AppData\Local\Temp\RtmpUtW45x\file16f870f55e04.csv' missing!
# In addition: Warning message:
# In system(cmd, intern = !verbose) :
# running command '"C:\STRAWB~1\perl\bin\perl.exe"
# "C:/Program Files/R/R-3.6.1rc/library/gdata/perl/xls2csv.pl"
# "C:\Users\jay\AppData\Local\Temp\RtmpUtW45x\file16f8746a46db"
# "C:\Users\jay\AppData\Local\Temp\RtmpUtW45x\file16f870f55e04.csv" "1"' had status 255
# Error in file.exists(tfn) : invalid 'file' argument
# even not...
tmp1 <- readLines(temp)
# Warning message:
# In readLines(temp) :
# incomplete final line found on
# 'C:\Users\jay\AppData\Local\Temp\Rtmp00GPlq\file2334435c2905'
str(tmp1)
# chr [1:8733] "WEO Country Code\tISO\tWEO Subject Code\tCountry\tSubject
# Descriptor\tSubject Notes\tUnits\tScale\tCountry/Seri"| __truncated__ ...
4. SDMX
I also tried the SDMX the IMF offer, but also without success. Probably this would be a more sophisticated approach, but I never used SDMX.
link <- "https://www.imf.org/external/pubs/ft/weo/2019/02/weodata/WEOOct2019_SDMXData.zip"
temp <- tempfile()
download.file(link, temp, quiet=TRUE)
imf <- rsdmx::readSDMX(temp)
# Error in function (type, msg, asError = TRUE) :
# Could not resolve host: C
# imf <- rsdmx::readSDMX(unzip(temp)) # runs forever and crashes R
unlink(temp)
Now... does anybody know what's going on, and how I may load the data into R?
Why not just use fill=TRUE?
imf <- read.table(file=myUrl, sep="\t", header=TRUE, fill = TRUE)
from ?read.table
fill
logical. If TRUE then in case the rows have unequal length, blank fields are implicitly added. See ‘Details’.

R: annotate() gives error in 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(...)

Roxygenize fails with... is.call(call) is not TRUE

Every time I try to roxygenize a package I get this error:
Error: is.call(call) is not TRUE
The results of a traceback():
11: stop(sprintf(ngettext(length(r), "%s is not TRUE", "%s are not all TRUE"),
ch), call. = FALSE, domain = NA)
10: stopifnot(is.call(call))
9: standardise_call(call, env)
8: object_from_call(call, env, preref)
7: (function (call, ref, comment_ref)
{
preref <- parse.preref(as.character(comment_ref))
if (is.null(preref))
return()
preref$object <- object_from_call(call, env, preref)
preref$srcref <- list(filename = file, lloc = as.vector(ref))
add_defaults(preref)
})(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]])
6: mapply(FUN = f, ..., SIMPLIFY = FALSE)
5: Map(extract, parsed, refs, comment_refs)
4: FUN(c("/home/path/to/package/file1.r",
"/home/path/to/package/file2.r",
.... # list of files truncated
"/home/path/to/package/doc.file.r")[[25L]],
...)
3: lapply(r_files(base_path), parse_file, env = env)
2: parse_package(base_path, load_code)
1: roxygenize("~/Current/r/path/to/package/")
Has anyone experienced this problem before? I'm not even sure how to debug it further.
This error occurs if, instead of the typical NULL value that one puts at the end of the documentation for the package, one uses NA. Simply updating to NULL will remove the error.

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
})()

Resources