Ответ 1
Мне удалось подтвердить, что проблема с памятью существует в Windows 7. (Работа через VMware Fusion на MacOSX). Он также, похоже, существует на MacOSX, хотя использование памяти выглядит довольно постепенным [неподтвержденным, но указывает на утечку памяти]. Немного сложно с MacOSX, поскольку ОС сжимает память, если видит высокую степень использования.
Предлагаемое обходное решение:
Мое предложение в свете вышесказанного состоит в том, чтобы разбить наборы таблиц в меньшие группы при загрузке из Бюро переписи населения США. Зачем? Ну, глядя на код, вы загружаете данные для хранения в. CSV файлах. Следовательно, обходной путь в краткосрочной перспективе состоит в том, чтобы разбить список загружаемых таблиц. Ваша программа должна быть в состоянии успешно выполнить множество запусков.
Один из вариантов - создать обертку RScript и запустить ее через N прогонов, где каждый вызывает отдельный сеанс R. то есть Rscript последовательно вызывает N RSessions, каждый сеанс загружает N файлов
пь. Основываясь на вашем коде и наблюдаемом использовании памяти, я считаю, что вы загружаете много таблиц, поэтому наилучшим вариантом может быть разделение на R сеансов (ов).
nb. Следующее должно работать под cgiwin
в Windows 7.
Вызов Script
Пример: Загрузите первичные таблицы с 01 по 27 - если они не существуют, пропустите...
!#/bin/bash
#Ref: https://censusreporter.org/topics/table-codes/
# Params: Primary Table Year Span
for CensusTableCode in $(seq -w 1 27)
do
R --no-save -q --slave < ./PullCensus.R --args B"$CensusTableCode"001 2014 5
done
PullCensus.R
if (!require(acs)) install.packages("acs")
if (!require(pryr)) install.packages("pryr")
# You can obtain a US Census key from the developer site
# "e24539dfe0e8a5c5bf99d78a2bb8138abaa3b851"
api.key.install(key = "** Secret**")
setwd("~/dev/stackoverflow/37264919")
# Extract Table Structure
#
# B = Detailed Column Breakdown
# 19 = Income (Households and Families)
# 001 =
# A - I = Race
#
args <- commandArgs(trailingOnly = TRUE) # trailingOnly=TRUE means that only your arguments are returned
if ( length(args) != 0 ) {
tableCodes <- args[1]
defEndYear = args[2]
defSpan = args[3]
} else {
tableCodes <- c("B02001")
defEndYear = 2014
defSpan = 5
}
# for loop to extract tables from API and save them on API
for (i in 1:length(tableCodes))
{
tryCatch(
table <- acs.fetch(table.number = tableCodes[i],
endyear = defEndYear,
span = defSpan,
geography = geo.make(state = "NY",
county = "*",
tract = "*"),
col.names = "pretty"),
error = function(e) { print("Table skipped")} )
# if the table is actually fetched then we save it
if (exists("table", mode = "S4"))
{
print(paste("Table", i, "fetched"))
if (!is.na(table))
{
write.csv(estimate(table), paste(defEndYear,"_",tableCodes[i], ".csv", sep = ""))
}
print(mem_used())
print(mem_change(rm(table)))
gc(reset = TRUE)
print(mem_used())
}
}
Я надеюсь, что это поможет в качестве примера. Это подход.; -)
Т.
Следующие шаги:
Я посмотрю источник пакета, чтобы увидеть, могу ли я увидеть, что на самом деле неправильно. В качестве альтернативы, вы сами можете сузить его и записать ошибку в пакет.
Фон/рабочий пример:
Я считаю, что это может помочь предоставить пример рабочего кода для создания описанного выше решения. Зачем? Цель здесь - предоставить пример, который люди могут использовать для тестирования и рассмотрения того, что происходит. Зачем? Ну, это облегчает понимание вашего вопроса и намерения.
В сущности, (как я понимаю) вы загружаете данные США по переписи США с веб-сайта переписи США. Табличные коды используются для указания данных, которые вы хотите загрузить. Итак, я просто создал набор табличных кодов и протестировал использование памяти, чтобы узнать, будет ли память потребляться, как вы объяснили.
library(acs)
library(pryr)
library(tigris)
library(stringr) # to pad fips codes
library(maptools)
# You can obtain a US Census key from the developer site
# "e24539dfe0e8a5c5bf99d78a2bb8138abaa3b851"
api.key.install(key = "<INSERT KEY HERE>")
# Table Codes
#
# While Census Reporter hopes to save you from the details, you may be
# interested to understand some of the rationale behind American Community
# Survey table identifiers.
#
# Detailed Tables
#
# The bulk of the American Community Survey is the over 1400 detailed data
# tables. These tables have reference codes, and knowing how the codes are
# structured can be helpful in knowing which table to use.
#
# Codes start with either the letter B or C, followed by two digits for the
# table subject, then 3 digits that uniquely identify the table. (For a small
# number of technical tables the unique identifier is 4 digits.) In some cases
# additional letters for racial iterations and Puerto Rico-specific tables.
#
# Full and Collapsed Tables
#
# Tables beginning with B have the most detailed column breakdown, while a
# C table for the same numbers will have fewer columns. For example, the
# B02003 table ("Detailed Race") has 71 columns, while the "collapsed
# version," C02003 has only 19 columns. While your instinct may be to want
# as much data as possible, sometimes choosing the C table can simplify
# your analysis.
#
# Table subjects
#
# The first two digits after B/C indicate the broad subject of a table.
# Note that many tables have more than one subject, but this reflects the
# main subject.
#
# 01 Age and Sex
# 02 Race
# 03 Hispanic Origin
# 04 Ancestry
# 05 Foreign Born; Citizenship; Year or Entry; Nativity
# 06 Place of Birth07Residence 1 Year Ago; Migration
# 08 Journey to Work; Workers' Characteristics; Commuting
# 09 Children; Household Relationship
# 10 Grandparents; Grandchildren
# 11 Household Type; Family Type; Subfamilies
# 12 Marital Status and History13Fertility
# 14 School Enrollment
# 15 Educational Attainment
# 16 Language Spoken at Home and Ability to Speak English
# 17 Poverty
# 18 Disability
# 19 Income (Households and Families)
# 20 Earnings (Individuals)
# 21 Veteran Status
# 22 Transfer Programs (Public Assistance)
# 23 Employment Status; Work Experience; Labor Force
# 24 Industry; Occupation; Class of Worker
# 25 Housing Characteristics
# 26 Group Quarters
# 27 Health Insurance
#
# Three groups of tables reflect technical details about how the Census is
# administered. In general, you probably don't need to look at these too
# closely, but if you need to check for possible weaknesses in your data
# analysis, they may come into play.
#
# 00 Unweighted Count
# 98 Quality Measures
# 99 Imputations
#
# Race and Latino Origin
#
# Many tables are provided in multiple racial tabulations. If a table code
# ends in a letter from A-I, that code indicates that the table universe is
# restricted to a subset based on responses to the race or
# Hispanic/Latino-origin questions.
#
# Here is a guide to those codes:
#
# A White alone
# B Black or African American Alone
# C American Indian and Alaska Native Alone
# D Asian Alone
# E Native Hawaiian and Other Pacific Islander Alone
# F Some Other Race Alone
# G Two or More Races
# H White Alone, Not Hispanic or Latino
# I Hispanic or Latino
setwd("~/dev/stackoverflow/37264919")
# Extract Table Structure
#
# B = Detailed Column Breakdown
# 19 = Income (Households and Families)
# 001 =
# A - I = Race
#
tablecodes <- c("B19001", "B19001A", "B19001B", "B19001C", "B19001D",
"B19001E", "B19001F", "B19001G", "B19001H", "B19001I" )
# for loop to extract tables from API and save them on API
for (i in 1:length(tablecodes))
{
print(tablecodes[i])
tryCatch(
table <- acs.fetch(table.number = tablecodes[i],
endyear = 2014,
span = 5,
geography = geo.make(state = "NY",
county = "*",
tract = "*"),
col.names = "pretty"),
error = function(e) { print("Table skipped")} )
# if the table is actually fetched then we save it
if (exists("table", mode="S4"))
{
print(paste("Table", i, "fetched"))
if (!is.na(table))
{
write.csv(estimate(table), paste("T",tablecodes[i], ".csv", sep = ""))
}
print(mem_used())
print(mem_change(rm(table)))
gc()
print(mem_used())
}
}
Вывод времени выполнения
> library(acs)
> library(pryr)
> library(tigris)
> library(stringr) # to pad fips codes
> library(maptools)
> # You can obtain a US Census key from the developer site
> # "e24539dfe0e8a5c5bf99d78a2bb8138abaa3b851"
> api.key.install(key = "...secret...")
>
...
> setwd("~/dev/stackoverflow/37264919")
>
> # Extract Table Structure
> #
> # B = Detailed Column Breakdown
> # 19 = Income (Households and Families)
> # 001 =
> # A - I = Race
> #
> tablecodes <- c("B19001", "B19001A", "B19001B", "B19001C", "B19001D",
+ "B19001E", "B19001F", "B19001G", "B19001H", "B19001I" )
>
> # for loop to extract tables from API and save them on API
> for (i in 1:length(tablecodes))
+ {
+ print(tablecodes[i])
+ tryCatch(
+ table <- acs.fetch(table.number = tablecodes[i],
+ endyear = 2014,
+ span = 5,
+ geography = geo.make(state = "NY",
+ county = "*",
+ tract = "*"),
+ col.names = "pretty"),
+ error = function(e) { print("Table skipped")} )
+
+ # if the table is actually fetched then we save it
+ if (exists("table", mode="S4"))
+ {
+ print(paste("Table", i, "fetched"))
+ if (!is.na(table))
+ {
+ write.csv(estimate(table), paste("T",tablecodes[i], ".csv", sep = ""))
+ }
+ print(mem_used())
+ print(mem_change(rm(table)))
+ gc()
+ print(mem_used())
+ }
+ }
[1] "B19001"
[1] "Table 1 fetched"
95.4 MB
-1.88 MB
93.6 MB
[1] "B19001A"
[1] "Table 2 fetched"
95.4 MB
-1.88 MB
93.6 MB
[1] "B19001B"
[1] "Table 3 fetched"
95.5 MB
-1.88 MB
93.6 MB
[1] "B19001C"
[1] "Table 4 fetched"
95.5 MB
-1.88 MB
93.6 MB
[1] "B19001D"
[1] "Table 5 fetched"
95.5 MB
-1.88 MB
93.6 MB
[1] "B19001E"
[1] "Table 6 fetched"
95.5 MB
-1.88 MB
93.6 MB
[1] "B19001F"
[1] "Table 7 fetched"
95.5 MB
-1.88 MB
93.6 MB
[1] "B19001G"
[1] "Table 8 fetched"
95.5 MB
-1.88 MB
93.6 MB
[1] "B19001H"
[1] "Table 9 fetched"
95.5 MB
-1.88 MB
93.6 MB
[1] "B19001I"
[1] "Table 10 fetched"
95.5 MB
-1.88 MB
93.6 MB
Выходные файлы
>ll
total 8520
[email protected] 13 hidden staff 442B Oct 17 20:41 .
[email protected] 40 hidden staff 1.3K Oct 17 23:17 ..
[email protected] 1 hidden staff 4.4K Oct 17 23:43 37264919.R
[email protected] 1 hidden staff 492K Oct 17 23:50 TB19001.csv
[email protected] 1 hidden staff 472K Oct 17 23:51 TB19001A.csv
[email protected] 1 hidden staff 414K Oct 17 23:51 TB19001B.csv
[email protected] 1 hidden staff 387K Oct 17 23:51 TB19001C.csv
[email protected] 1 hidden staff 403K Oct 17 23:51 TB19001D.csv
[email protected] 1 hidden staff 386K Oct 17 23:51 TB19001E.csv
[email protected] 1 hidden staff 402K Oct 17 23:51 TB19001F.csv
[email protected] 1 hidden staff 393K Oct 17 23:52 TB19001G.csv
[email protected] 1 hidden staff 465K Oct 17 23:44 TB19001H.csv
[email protected] 1 hidden staff 417K Oct 17 23:44 TB19001I.csv