I wrote 2 functions to archive objects from R (with their names - that's really important for me) and, if an object is of class lm, to archive data used to conduct this model.
My code is below :
archive_object_and_data <- function(object, archiveData = TRUE, rememberName = TRUE){
object <- deparse(substitute(object))
md5hash <- digest(object)
dir <- paste0(getwd(), "/")
dir.create(file.path(dir, md5hash), showWarnings = FALSE)
if (rememberName){
save( file = paste0(dir, md5hash, "/obj.rda"), ascii = TRUE, list = objectName,
envir = parent.frame(2))
}else{
save( file = paste0(dir, md5hash, "/obj2.rda"), ascii = TRUE, list =objectName,
envir = parent.frame(5))}
if ( archiveData )
archiveDataFromObj( object, md5hash, changeBool = FALSE )
}
#
archiveDataFromObj <- function (object, md5hash, changeBool = TRUE)
UseMethod("archiveDataFromObj")
archiveDataFromObj.default <- function(object, md5hash, changeBool = TRUE) {
}
archiveDataFromObj.lm <- function(object, md5hash, changeBool = TRUE) {
extractedDF <- object$model
md5hashDF <- archive_object_and_data(extractedDF, rememberName = changeBool)
}
And the object I want to archive looks like this :
data( iris )
test_data <- iris[,-5]
model2 <- lm(Sepal.Length~. , data=test_data)
archive_object_and_data(model2)
And error appears like this :
Error in save(file = paste0(dir, md5hash, "/obj2.rda"), ascii = TRUE, :
object ‘extractedDF’ not found
6 stop(sprintf(ngettext(n, "object %s not found", "objects %s not found"),
paste(sQuote(list[!ok]), collapse = ", ")), domain = NA)
5 save(file = paste0(dir, md5hash, "/obj2.rda"), ascii = TRUE,
list = objectName, envir = parent.frame(5))
4 archive_object_and_data(extractedDF, rememberName = changeBool) at archiveDataFromObj.R
3 archiveDataFromObj.lm(object, md5hash, changeBool = FALSE) at archiveDataFromObj.R#1
2 archiveDataFromObj(object, md5hash, changeBool = FALSE) at archive_object_and_data.R
1 archive_object_and_data(model2)
Can any1 help me with this problem?
It looks like there's a problem with that line :(
}else{
save( file = paste0(dir, md5hash, "/obj2.rda"), ascii = TRUE, list =objectName,
envir = parent.frame(5))}
Related
A few days ago I wanted to install a certain package in R that wasn't available in the older version of R. I installed new version of R and R studio and now there is a problem. I can't install any packages, whenever I try, I get the following error:
Error in install.packages : STRING_ELT() can only be applied to a 'character vector', not a 'NULL'
Error in list.files(skeletonPath) :
STRING_ELT() can only be applied to a 'character vector', not a 'NULL'
Error in list.files(skeletonPath) :
STRING_ELT() can only be applied to a 'character vector', not a 'NULL'
Error in list.files(.rs.uniqueLibraryPaths(), full.names = TRUE) :
STRING_ELT() can only be applied to a 'character vector', not a 'NULL'
I also get the same error when I open R studio for the first time (without the first line of the code above). When I try to install packages manually by going to Tools, I get "R code execution error" message.
I'm not sure what's wrong, I don't understand that error.
Edit: After typing trace(utils:::unpackPkgZip, edit=TRUE) new window with the code opens:
function (pkg, pkgname, lib, libs_only = FALSE, lock = FALSE,
quiet = FALSE)
{
.zip.unpack <- function(zipname, dest) {
if (file.exists(zipname)) {
if ((unzip <- getOption("unzip")) != "internal") {
system(paste(shQuote(unzip), "-oq", zipname,
"-d", dest), show.output.on.console = FALSE,
invisible = TRUE)
}
else unzip(zipname, exdir = dest)
}
else stop(gettextf("zip file %s not found", sQuote(zipname)),
domain = NA)
}
lib <- normalizePath(lib, mustWork = TRUE)
tmpDir <- tempfile(, lib)
if (!dir.create(tmpDir))
stop(gettextf("unable to create temporary directory %s",
sQuote(normalizePath(tmpDir, mustWork = FALSE))),
domain = NA, call. = FALSE)
cDir <- getwd()
on.exit(setwd(cDir))
on.exit(unlink(tmpDir, recursive = TRUE), add = TRUE)
res <- .zip.unpack(pkg, tmpDir)
setwd(tmpDir)
res <- tools::checkMD5sums(pkgname, file.path(tmpDir, pkgname))
if (!quiet && !is.na(res) && res) {
cat(gettextf("package %s successfully unpacked and MD5 sums checked\n",
sQuote(pkgname)))
flush.console()
}
desc <- read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package",
"Type"))
if (desc[1L, "Type"] %in% "Translation") {
fp <- file.path(pkgname, "share", "locale")
if (file.exists(fp)) {
langs <- dir(fp)
for (lang in langs) {
path0 <- file.path(fp, lang, "LC_MESSAGES")
mos <- dir(path0, full.names = TRUE)
path <- file.path(R.home("share"), "locale",
lang, "LC_MESSAGES")
if (!file.exists(path))
if (!dir.create(path, FALSE, TRUE))
warning(gettextf("failed to create %s",
sQuote(path)), domain = NA)
res <- file.copy(mos, path, overwrite = TRUE)
if (any(!res))
warning(gettextf("failed to create %s", paste(sQuote(mos[!res]),
collapse = ",")), domain = NA)
}
}
fp <- file.path(pkgname, "library")
if (file.exists(fp)) {
spkgs <- dir(fp)
for (spkg in spkgs) {
langs <- dir(file.path(fp, spkg, "po"))
for (lang in langs) {
path0 <- file.path(fp, spkg, "po", lang, "LC_MESSAGES")
mos <- dir(path0, full.names = TRUE)
path <- file.path(R.home(), "library", spkg,
"po", lang, "LC_MESSAGES")
if (!file.exists(path))
if (!dir.create(path, FALSE, TRUE))
warning(gettextf("failed to create %s",
sQuote(path)), domain = NA)
res <- file.copy(mos, path, overwrite = TRUE)
if (any(!res))
warning(gettextf("failed to create %s",
paste(sQuote(mos[!res]), collapse = ",")),
domain = NA)
}
}
}
}
else {
instPath <- file.path(lib, pkgname)
if (identical(lock, "pkglock") || isTRUE(lock)) {
lockdir <- if (identical(lock, "pkglock"))
file.path(lib, paste0("00LOCK-", pkgname))
else file.path(lib, "00LOCK")
if (file.exists(lockdir)) {
stop(gettextf("ERROR: failed to lock directory %s for modifying\nTry removing %s",
sQuote(lib), sQuote(lockdir)), domain = NA)
}
dir.create(lockdir, recursive = TRUE)
if (!dir.exists(lockdir))
stop(gettextf("ERROR: failed to create lock directory %s",
sQuote(lockdir)), domain = NA)
if (file.exists(instPath)) {
file.copy(instPath, lockdir, recursive = TRUE)
on.exit({
if (restorePrevious) {
try(unlink(instPath, recursive = TRUE))
savedcopy <- file.path(lockdir, pkgname)
file.copy(savedcopy, lib, recursive = TRUE)
warning(gettextf("restored %s", sQuote(pkgname)),
domain = NA, call. = FALSE, immediate. = TRUE)
}
}, add = TRUE)
restorePrevious <- FALSE
}
on.exit(unlink(lockdir, recursive = TRUE), add = TRUE)
}
if (libs_only) {
if (!file_test("-d", file.path(instPath, "libs")))
warning(gettextf("there is no 'libs' directory in package %s",
sQuote(pkgname)), domain = NA, call. = FALSE,
immediate. = TRUE)
for (sub in c("i386", "x64")) if (file_test("-d",
file.path(tmpDir, pkgname, "libs", sub))) {
unlink(file.path(instPath, "libs", sub), recursive = TRUE)
ret <- file.copy(file.path(tmpDir, pkgname,
"libs", sub), file.path(instPath, "libs"),
recursive = TRUE)
if (any(!ret)) {
warning(gettextf("unable to move temporary installation %s to %s",
sQuote(normalizePath(file.path(tmpDir, pkgname,
"libs", sub), mustWork = FALSE)), sQuote(normalizePath(file.path(instPath,
"libs"), mustWork = FALSE))), domain = NA,
call. = FALSE, immediate. = TRUE)
restorePrevious <- TRUE
}
}
fi <- file.info(Sys.glob(file.path(instPath, "libs",
"*")))
dirs <- row.names(fi[fi$isdir %in% TRUE])
if (length(dirs)) {
descfile <- file.path(instPath, "DESCRIPTION")
olddesc <- readLines(descfile)
olddesc <- grep("^Archs:", olddesc, invert = TRUE,
value = TRUE, useBytes = TRUE)
newdesc <- c(olddesc, paste("Archs:", paste(basename(dirs),
collapse = ", ")))
writeLines(newdesc, descfile, useBytes = TRUE)
}
}
else {
ret <- unlink(instPath, recursive = TRUE, force = TRUE)
if (ret == 0) {
Sys.sleep(0.5)
ret <- file.rename(file.path(tmpDir, pkgname),
instPath)
if (!ret) {
warning(gettextf("unable to move temporary installation %s to %s",
sQuote(normalizePath(file.path(tmpDir, pkgname),
mustWork = FALSE)), sQuote(normalizePath(instPath,
mustWork = FALSE))), domain = NA, call. = FALSE,
immediate. = TRUE)
restorePrevious <- TRUE
}
}
else {
warning(gettextf("cannot remove prior installation of package %s",
sQuote(pkgname)), domain = NA, call. = FALSE,
immediate. = TRUE)
restorePrevious <- TRUE
}
}
}
}
Edit 2: Completely deleting all R versions from computer except for the newest version solved the problem.
In RStudio you go to Session and do the following things:
Clear Workspace
Terminate R
Restart R
Then close the program and restarted it. Some how that fix the issue.
For the error: "Error listing packages: R code execution error" try the following things:
Try restarting R using .rs.restartR()
Try removing the packages using remove.packages()
I am trying to modify an existing function by copy and pasting it to an R script, and assigning it to a new function object in my local environment. However the new function cannot find functions that are called to within the original function. How can I fix this without looking up and finding each function individually? I am guessing that the original function is somehow linked to the package or its dependencies and 'knows where to look' for the missing function, but I cannot figure out how to do this with my new copy-and-pasted function.
library("camtrapR")
Print the function name
activityDensity
The output here is the code for this function. I have omitted it here because it is long (and I have pasted it below), but I copy and paste the output of the function code exactly (see below where I assign this exact code to a new function), except for the last two lines of output, which I think are important:
<bytecode: 0x000000002a2d1e20>
<environment: namespace:camtrapR>
So now I assign the copy and pasted code from the output above to a new function with New <-
New <- function (recordTable, species, allSpecies = FALSE, speciesCol = "Species",
recordDateTimeCol = "DateTimeOriginal", recordDateTimeFormat = "%Y-%m-%d %H:%M:%S",
plotR = TRUE, writePNG = FALSE, plotDirectory, createDir = FALSE,
pngMaxPix = 1000, add.rug = TRUE, ...)
{
wd0 <- getwd()
mar0 <- par()$mar
on.exit(setwd(wd0))
on.exit(par(mar = mar0), add = TRUE)
recordTable <- dataFrameTibbleCheck(df = recordTable)
timeZone <- "UTC"
checkForSpacesInColumnNames(speciesCol = speciesCol, recordDateTimeCol = recordDateTimeCol)
if (!is.data.frame(recordTable))
stop("recordTable must be a data frame", call. = FALSE)
if (!speciesCol %in% colnames(recordTable))
stop(paste("speciesCol = \"", speciesCol, "\" is not a column name in recordTable",
sep = ""), call. = FALSE)
if (!recordDateTimeCol %in% colnames(recordTable))
stop(paste("recordDateTimeCol = \"", recordDateTimeCol,
"\" is not a column name in recordTable", sep = ""),
call. = FALSE)
stopifnot(is.logical(c(allSpecies, writePNG, plotR, createDir)))
if (allSpecies == FALSE) {
stopifnot(species %in% recordTable[, speciesCol])
stopifnot(hasArg(species))
}
recordTable$DateTime2 <- parseDateTimeObject(inputColumn = recordTable[,
recordDateTimeCol], dateTimeFormat = recordDateTimeFormat,
timeZone = timeZone)
recordTable$Time2 <- format(recordTable$DateTime2, format = "%H:%M:%S",
usetz = FALSE)
recordTable$Time.rad <- (as.numeric(as.POSIXct(strptime(recordTable$Time2,
format = "%H:%M:%S", tz = timeZone))) - as.numeric(as.POSIXct(strptime("0",
format = "%S", tz = timeZone))))/3600 * (pi/12)
if (isTRUE(writePNG)) {
if (hasArg(plotDirectory)) {
if (isTRUE(createDir)) {
dir.create(plotDirectory, recursive = TRUE, showWarnings = FALSE)
setwd(plotDirectory)
}
else {
stopifnot(file.exists(plotDirectory))
setwd(plotDirectory)
}
}
else {
stop("writePNG is TRUE. Please set plotDirectory",
call. = FALSE)
}
}
pngWidth <- pngMaxPix
pngHeight <- round(pngMaxPix * 0.8)
if (allSpecies == FALSE) {
subset_species <- subset(recordTable, recordTable[, speciesCol] ==
species)
if (nrow(subset_species) == 1)
stop(paste(species, "had only 1 record. Cannot estimate density."),
call. = FALSE)
try_error_tmp <- try({
if (isTRUE(writePNG))
png(filename = paste("activity_density_",
species, "_", Sys.Date(), ".png",
sep = ""), width = pngWidth, height = pngHeight,
units = "px", res = 96, type = "cairo")
if (isTRUE(writePNG) | isTRUE(plotR)) {
densityPlot(subset_species$Time.rad, main = paste("Activity of",
species), rug = add.rug, ...)
mtext(paste("number of records:", nrow(subset_species)),
side = 3, line = 0)
}
if (isTRUE(writePNG))
dev.off()
}, silent = TRUE)
if (class(try_error_tmp) == "try-error")
warning(paste(toupper(species), ": ", try_error_tmp[1],
" - SKIPPED", sep = ""), call. = FALSE)
}
else {
subset_species_list <- list()
for (i in 1:length(unique(recordTable[, speciesCol]))) {
spec.tmp <- unique(recordTable[, speciesCol])[i]
subset_species <- subset(recordTable, recordTable[,
speciesCol] == spec.tmp)
plot_main_title <- paste("Activity of", spec.tmp)
if (nrow(subset_species) == 1) {
warning(paste(toupper(spec.tmp), ": It had only 1 record. Cannot estimate density. - SKIPPED",
sep = ""), call. = FALSE)
next
}
else {
try_error_tmp <- try({
if (isTRUE(writePNG))
png(filename = paste("activity_density_",
spec.tmp, "_", Sys.Date(), ".png",
sep = ""), width = pngWidth, height = pngHeight,
units = "px", res = 96, type = "cairo")
if (isTRUE(writePNG) | isTRUE(plotR)) {
densityPlot(subset_species$Time.rad, main = plot_main_title,
rug = add.rug, ...)
mtext(paste("number of records:", nrow(subset_species)),
side = 3, line = 0)
}
if (isTRUE(writePNG))
dev.off()
}, silent = TRUE)
if (class(try_error_tmp) == "try-error")
warning(paste(toupper(spec.tmp), ": ",
try_error_tmp[1], " - SKIPPED",
sep = ""), call. = FALSE)
}
subset_species_list[[i]] <- subset_species$Time.rad
names(subset_species_list)[i] <- spec.tmp
}
}
if (allSpecies == FALSE) {
return(invisible(subset_species$Time.rad))
}
else {
return(invisible(subset_species_list))
}
}
Yet, when I try to run this new function (arguments omitted here for clarity), it can't find a function embedded within.
How can I somehow assign this function to look within the original package camtrapR for any dependencies, etc.? and why does the code output from the function not already do this?
New()
Error in dataFrameTibbleCheck(df = recordTable) :
could not find function "dataFrameTibbleCheck"
This answer here: https://stackoverflow.com/a/49277036/9096420 allows one to manually edit and save a function's code for each R session, but it is non-reproducible (not code) that can be shared or re-used.
If New is the new function copied from camtrapR then use
environment(New) <- asNamespace("camtrapR")
to ensure that the function calls in its body are looked up in the correct places.
I am scraping some data from an API, and my code works just fine as long as I extract pages 0 to 98. Whenever my loop reaches 99, I get an error Error: Internal Server Error (HTTP 500)..
Tried to find an answer but I am only proficient in R and C# and cannot understand Python or other.
keywords = c('ABC OR DEF')
parameters <- list(
'q' = keywords,
num_days = 1,
language = 'en',
num_results = 100,
page = 0,
'api_key' = '123456'
)
response <- httr::GET(get_url, query = parameters)
# latest_page_number <- get_last_page(parsed)
httr::stop_for_status(response)
content <- httr::content(response, type = 'text', encoding = 'utf-8')
parsed <- jsonlite::fromJSON(content, simplifyVector = FALSE, simplifyDataFrame = TRUE)
num_pages = round(parsed[["total_results"]]/100)
print(num_pages)
result = parsed$results
for(x in 1:(num_pages))
{
print(x)
parameters <- list(
'q' = keywords,
page = x,
num_days = 7,
language = 'en',
num_results = 100,
'api_key' = '123456'
)
response <- httr::GET(get_url, query = parameters)
httr::stop_for_status(response)
content <- httr::content(response, type = 'text', encoding = 'utf-8')
# content <- httr::content(response)
parsed <- jsonlite::fromJSON(content, simplifyVector = FALSE, simplifyDataFrame = TRUE)
Sys.sleep(0.2)
result = rbind(result,parsed$results[,colnames(result)])
}
I am parsing argument in a rscript (merge_em.r) below. Let's say I run the code below using commandline Rscript merge_em.r dataframe1, dataframe2 which gives me this error: Error in setwd(working.dir) : character argument expected. I want to keep working directory argument optional. How do I do it?
library("argparse")
merge_em <- function (x, y, working.dir){
mergedfile <- merge (x, y, by = intersect(names(x), names(y)))
if (missing(working.dir)) {
print ("Working directory not specified! Will use present working directory.")
working.dir <- as.character(file.path(getwd()))
} else {
working.dir <- working.dir
}
setwd(working.dir)
write.table (mergedfile, "merged.txt",
col.names = FALSE,
row.names = FALSE,
sep = "\t",
quote = FALSE
)
}
main <- function() {
# breaks if you set warn = 2
options(error = traceback,
warn = 1)
parser <- ArgumentParser(prog = "merge_em.r",
description = "Merge dataframes")
parser <- ArgumentParser()
parser$add_argument("x")
parser$add_argument("y")
parser$add_argument(
"--working_dir",
dest = "working.dir",
type = "character",
metavar = "DIR",
required = FALSE,
help = "Working directory where files are present"
)
args <- parser$parse_args()
working.dir <- args$working.dir
x <- args$x
if (!R.utils::isAbsolutePath(x))
x <- file.path(working.dir, x)
y <- args$y
if (!R.utils::isAbsolutePath(y))
y <- file.path(working.dir, y)
tryCatch(
merge_em (x, y, working.dir)
,
finally = setwd(working.dir)
)
}
main()
You could exchange the missing() conditional to this:
if (working.dir=="") {
print ("Working directory not specified! Will use present working directory.")
working.dir <- as.character(file.path(getwd()))
} else {
print ("Working directory is specified!")
working.dir <- working.dir
}
And change the argument for working_dir to (default=""):
parser$add_argument(
"--working_dir",
dest = "working.dir",
type = "character",
metavar = "DIR",
default="",
required = FALSE,
help = "Working directory where files are present"
)
And change the tryCatch to:
tryCatch(merge_em(x, y, working.dir), finally = print("Fin"))
Why are you using setwd() io the finally part? If the argument is not given, there is nothing to set or?
Like that you can call the script like this, for example:
Rscript merge_em.r data_frame1, data_frame2
Or with a directory:
Rscript merge_em.r data_frame1, data_frame2, --working_dir "path_to_folder"
Full code:
library(argparse)
merge_em <- function (x, y, working.dir){
mergedfile <- merge (x, y, by = intersect(names(x), names(y)))
if (working.dir=="") {
print ("Working directory not specified! Will use present working directory.")
working.dir <- as.character(file.path(getwd()))
} else {
print ("Working directory is specified!")
working.dir <- working.dir
}
setwd(working.dir)
write.csv(x = mergedfile, file = "merged.txt",
row.names = FALSE,
quote = FALSE
)
}
main <- function() {
# breaks if you set warn = 2
options(error = traceback,
warn = 1)
parser <- ArgumentParser(prog = "merge_em.r",
description = "Merge dataframes")
parser <- ArgumentParser()
parser$add_argument("x")
parser$add_argument("y")
parser$add_argument(
"--working_dir",
dest = "working.dir",
type = "character",
metavar = "DIR",
default="",
required = FALSE,
help = "Working directory where files are present"
)
args <- parser$parse_args()
working.dir <- args$working.dir
x <- args$x
if (!R.utils::isAbsolutePath(x))
x <- file.path(working.dir, x)
y <- args$y
if (!R.utils::isAbsolutePath(y))
y <- file.path(working.dir, y)
tryCatch(merge_em(x, y, working.dir), finally = print("Fin"))
}
main()
You can set it as a default and override it when necessary.
merge_em <- function (x, y, working.dir = getwd()){
mergedfile <- merge (x, y, by = intersect(names(x), names(y)))
setwd(working.dir)
write (mergedfile, "merged.txt",
col.names = FALSE,
row.names = FALSE,
sep = "\t",
quote = FALSE
)
}
And override it with some other value:
merger_em(x, y, 'another/path/dir')
I haven't tested this, but default parameters are a standard in many languages.
Also, you can setwd with getwd like: setwd(getwd())
I'm making an R package that interfaces with the api from opendata.socrata.com.
I've run into a problem, that I've tracked to the build of the RCurl package.
On windows, with the RCurl build with openSSL, I've got no problems, but on Linux, with GnuTLS, it doesn't work.
You can check the build using curlVersion()$ssl_version.
Here is the function:
search.Socrata.Views <- function(search = NULL, ## full
topic = NULL, ## description
name = NULL, ## title field search
tags = NULL,
category = NULL,
count = FALSE,
limit = 10, ## max 200
page = 1,
type = "json" ## can also be xml
){
require('RCurl')
require('XML')
require('rjson')
## setting curl options
capath = system.file("CurlSSL",package = "RCurl")
cainfo = system.file("CurlSSL", "ca-bundle.crt", package = "RCurl")
cookie = 'cookiefile.txt'
curl = getCurlHandle ( cookiefile = cookie,
cookiejar = cookie,
useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en - US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6",
header = FALSE,
verbose = TRUE,
netrc = FALSE,
maxredirs = as.integer(20),
followlocation = TRUE,
ssl.verifypeer = TRUE,
cainfo = cainfo,
timeout = 100
)
## capath doesn't work:: NEED cainfo!
## test for existing cainfo:
if (!file.exists(cainfo)){
download.file('http://curl.haxx.se/ca/cacert.pem', cainfo )
}
## test for age of cainfo, if older than 2 weeks get new.
if (file.exists(cainfo)){
file.inf.cainfo <- file.info(cainfo)
age.cainfo <- Sys.time() - file.inf.cainfo[["mtime"]]
if(as.numeric(age.cainfo, units="days") > 14 ){
download.file('http://curl.haxx.se/ca/cacert.pem', cainfo )
}
}
### Make URL
baseSocrataUrl <- 'https://opendata.socrata.com/api/views.'
if(!is.null(category)){
category <- match.arg( category, c('Business', 'Fun', 'Personal', 'Education', 'Government'))
}
type <- match.arg( type, c('json', 'xml'))
## Tag
if(is.null(tags)){
tags <- NULL
} else {
tags <- URLencode( paste('&tags=', tags, sep = ''))
}
## Category
if(is.null(category)){
category <- NULL
} else {
category <- URLencode( paste('&category=', category, sep = ''))
}
## Limit
if(limit > 200){
limit <- '&limit=200'
} else {
limit <- paste('&limit=', limit, sep = '')
}
## search
if(is.null(search)){
search <- NULL
} else {
search <- URLencode( paste('&full=', search, sep = ''))
}
## page
page <- paste('&page=', page, sep = '')
## topic
if(is.null(topic)){
topic <- NULL
} else {
topic <- URLencode( paste('&description=', topic, sep = ''))
}
## name
if(is.null(name)){
name <- NULL
} else {
name <- URLencode( paste('&name=', name, sep = ''))
}
## count
if(count){
count <- '&count=TRUE'
} else {
count <- NULL
}
### Retrieving html
SocrataUrl <- paste( baseSocrataUrl, type, '?', page, tags, category, limit, search, name, topic, count, sep = '')
SocrataHtml <- getURL(SocrataUrl, curl = curl)
assign('search.Socrata.Call', SocrataUrl, envir=.GlobalEnv)
if(type == 'json'){
SocrataTable <- fromJSON(SocrataHtml)
SocrataTable <- lapply( SocrataTable, function(x){data.frame( x, stringsAsFactors = FALSE) } )
SocrataTable.df <- data.frame( matrix( nrow = length( SocrataTable), ncol = max(unlist(lapply(SocrataTable, length) ) ) ) )
names(SocrataTable.df) <- names( SocrataTable [lapply( SocrataTable, length ) == max( unlist( lapply( SocrataTable, length) ) ) ] [[1]] )
for( i in 1: length( SocrataTable ) ){
for( j in 1: length( names( SocrataTable[[i]] ) ) ){
SocrataTable.df[i, names( SocrataTable[[i]] )[j]] <- SocrataTable[[i]][i, names( SocrataTable[[i]] ) [j] ]
}
}
rm(curl)
gc()
return(SocrataTable.df)
} else {
rm(curl)
gc()
return(SocrataHtml)
}
}
Run the function with:
socrata.views <- search.Socrata.Views(topic = 'airplane')
print(socrata.views)
I haven't tested your code under Linux, but I can say that you're constructing URLs the hard way, which may be causing bugs. Using getForm, you can simplify your code considerably.
params <- list(
category = category,
tags = tags,
limit = min(limit, 200)
#etc.
)
params <- Filter(Negate(is.null), params)
getForm(baseSocrataUrl, .params = params, curl = curl)