error "variable names are limited to 256 bytes" when sourcing code

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

error "variable names are limited to 256 bytes" when sourcing code

Brian Frizzelle
I've written a function that takes some input data output from a
simulation model and creates some graphs. It's not very complicated
code, and it works perfectly fine if I just run the code as is.

But I have converted it into a function so we call it externally, and
when I try to source the code to test the function, I get the error
message "variable names are limited to 256 bytes". I've tried searching
online for a solution to this, but everything I have come across deals
with this error in relation to input data, not a function.

The code is 389 lines long, so I'd rather not paste it here and clog up
inboxes. If you have an idea as to why this is happening and would like
to see the code, please email me and I will send it to you.

Thanks in advance.

--

Brian Frizzelle
Spatial Analysis Services
Carolina Population Center
University of North Carolina at Chapel Hill
Voice: 919-966-6663
Fax: 919-966-6638
Email: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: error "variable names are limited to 256 bytes" when sourcing code

Wu Gong
I can only repeat your error message :)


> n256 <- paste(rep("A",256),collapse="")
> assign(n256, 1)
> n257 <- paste(rep("A",257),collapse="")
> assign(n257, 1)
Error in assign(n257, 1) : variable names are limited to 256 bytes
Reply | Threaded
Open this post in threaded view
|

Re: error "variable names are limited to 256 bytes" when sourcing code

barry rowlingson
On Wed, May 26, 2010 at 6:00 PM, Wu Gong <[hidden email]> wrote:

>
> I can only repeat your error message :)
>
>
>> n256 <- paste(rep("A",256),collapse="")
>> assign(n256, 1)
>> n257 <- paste(rep("A",257),collapse="")
>> assign(n257, 1)
> Error in assign(n257, 1) : variable names are limited to 256 bytes
>

 If a variable name can have 26 upper case + 26 lower case + 10
numbers then the number of possible variable names is:

711659926691456588820198688981513283237719214167524272940980007340737850\
071505550367426050190853744948955339987662427844810850852717191846883823768674\
280839119270574786535774460628640384757837267418932039347078114901615267344319\
690975277428929737916031623809028545597238524149983532303848529517503894555603\
085813572927495336324076794731576794044444062823255544802787912646756996122962\
654809395519130134923611540639384237080197541181260772381917961683956924416

 which should be enough for everyone (that's probably a lower bound
since names can have dots and underscores etc in them).

 I suspect the code is doing something very horrible with variable
names, and needs to be restructured to use lists or vectors or arrays
or anything other than encoding information into the name of an
object.

 I had to calculate 62^256 in Maxima since R thinks it's infinite.

Barry

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: error &quot;variable names are limited to 256 bytes&quot; when sourcing code

Ben Bolker
Barry Rowlingson <b.rowlingson <at> lancaster.ac.uk> writes:

>  If a variable name can have 26 upper case + 26 lower case + 10
> numbers then the number of possible variable names is:
>
> 711659926691456588820198688981513283237719214167524272940980007340737850\
> 071505550367426050190853744948955339987662427844810850852717191846883823768674\
> 280839119270574786535774460628640384757837267418932039347078114901615267344319\
> 690975277428929737916031623809028545597238524149983532303848529517503894555603\
> 085813572927495336324076794731576794044444062823255544802787912646756996122962\
> 654809395519130134923611540639384237080197541181260772381917961683956924416
>
>  which should be enough for everyone (that's probably a lower bound
> since names can have dots and underscores etc in them).

  [completely off topic]

  You don't really need Maxima if you're willing to work on a log
scale ... the first character has to be alphabetic, so there are
only 52 choices.  I've included (26*2+10+2=64) possibilities for
the other 255 slots.

> log(52)+255*log(64)
[1] 1064.466
> .Last.value/log(10)
[1] 462.2919
> 10^462
[1] Inf

  I'm not quite sure how it works, but you can get even more if
you allow back-quotes:

> `#$!!`=4
> `#$!!`
[1] 4

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: error "variable names are limited to 256 bytes" when sou

Ted.Harding-2
In reply to this post by barry rowlingson
On 26-May-10 18:40:46, Barry Rowlingson wrote:

> On Wed, May 26, 2010 at 6:00 PM, Wu Gong <[hidden email]> wrote:
>>
>> I can only repeat your error message :)
>>
>>> n256 <- paste(rep("A",256),collapse="")
>>> assign(n256, 1)
>>> n257 <- paste(rep("A",257),collapse="")
>>> assign(n257, 1)
>> Error in assign(n257, 1) : variable names are limited to 256 bytes
>
>  If a variable name can have 26 upper case + 26 lower case + 10
> numbers then the number of possible variable names is:
>
> 711659926691456588820198688981513283237719214167524272940980\
> 007340737850071505550367426050190853744948955339987662427844\
> 810850852717191846883823768674280839119270574786535774460628\
> 640384757837267418932039347078114901615267344319690975277428\
> 929737916031623809028545597238524149983532303848529517503894\
> 555603085813572927495336324076794731576794044444062823255544\
> 802787912646756996122962654809395519130134923611540639384237\
> 080197541181260772381917961683956924416
>
> which should be enough for everyone (that's probably a lower
> bound since names can have dots and underscores etc in them).
>
> I suspect the code is doing something very horrible with variable
> names, and needs to be restructured to use lists or vectors or
> arrays or anything other than encoding information into the
> name of an object.
>
> I had to calculate 62^256 in Maxima since R thinks it's infinite.
> Barry

Barry,
Add in a couple of extras to the domain, for the "." and "_", and
you get 64^256 = (2^6)^256 = 2^1536, which is easier to grasp.

And I wouldn't bother with maxima myself -- bc will do:

$ bc -l
[banner stuff]
2^1536
241031242692103258858011660602831411291209324794568895135967\
503906525739159180320066908502410734604966344876628088800478\
786241697879495832496961298789077465145521333938162522477078\
207791768149967684554313738782005759734585790459910946138712\
209950796499781564134230067762947335528161742841179416396778\
587037036896910922159194305423201156275845008057958785090099\
371489228347664663118151506380487337518226050624699283789870\
5971012525843324401232986857004760339316736

or, less precisely, 10 to the power:

scale=200
l(2^1536)/l(10)
462.38207333987511584830294229682128911593965792579871945281\
658029123817872551604657192025925353396025449135696603800890\
228956346542109210521552742457041240329907655309444106648855\
511190262212121781847591

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 26-May-10                                       Time: 20:12:29
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: error "variable names are limited to 256 bytes" when sourcing code

Brian Frizzelle
In reply to this post by Brian Frizzelle
All,

I think there may be some misunderstanding about my problem. In my code, which is written as an R function, all of my variable names are short. I get that error when I try to source the function so I can call it. I just do not know why I'm getting the error, especially since I have written other very similar functions that all work.

So I have chosen to post the code below. I welcome any ideas about where in this code the error is occurring.

##########################################################
###  SCRIPT: graph_hh_wealth_function.R
###  DATE: April 22, 2010
###  AUTHOR: Brian Frizzelle
###                                
###  This function draws two line graphs of household-level wealth from a variable
###  in a dataset output from an agent-based model run.
###
###  The two graphs are:
###   1) a line graph showing change in Wealth over time for each individual
###     household
###   2) a line graph of summary statistics of Wealth for the households (min,
###     max, mean, and error bars)
###
###  Required Arguments:
###   * inpath - The path to the directory containing the village-level statistics
### file
### NOTE: Use the UNIX forward slash (/) convention when entering the path
### and do not include a slash at the end of the path.
### OK: "D:/data"
### Not OK: "D:\data"
### Not OK: "D:/data/"
### * infile - The name of the village-level statistics file
### * outpath - The path to the directory where you want the output graphics to
### be saved
### * outpref - The prefix that you want for the output PNG graphics files
### NOTE: Do not include underscores (_) or spaces in the output prefix.
### Instead, please use dashes (-). Your prefix will be separated
### from the remainder of the file names by a dash.
###
###  Optional Arguments:
### * log.plot - Logical. Default is FALSE.
### If TRUE, then the individual household wealth graph is plotted
### with a logarithmic Y-axis.
### If FALSE, then the individual household wealth graph is plotted
### with a standard Y-axis.
### * err.bar - Logical. Default is TRUE.
### If TRUE, then error bars of 1 standard deviation will be drawn
### around the mean.
### If FALSE, then no error bars will be drawn.
### * n.quantiles - Integer. Value between 3 and 10. Default is 0, meaning no
### quantile lines will be drawn.
### This is the number of bins into which you would like the variable
### separated. One line for each will be drawn, with the exception of
### the min and max, which are already drawn.
##########################################################

graph.hh.wealth <- function(inpath, infile, outpath, outpref, log.plot=FALSE,
                                    err.bar=TRUE, n.quantiles=0)

{

        ##*************************************************
        ## Set the path and name of the input file
        ##  - The 'paste' command concatenates the two
        ##  - The 'skiplines' var sets the number of lines
        ##      to skip when reading in the dataset.
        ##*************************************************
        if (substr(inpath, nchar(inpath), nchar(inpath)) == "/")
                inpath <- substr(inpath, 0, nchar(inpath)-1)
        if (substr(outpath, nchar(outpath), nchar(outpath)) == "/")
                outpath <- substr(outpath, 0, nchar(outpath)-1)
        pathfile <- paste(inpath, infile, sep="/")
        skiplines <- 1

        ##*************************************************
        ## Set the names of the output file graphics
        ##*************************************************
        fnout.wlth <- "hhwealth.png"
        fnout.wlthss <- "hhwealth-sumstats.png"
        output.png.wlth <- paste(outpath, "/", outpref, fnout.wlth, sep="")
        output.png.wlthss <- paste(outpath, "/", outpref, fnout.wlthss, sep="")

        ##*************************************************
        ## Read in the household-level output dataset
        ##*************************************************
        hhstats <- read.delim(file=pathfile, header=TRUE, sep ="\t", dec=".", skip=skiplines)

        ##*************************************************
        ## Get some information from the household-level
        ## dataset for use in plotting.
        ##  - the village number
        ##  - a vector of the unique HH IDs
        ##  - the maximum number of model run years
        ##  - the maximum wealth among all households
        ##*************************************************
        villnum <- mean(hhstats$V84ID)
        uniq.hh.ids <- unique(hhstats$HHID00)
        max.yr <- max(hhstats$Year)
        max.wlth <- max(hhstats$Wealth)

        ##*************************************************
        ## Extract out the vars needed for the individual
        ## household line graph
        ## Vars: Year, HHID00, Wealth, Status
        ##*************************************************
        hh.wealth <- hhstats[,c("Year","HHID00","Wealth","Status")]
        hh.wealth.pos <- hh.wealth[hh.wealth$Wealth >= 0,]
        hh.wealth.plot <- hh.wealth.pos[hh.wealth.pos$Status == 0 | hh.wealth.pos$Status == 1,]

        ##*************************************************
        ## Get the summary statistics by year for Wealth
        ##*************************************************
        ## Get a list of unique years for iterating
        unqyrlist <- unique(hh.wealth.plot$Year)

        ## Determine the divisor for the quantiles
        div.q <- 1.0 / n.quantiles

        ## Loop through years to extract summary statistics
        for (yr in min(unqyrlist):max(unqyrlist)) {
                # Get records for current year
                this.yr <- hh.wealth.plot[hh.wealth.plot$Year == yr,]
                # Calc summary statistics by statistic
                this.n <- dim(this.yr)[1]
                this.min <- min(this.yr$Wealth)
                this.max <- max(this.yr$Wealth)
                this.mean <- mean(this.yr$Wealth)
                this.med <- median(this.yr$Wealth)
                this.sd <- sd(this.yr$Wealth)
                this.ebneg <- this.mean-this.sd
                this.ebpos <- this.mean+this.sd
                this.sumstats <- c(yr, this.n, this.min, this.max, this.mean,
                                        this.med, this.sd, this.ebneg, this.ebpos)
                # Convert vector to 1-row data frame
                this.df <- as.data.frame(t(as.matrix(this.sumstats)))
                colnames(this.df) <- c("Year", "Count", "Min", "Max", "Mean", "Median",
                                                "SD", "EB.Low", "EB.High")
                # Now, calculate the specified quantiles for this year
                names(yr) <- "Year"
                this.q <- as.data.frame(t(as.matrix(c(
                                quantile(this.yr$Wealth, probs=seq(0, 1, div.q)), yr))))
                # Append the data frame to the output data frame
                if (yr == min(unqyrlist)) {
                        hh.wealth.plot.ss <- this.df
                        hh.wealth.quantiles <- this.q
                } else {
                        hh.wealth.plot.ss <- rbind.data.frame(hh.wealth.plot.ss, this.df)
                        hh.wealth.quantiles <- rbind.data.frame(hh.wealth.quantiles, this.q)
                }
        }

        ##*************************************************
        ## Set up parameters for quantile plots
        ##*************************************************
        q.names <- c("", "", "Terciles", "Quantiles", "Quintiles",
                        "Sextiles", "Septiles", "Octiles", "Noniles", "Deciles")
        q.labs <- c("1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th")
        # Determine if median will be plotted
        if (n.quantiles %% 2) plot.med <- FALSE else plot.med <- TRUE
        # Determine which non-median quantile columns will be plotted
        if (n.quantiles %% 2) { # If the number of quantiles is odd
                cols.q <- c(2:n.quantiles)
        } else { # If the number of quantiles is even
                cols.q <- c(2:(n.quantiles/2), (2+(n.quantiles/2)):n.quantiles)
                col.med <- (n.quantiles/2) + 1
}

        ##*************************************************
        ## Set up plotting parameters for the summary
        ## statistics graph
        ##*************************************************
        # Set y axis limits
        ylim.min <- min(hh.wealth.plot.ss)
        ylim.max <- max(hh.wealth.plot.ss)
        # Set title
        ttl.ss <- paste("Summary Statistics of Household Wealth Change Over Time\n (Village",
                villnum, ")", sep="")
        # Set legend based on arguments
        leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", "# of HHs")
        if (err.bars == TRUE & n.quantiles == 0) {
                leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", "# of HHs")
                leg.lty.ss <- c(1,2,4,5,0)
                leg.lwd.ss <- c(2,1,1,1,0)
                leg.pch.ss <- c(-1,-1,-1,-1,16)
                leg.col.ss <- c("black","darkolivegreen4","chartreuse4","red","blue")
        } else if (err.bars == FALSE & n.quantiles >= 3) {
                if (n.quantiles %% 2) { # Odd number of quantiles
                        leg.txt.ss <- c("Mean", "Min", "Max", q.names[n.quantiles], "# of HHs")
                        leg.lty.ss <- c(1,2,4,3,0)
                        leg.lwd.ss <- c(2,1,1,1,0)
                        leg.pch.ss <- c(-1,-1,-1,-1,16)
                        leg.col.ss <- c("black","darkolivegreen4","chartreuse4","orange","blue")
                } else { # Even number of quantiles
                        leg.txt.ss <- c("Mean", "Min", "Max", "Median", q.names[n.quantiles],
                                                "# of HHs")
                        leg.lty.ss <- c(1,2,4,3,3,0)
                        leg.lwd.ss <- c(2,1,1,2,1,0)
                        leg.pch.ss <- c(-1,-1,-1,-1,-1,16)
                        leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4", "orange",
                                                "orange", "blue")
                }
        } else if (err.bars == TRUE & n.quantiles >= 3) {
                if (n.quantiles %% 2) { # Odd number of quantiles
                        leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", q.names[n.quantiles],
                                                "# of HHs")
                        leg.lty.ss <- c(1,2,4,5,3,0)
                        leg.lwd.ss <- c(2,1,1,1,1,0)
                        leg.pch.ss <- c(-1,-1,-1,-1,-1,16)
                        leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4", "red", "orange",
                                                "blue")
                } else { # Even number of quantiles
                        leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", "Median",
                                                q.names[n.quantiles], "# of HHs")
                        leg.lty.ss <- c(1,2,4,5,3,3,0)
                        leg.lwd.ss <- c(2,1,1,1,2,1,0)
                        leg.pch.ss <- c(-1,-1,-1,-1,-1,-1,16)
                        leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4", "red", "orange",
                                                "orange","blue")
                }
        } else {
                leg.txt.ss <- c("Mean", "Min", "Max", "# of HHs")
                leg.lty.ss <- c(1,2,4,0)
                leg.lwd.ss <- c(2,1,1,0)
                leg.pch.ss <- c(-1,-1,-1,16)
                leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4", "blue")
        }


        ##*************************************************
        ## Set up vector of possible colors for the lines
        ##*************************************************
        colorset <- c(3:12, 26:62, 67:79, 81, 83:137, 139, 142:151)

        ##*************************************************
        ## Set up plotting parameters for the individual
        ## household graph
        ##*************************************************
        ## Create a vector of colors from the colorset
        ## above, one for every HHID00
        colors.id <- sample(colorset, length(uniq.hh.ids), replace=TRUE)
        ## Other parameters
        ttl.hh <- paste("Change in Household Wealth Over Time\n (Village",
                villnum, ")", sep="")
        leg.txt.hh <- c("Old Households", "Split Households")

        ##*************************************************
        ## Plot household-level summary statistics
        ##*************************************************
        png(filename=output.png.wlthss, width=10, height=7, units="in", res=300)

        ## Set initial plot with mean
        plot(hh.wealth.plot.ss$Year, # x var
                hh.wealth.plot.ss$Mean, # y var (MEAN)
                type="l", # line graph
                xlim=c(0,max.yr), # x-axis limits
                ylim=c(ylim.min, ylim.max), # y-axis limits
                lty=1, # solid line
                lwd=2, # line thickness
                xlab="",
                ylab="Wealth", # y-axis label
                main=ttl.ss)
        ## Add MIN
        lines(hh.wealth.plot.ss$Year, # x var
                hh.wealth.plot.ss$Min, # y var (MIN)
                type="l", # line graph
                col="darkolivegreen4",
                lty=2)
        ## Add MAX
        lines(hh.wealth.plot.ss$Year, # x var
                hh.wealth.plot.ss$Max, # y var (MAX)
                type="l", # line graph
                col="chartreuse4",
                lty=4)

` ## Add ERROR BARS if Argument 'err.bars' is TRUE
        if (err.bars == TRUE) {
                ## Add LOW ERROR BAR
                lines(hh.wealth.plot.ss$Year, # x var
                        hh.wealth.plot.ss$EB.Low, # y var (EB LOW)
                        type="l", # line graph
                        col="red",
                        lty=3)
                ## Add HIGH ERROR BAR
                lines(hh.wealth.plot.ss$Year, # x var
                        hh.wealth.plot.ss$EB.High, # y var (EB HIGH)
                        type="l", # line graph
                        col="red",
                        lty=3)
        }
        ## Add QUANTILES if Argument 'n.quantiles' is >= 3
        if (n.quantiles >= 3) {
                # Cycle through column numbers and draw quantile lines
                for (qcol in 1:length(cols.q)) {
                        lines(hh.wealth.quantiles$Year, # Plot quantile lines
                                hh.wealth.quantiles[,cols.q[qcol]],
                                type="l",
                                col="orange",
                                lty=3)
                        }
                # Add MEDIAN line
                if (plot.med == TRUE) {
                        lines(hh.wealth.quantiles$Year, # Plot median
                                hh.wealth.quantiles[,col.med],
                                type="l",
                                col="orange",
                                lty=3,
                                lwd=2)
                }
        }
        ## Add COUNT
        points(hh.wealth.plot.ss$Year, # x var
                hh.wealth.plot.ss$Count, # y var (COUNT)
                type="p", # line graph
                pch=16,
                col="blue",
                cex=0.5)
        ## Add LEGEND
        legend(x="topright",
                leg.txt.ss,
                lty=leg.lty.ss,
                lwd=leg.lwd.ss,
                pch=leg.pch.ss,
                col=leg.col.ss,
                cex=1)

        dev.off()


        ##*************************************************
        ## Plot wealth for individual households
        ##*************************************************
        png(filename=output.png.wlth, width=10, height=7, units="in", res=300)

        ## Create an empty plot
        if (log.plot == FALSE) {
                plot(hh.wealth.plot$Year, hh.wealth.plot$Wealth,
                        type="n",
                        xlim=c(0,max.yr),
                        main=ttl.hh,
                        xlab="",
                        ylab="Wealth")
        } else {
                plot(hh.wealth.plot$Year, hh.wealth.plot$Wealth,
                        log="y",
                        type="n",
                        xlim=c(0,max.yr),
                        main=ttl.hh,
                        xlab="",
                        ylab="Wealth (log scale)")
        }

        #legend(x=leg.x.coord, y=leg.y.coord, # Sets the location for the legend
        legend(x="topright",
                leg.txt.hh, # text in the legent
                col=c("red", "red"), # sets the line colors in the legend
                lty=c(1,3), # draws lines
                lwd=c(1,1), # sets line thickness
        # bty="n", # no border on the legend
                ncol=2, # makes it a 2-column legend
                cex=0.8) # sets the legend text size

        ## Loop through IDs and add a line for each
        for (id in 1:length(uniq.hh.ids)) {
                ## Get the current HH ID
                this.id <- uniq.hh.ids[id]

                ## Extract the records for the current ID
                this.sub <- hh.wealth.plot[hh.wealth.plot$HHID00 == this.id,]

                if (dim(this.sub)[1] > 0) {

                        ## Set line type
                        if (mean(this.sub$Status) == 0) {
                                ltype <- 1
                        } else {
                                ltype <- 3
                        }

                        ## Add the line for this ID
                        lines(this.sub$Year, this.sub$Wealth,
                                type="l",
                                col= colors.id[id],
                                lwd=1,
                                lty=ltype)
                }

        }

        dev.off()

}
Reply | Threaded
Open this post in threaded view
|

Re: error "variable names are limited to 256 bytes" when sourcing code

Erik Iverson-3
Is the '`' character supposed to be there before the ## Add error bars
comment?

If that is the problem, let it be a good lesson    to use an editor with
syntax highlighting. :)
 
 
 


Brian Frizzelle wrote:

> All,
>
> I think there may be some misunderstanding about my problem. In my code,
> which is written as an R function, all of my variable names are short. I get
> that error when I try to source the function so I can call it. I just do not
> know why I'm getting the error, especially since I have written other very
> similar functions that all work.
>
> So I have chosen to post the code below. I welcome any ideas about where in
> this code the error is occurring.
>
> ##########################################################
> ###  SCRIPT: graph_hh_wealth_function.R
> ###  DATE: April 22, 2010
> ###  AUTHOR: Brian Frizzelle
> ###                                
> ###  This function draws two line graphs of household-level wealth from a
> variable
> ###  in a dataset output from an agent-based model run.
> ###
> ###  The two graphs are:
> ###   1) a line graph showing change in Wealth over time for each individual
> ###     household
> ###   2) a line graph of summary statistics of Wealth for the households
> (min,
> ###     max, mean, and error bars)
> ###
> ###  Required Arguments:
> ###   * inpath - The path to the directory containing the village-level
> statistics
> ### file
> ### NOTE: Use the UNIX forward slash (/) convention when entering the path
> ### and do not include a slash at the end of the path.
> ### OK: "D:/data"
> ### Not OK: "D:\data"
> ### Not OK: "D:/data/"
> ### * infile - The name of the village-level statistics file
> ### * outpath - The path to the directory where you want the output graphics
> to
> ### be saved
> ### * outpref - The prefix that you want for the output PNG graphics files
> ### NOTE: Do not include underscores (_) or spaces in the output prefix.
> ### Instead, please use dashes (-). Your prefix will be separated
> ### from the remainder of the file names by a dash.
> ###
> ###  Optional Arguments:
> ### * log.plot - Logical. Default is FALSE.
> ### If TRUE, then the individual household wealth graph is plotted
> ### with a logarithmic Y-axis.
> ### If FALSE, then the individual household wealth graph is plotted
> ### with a standard Y-axis.
> ### * err.bar - Logical. Default is TRUE.
> ### If TRUE, then error bars of 1 standard deviation will be drawn
> ### around the mean.
> ### If FALSE, then no error bars will be drawn.
> ### * n.quantiles - Integer. Value between 3 and 10. Default is 0, meaning
> no
> ### quantile lines will be drawn.
> ### This is the number of bins into which you would like the variable
> ### separated. One line for each will be drawn, with the exception of
> ### the min and max, which are already drawn.
> ##########################################################
>
> graph.hh.wealth <- function(inpath, infile, outpath, outpref,
> log.plot=FALSE,
>    err.bar=TRUE, n.quantiles=0)
>
> {
>
> ##*************************************************
> ## Set the path and name of the input file
> ##  - The 'paste' command concatenates the two
> ##  - The 'skiplines' var sets the number of lines
> ##      to skip when reading in the dataset.
> ##*************************************************
> if (substr(inpath, nchar(inpath), nchar(inpath)) == "/")
> inpath <- substr(inpath, 0, nchar(inpath)-1)
> if (substr(outpath, nchar(outpath), nchar(outpath)) == "/")
> outpath <- substr(outpath, 0, nchar(outpath)-1)
> pathfile <- paste(inpath, infile, sep="/")
> skiplines <- 1
>
> ##*************************************************
> ## Set the names of the output file graphics
> ##*************************************************
> fnout.wlth <- "hhwealth.png"
> fnout.wlthss <- "hhwealth-sumstats.png"
> output.png.wlth <- paste(outpath, "/", outpref, fnout.wlth, sep="")
> output.png.wlthss <- paste(outpath, "/", outpref, fnout.wlthss, sep="")
>
> ##*************************************************
> ## Read in the household-level output dataset
> ##*************************************************
> hhstats <- read.delim(file=pathfile, header=TRUE, sep ="\t", dec=".",
> skip=skiplines)
>
> ##*************************************************
> ## Get some information from the household-level
> ## dataset for use in plotting.
> ##  - the village number
> ##  - a vector of the unique HH IDs
> ##  - the maximum number of model run years
> ##  - the maximum wealth among all households
> ##*************************************************
> villnum <- mean(hhstats$V84ID)
> uniq.hh.ids <- unique(hhstats$HHID00)
> max.yr <- max(hhstats$Year)
> max.wlth <- max(hhstats$Wealth)
>
> ##*************************************************
> ## Extract out the vars needed for the individual
> ## household line graph
> ## Vars: Year, HHID00, Wealth, Status
> ##*************************************************
> hh.wealth <- hhstats[,c("Year","HHID00","Wealth","Status")]
> hh.wealth.pos <- hh.wealth[hh.wealth$Wealth >= 0,]
> hh.wealth.plot <- hh.wealth.pos[hh.wealth.pos$Status == 0 |
> hh.wealth.pos$Status == 1,]
>
> ##*************************************************
> ## Get the summary statistics by year for Wealth
> ##*************************************************
> ## Get a list of unique years for iterating
> unqyrlist <- unique(hh.wealth.plot$Year)
>
> ## Determine the divisor for the quantiles
> div.q <- 1.0 / n.quantiles
>
> ## Loop through years to extract summary statistics
> for (yr in min(unqyrlist):max(unqyrlist)) {
> # Get records for current year
> this.yr <- hh.wealth.plot[hh.wealth.plot$Year == yr,]
> # Calc summary statistics by statistic
> this.n <- dim(this.yr)[1]
> this.min <- min(this.yr$Wealth)
> this.max <- max(this.yr$Wealth)
> this.mean <- mean(this.yr$Wealth)
> this.med <- median(this.yr$Wealth)
> this.sd <- sd(this.yr$Wealth)
> this.ebneg <- this.mean-this.sd
> this.ebpos <- this.mean+this.sd
> this.sumstats <- c(yr, this.n, this.min, this.max, this.mean,
> this.med, this.sd, this.ebneg, this.ebpos)
> # Convert vector to 1-row data frame
> this.df <- as.data.frame(t(as.matrix(this.sumstats)))
> colnames(this.df) <- c("Year", "Count", "Min", "Max", "Mean", "Median",
> "SD", "EB.Low", "EB.High")
> # Now, calculate the specified quantiles for this year
> names(yr) <- "Year"
> this.q <- as.data.frame(t(as.matrix(c(
> quantile(this.yr$Wealth, probs=seq(0, 1, div.q)), yr))))
> # Append the data frame to the output data frame
> if (yr == min(unqyrlist)) {
> hh.wealth.plot.ss <- this.df
> hh.wealth.quantiles <- this.q
> } else {
> hh.wealth.plot.ss <- rbind.data.frame(hh.wealth.plot.ss, this.df)
> hh.wealth.quantiles <- rbind.data.frame(hh.wealth.quantiles, this.q)
> }
> }
>
> ##*************************************************
> ## Set up parameters for quantile plots
> ##*************************************************
> q.names <- c("", "", "Terciles", "Quantiles", "Quintiles",
> "Sextiles", "Septiles", "Octiles", "Noniles", "Deciles")
> q.labs <- c("1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th")
> # Determine if median will be plotted
> if (n.quantiles %% 2) plot.med <- FALSE else plot.med <- TRUE
> # Determine which non-median quantile columns will be plotted
> if (n.quantiles %% 2) { # If the number of quantiles is odd
> cols.q <- c(2:n.quantiles)
> } else { # If the number of quantiles is even
> cols.q <- c(2:(n.quantiles/2), (2+(n.quantiles/2)):n.quantiles)
> col.med <- (n.quantiles/2) + 1
> }
>
> ##*************************************************
> ## Set up plotting parameters for the summary
> ## statistics graph
> ##*************************************************
> # Set y axis limits
> ylim.min <- min(hh.wealth.plot.ss)
> ylim.max <- max(hh.wealth.plot.ss)
> # Set title
> ttl.ss <- paste("Summary Statistics of Household Wealth Change Over Time\n
> (Village",
> villnum, ")", sep="")
> # Set legend based on arguments
> leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", "# of HHs")
> if (err.bars == TRUE & n.quantiles == 0) {
> leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", "# of HHs")
> leg.lty.ss <- c(1,2,4,5,0)
> leg.lwd.ss <- c(2,1,1,1,0)
> leg.pch.ss <- c(-1,-1,-1,-1,16)
> leg.col.ss <- c("black","darkolivegreen4","chartreuse4","red","blue")
> } else if (err.bars == FALSE & n.quantiles >= 3) {
> if (n.quantiles %% 2) { # Odd number of quantiles
> leg.txt.ss <- c("Mean", "Min", "Max", q.names[n.quantiles], "# of HHs")
> leg.lty.ss <- c(1,2,4,3,0)
> leg.lwd.ss <- c(2,1,1,1,0)
> leg.pch.ss <- c(-1,-1,-1,-1,16)
> leg.col.ss <- c("black","darkolivegreen4","chartreuse4","orange","blue")
> } else { # Even number of quantiles
> leg.txt.ss <- c("Mean", "Min", "Max", "Median", q.names[n.quantiles],
> "# of HHs")
> leg.lty.ss <- c(1,2,4,3,3,0)
> leg.lwd.ss <- c(2,1,1,2,1,0)
> leg.pch.ss <- c(-1,-1,-1,-1,-1,16)
> leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4", "orange",
> "orange", "blue")
> }
> } else if (err.bars == TRUE & n.quantiles >= 3) {
> if (n.quantiles %% 2) { # Odd number of quantiles
> leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", q.names[n.quantiles],
> "# of HHs")
> leg.lty.ss <- c(1,2,4,5,3,0)
> leg.lwd.ss <- c(2,1,1,1,1,0)
> leg.pch.ss <- c(-1,-1,-1,-1,-1,16)
> leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4", "red",
> "orange",
> "blue")
> } else { # Even number of quantiles
> leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", "Median",
> q.names[n.quantiles], "# of HHs")
> leg.lty.ss <- c(1,2,4,5,3,3,0)
> leg.lwd.ss <- c(2,1,1,1,2,1,0)
> leg.pch.ss <- c(-1,-1,-1,-1,-1,-1,16)
> leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4", "red",
> "orange",
> "orange","blue")
> }
> } else {
> leg.txt.ss <- c("Mean", "Min", "Max", "# of HHs")
> leg.lty.ss <- c(1,2,4,0)
> leg.lwd.ss <- c(2,1,1,0)
> leg.pch.ss <- c(-1,-1,-1,16)
> leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4", "blue")
> }
>
>
> ##*************************************************
> ## Set up vector of possible colors for the lines
> ##*************************************************
> colorset <- c(3:12, 26:62, 67:79, 81, 83:137, 139, 142:151)
>
> ##*************************************************
> ## Set up plotting parameters for the individual
> ## household graph
> ##*************************************************
> ## Create a vector of colors from the colorset
> ## above, one for every HHID00
> colors.id <- sample(colorset, length(uniq.hh.ids), replace=TRUE)
> ## Other parameters
> ttl.hh <- paste("Change in Household Wealth Over Time\n (Village",
> villnum, ")", sep="")
> leg.txt.hh <- c("Old Households", "Split Households")
>
> ##*************************************************
> ## Plot household-level summary statistics
> ##*************************************************
> png(filename=output.png.wlthss, width=10, height=7, units="in", res=300)
>
> ## Set initial plot with mean
> plot(hh.wealth.plot.ss$Year, # x var
> hh.wealth.plot.ss$Mean, # y var (MEAN)
> type="l", # line graph
> xlim=c(0,max.yr), # x-axis limits
> ylim=c(ylim.min, ylim.max), # y-axis limits
> lty=1, # solid line
> lwd=2, # line thickness
> xlab="",
> ylab="Wealth", # y-axis label
> main=ttl.ss)
> ## Add MIN
> lines(hh.wealth.plot.ss$Year, # x var
> hh.wealth.plot.ss$Min, # y var (MIN)
> type="l", # line graph
> col="darkolivegreen4",
> lty=2)
> ## Add MAX
> lines(hh.wealth.plot.ss$Year, # x var
> hh.wealth.plot.ss$Max, # y var (MAX)
> type="l", # line graph
> col="chartreuse4",
> lty=4)
>
> ` ## Add ERROR BARS if Argument 'err.bars' is TRUE
> if (err.bars == TRUE) {
> ## Add LOW ERROR BAR
> lines(hh.wealth.plot.ss$Year, # x var
> hh.wealth.plot.ss$EB.Low, # y var (EB LOW)
> type="l", # line graph
> col="red",
> lty=3)
> ## Add HIGH ERROR BAR
> lines(hh.wealth.plot.ss$Year, # x var
> hh.wealth.plot.ss$EB.High, # y var (EB HIGH)
> type="l", # line graph
> col="red",
> lty=3)
> }
> ## Add QUANTILES if Argument 'n.quantiles' is >= 3
> if (n.quantiles >= 3) {
> # Cycle through column numbers and draw quantile lines
> for (qcol in 1:length(cols.q)) {
> lines(hh.wealth.quantiles$Year, # Plot quantile lines
> hh.wealth.quantiles[,cols.q[qcol]],
> type="l",
> col="orange",
> lty=3)
> }
> # Add MEDIAN line
> if (plot.med == TRUE) {
> lines(hh.wealth.quantiles$Year, # Plot median
> hh.wealth.quantiles[,col.med],
> type="l",
> col="orange",
> lty=3,
> lwd=2)
> }
> }
> ## Add COUNT
> points(hh.wealth.plot.ss$Year, # x var
> hh.wealth.plot.ss$Count, # y var (COUNT)
> type="p", # line graph
> pch=16,
> col="blue",
> cex=0.5)
> ## Add LEGEND
> legend(x="topright",
> leg.txt.ss,
> lty=leg.lty.ss,
> lwd=leg.lwd.ss,
> pch=leg.pch.ss,
> col=leg.col.ss,
> cex=1)
>
> dev.off()
>
>
> ##*************************************************
> ## Plot wealth for individual households
> ##*************************************************
> png(filename=output.png.wlth, width=10, height=7, units="in", res=300)
>
> ## Create an empty plot
> if (log.plot == FALSE) {
> plot(hh.wealth.plot$Year, hh.wealth.plot$Wealth,
> type="n",
> xlim=c(0,max.yr),
> main=ttl.hh,
> xlab="",
> ylab="Wealth")
> } else {
> plot(hh.wealth.plot$Year, hh.wealth.plot$Wealth,
> log="y",
> type="n",
> xlim=c(0,max.yr),
> main=ttl.hh,
> xlab="",
> ylab="Wealth (log scale)")
> }
>
> #legend(x=leg.x.coord, y=leg.y.coord, # Sets the location for the legend
> legend(x="topright",
> leg.txt.hh, # text in the legent
> col=c("red", "red"), # sets the line colors in the legend
> lty=c(1,3), # draws lines
> lwd=c(1,1), # sets line thickness
> # bty="n", # no border on the legend
> ncol=2, # makes it a 2-column legend
> cex=0.8) # sets the legend text size
>
> ## Loop through IDs and add a line for each
> for (id in 1:length(uniq.hh.ids)) {
> ## Get the current HH ID
> this.id <- uniq.hh.ids[id]
>
> ## Extract the records for the current ID
> this.sub <- hh.wealth.plot[hh.wealth.plot$HHID00 == this.id,]
>
> if (dim(this.sub)[1] > 0) {
>
> ## Set line type
> if (mean(this.sub$Status) == 0) {
> ltype <- 1
> } else {
> ltype <- 3
> }
>
> ## Add the line for this ID
> lines(this.sub$Year, this.sub$Wealth,
> type="l",
> col= colors.id[id],
> lwd=1,
> lty=ltype)
> }
>
> }
>
> dev.off()
>
> }

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: error "variable names are limited to 256 bytes" when sourcing code

Joris FA Meys
I was about to say the same. loading the code line per line gave the error a
few lines after that one. Which explains...

Cheers
Joris

On Wed, May 26, 2010 at 9:27 PM, Erik Iverson <[hidden email]> wrote:

> Is the '`' character supposed to be there before the ## Add error bars
> comment?
>
> If that is the problem, let it be a good lesson    to use an editor with
> syntax highlighting. :)
>
>
>
>
> Brian Frizzelle wrote:
>
>> All,
>>
>> I think there may be some misunderstanding about my problem. In my code,
>> which is written as an R function, all of my variable names are short. I
>> get
>> that error when I try to source the function so I can call it. I just do
>> not
>> know why I'm getting the error, especially since I have written other very
>> similar functions that all work.
>>
>> So I have chosen to post the code below. I welcome any ideas about where
>> in
>> this code the error is occurring.
>>
>> ##########################################################
>> ###  SCRIPT: graph_hh_wealth_function.R
>> ###  DATE: April 22, 2010
>> ###  AUTHOR: Brian Frizzelle
>> ###                                 ###  This function draws two line
>> graphs of household-level wealth from a
>> variable ###  in a dataset output from an agent-based model run.
>> ###
>> ###  The two graphs are:
>> ###     1)      a line graph showing change in Wealth over time for each
>> individual
>> ###             household
>> ###   2)        a line graph of summary statistics of Wealth for the
>> households
>> (min,
>> ###             max, mean, and error bars)
>> ###
>> ###  Required Arguments:
>> ###   * inpath -        The path to the directory containing the
>> village-level
>> statistics
>> ###                     file
>> ###             NOTE: Use the UNIX forward slash (/) convention when
>> entering the path
>> ###                     and do not include a slash at the end of the path.
>> ###                     OK:             "D:/data"
>> ###                     Not OK:         "D:\data"
>> ###                     Not OK:         "D:/data/"
>> ###     * infile -      The name of the village-level statistics file
>> ###     * outpath -     The path to the directory where you want the
>> output graphics
>> to
>> ###                     be saved
>> ###     * outpref - The prefix that you want for the output PNG graphics
>> files
>> ###             NOTE: Do not include underscores (_) or spaces in the
>> output prefix.
>> ###                     Instead, please use dashes (-). Your prefix will
>> be separated
>> ###                     from the remainder of the file names by a dash.
>> ###
>> ###  Optional Arguments:
>> ###     * log.plot - Logical. Default is FALSE.
>> ###                     If TRUE, then the individual household wealth
>> graph is plotted
>> ###                     with a logarithmic Y-axis.
>> ###                     If FALSE, then the individual household wealth
>> graph is plotted
>> ###                     with a standard Y-axis.
>> ###     * err.bar -     Logical. Default is TRUE.
>> ###                     If TRUE, then error bars of 1 standard deviation
>> will be drawn
>> ###                     around the mean.
>> ###                     If FALSE, then no error bars will be drawn.
>> ###     * n.quantiles - Integer. Value between 3 and 10. Default is 0,
>> meaning
>> no
>> ###                     quantile lines will be drawn.
>> ###                     This is the number of bins into which you would
>> like the variable
>> ###                     separated. One line for each will be drawn, with
>> the exception of
>> ###                     the min and max, which are already drawn.
>> ##########################################################
>>
>> graph.hh.wealth <- function(inpath, infile, outpath, outpref,
>> log.plot=FALSE,                                     err.bar=TRUE,
>> n.quantiles=0)
>>
>> {
>>
>>        ##*************************************************
>>        ## Set the path and name of the input file
>>        ##  - The 'paste' command concatenates the two
>>        ##  - The 'skiplines' var sets the number of lines
>>        ##      to skip when reading in the dataset.
>>        ##*************************************************
>>        if (substr(inpath, nchar(inpath), nchar(inpath)) == "/")
>>                inpath <- substr(inpath, 0, nchar(inpath)-1)
>>        if (substr(outpath, nchar(outpath), nchar(outpath)) == "/")
>>                outpath <- substr(outpath, 0, nchar(outpath)-1)
>>        pathfile <- paste(inpath, infile, sep="/")
>>        skiplines <- 1
>>
>>        ##*************************************************
>>        ## Set the names of the output file graphics
>>        ##*************************************************
>>        fnout.wlth <- "hhwealth.png"
>>        fnout.wlthss <- "hhwealth-sumstats.png"
>>        output.png.wlth <- paste(outpath, "/", outpref, fnout.wlth, sep="")
>>        output.png.wlthss <- paste(outpath, "/", outpref, fnout.wlthss,
>> sep="")
>>
>>        ##*************************************************
>>        ## Read in the household-level output dataset
>>        ##*************************************************
>>        hhstats <- read.delim(file=pathfile, header=TRUE, sep ="\t",
>> dec=".",
>> skip=skiplines)
>>
>>        ##*************************************************
>>        ## Get some information from the household-level
>>        ## dataset for use in plotting.
>>        ##  - the village number
>>        ##  - a vector of the unique HH IDs
>>        ##  - the maximum number of model run years
>>        ##  - the maximum wealth among all households
>>        ##*************************************************
>>        villnum <- mean(hhstats$V84ID)
>>        uniq.hh.ids <- unique(hhstats$HHID00)
>>        max.yr <- max(hhstats$Year)
>>        max.wlth <- max(hhstats$Wealth)
>>
>>        ##*************************************************
>>        ## Extract out the vars needed for the individual
>>        ## household line graph
>>        ## Vars: Year, HHID00, Wealth, Status
>>        ##*************************************************
>>        hh.wealth <- hhstats[,c("Year","HHID00","Wealth","Status")]
>>        hh.wealth.pos <- hh.wealth[hh.wealth$Wealth >= 0,]
>>        hh.wealth.plot <- hh.wealth.pos[hh.wealth.pos$Status == 0 |
>> hh.wealth.pos$Status == 1,]
>>
>>        ##*************************************************
>>        ## Get the summary statistics by year for Wealth
>>        ##*************************************************
>>        ## Get a list of unique years for iterating
>>        unqyrlist <- unique(hh.wealth.plot$Year)
>>
>>        ## Determine the divisor for the quantiles
>>        div.q <- 1.0 / n.quantiles
>>
>>        ## Loop through years to extract summary statistics
>>        for (yr in min(unqyrlist):max(unqyrlist)) {
>>                # Get records for current year
>>                this.yr <- hh.wealth.plot[hh.wealth.plot$Year == yr,]
>>                # Calc summary statistics by statistic
>>                this.n <- dim(this.yr)[1]
>>                this.min <- min(this.yr$Wealth)
>>                this.max <- max(this.yr$Wealth)
>>                this.mean <- mean(this.yr$Wealth)
>>                this.med <- median(this.yr$Wealth)
>>                this.sd <- sd(this.yr$Wealth)
>>                this.ebneg <- this.mean-this.sd
>>                this.ebpos <- this.mean+this.sd
>>                this.sumstats <- c(yr, this.n, this.min, this.max,
>> this.mean,                                   this.med, this.sd,
>> this.ebneg, this.ebpos)
>>                # Convert vector to 1-row data frame
>>                this.df <- as.data.frame(t(as.matrix(this.sumstats)))
>>                colnames(this.df) <- c("Year", "Count", "Min", "Max",
>> "Mean", "Median",                                                 "SD",
>> "EB.Low", "EB.High")
>>                # Now, calculate the specified quantiles for this year
>>                names(yr) <- "Year"
>>                this.q <- as.data.frame(t(as.matrix(c(
>>      quantile(this.yr$Wealth, probs=seq(0, 1, div.q)), yr))))
>>                # Append the data frame to the output data frame
>>                if (yr == min(unqyrlist)) {
>>                        hh.wealth.plot.ss <- this.df
>>                        hh.wealth.quantiles <- this.q
>>                } else {
>>                        hh.wealth.plot.ss <-
>> rbind.data.frame(hh.wealth.plot.ss, this.df)
>>                        hh.wealth.quantiles <-
>> rbind.data.frame(hh.wealth.quantiles, this.q)
>>                }
>>        }
>>
>>        ##*************************************************
>>        ## Set up parameters for quantile plots
>>        ##*************************************************
>>        q.names <- c("", "", "Terciles", "Quantiles", "Quintiles",
>>                        "Sextiles", "Septiles", "Octiles", "Noniles",
>> "Deciles")
>>        q.labs <- c("1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th",
>> "9th")
>>        # Determine if median will be plotted
>>        if (n.quantiles %% 2) plot.med <- FALSE else plot.med <- TRUE
>>        # Determine which non-median quantile columns will be plotted
>>        if (n.quantiles %% 2) {                 # If the number of
>> quantiles is odd
>>                cols.q <- c(2:n.quantiles)
>>        } else {                                        # If the number of
>> quantiles is even
>>                cols.q <- c(2:(n.quantiles/2),
>> (2+(n.quantiles/2)):n.quantiles)
>>                col.med <- (n.quantiles/2) + 1
>> }
>>
>>        ##*************************************************
>>        ## Set up plotting parameters for the summary
>>        ## statistics graph
>>        ##*************************************************
>>        # Set y axis limits
>>        ylim.min <- min(hh.wealth.plot.ss)
>>        ylim.max <- max(hh.wealth.plot.ss)
>>        # Set title
>>        ttl.ss <- paste("Summary Statistics of Household Wealth Change Over
>> Time\n
>> (Village",              villnum, ")", sep="")
>>        # Set legend based on arguments
>>        leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", "# of HHs")
>>        if (err.bars == TRUE & n.quantiles == 0) {
>>                leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars", "# of
>> HHs")
>>                leg.lty.ss <- c(1,2,4,5,0)
>>                leg.lwd.ss <- c(2,1,1,1,0)
>>                leg.pch.ss <- c(-1,-1,-1,-1,16)
>>                leg.col.ss <-
>> c("black","darkolivegreen4","chartreuse4","red","blue")
>>        } else if (err.bars == FALSE & n.quantiles >= 3) {
>>                if (n.quantiles %% 2) {                         # Odd
>> number of quantiles
>>                        leg.txt.ss <- c("Mean", "Min", "Max",
>> q.names[n.quantiles], "# of HHs")
>>                        leg.lty.ss <- c(1,2,4,3,0)
>>                        leg.lwd.ss <- c(2,1,1,1,0)
>>                        leg.pch.ss <- c(-1,-1,-1,-1,16)
>>                        leg.col.ss <-
>> c("black","darkolivegreen4","chartreuse4","orange","blue")
>>                } else {                                                #
>> Even number of quantiles
>>                        leg.txt.ss <- c("Mean", "Min", "Max", "Median",
>> q.names[n.quantiles],                                           "# of HHs")
>>                        leg.lty.ss <- c(1,2,4,3,3,0)
>>                        leg.lwd.ss <- c(2,1,1,2,1,0)
>>                        leg.pch.ss <- c(-1,-1,-1,-1,-1,16)
>>                        leg.col.ss <- c("black", "darkolivegreen4",
>> "chartreuse4", "orange",
>>  "orange", "blue")
>>                }
>>        } else if (err.bars == TRUE & n.quantiles >= 3) {
>>                if (n.quantiles %% 2) {                         # Odd
>> number of quantiles
>>                        leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars",
>> q.names[n.quantiles],                                               "# of
>> HHs")
>>                        leg.lty.ss <- c(1,2,4,5,3,0)
>>                        leg.lwd.ss <- c(2,1,1,1,1,0)
>>                        leg.pch.ss <- c(-1,-1,-1,-1,-1,16)
>>                        leg.col.ss <- c("black", "darkolivegreen4",
>> "chartreuse4", "red",
>> "orange",
>>                                                "blue")
>>                } else {                                                #
>> Even number of quantiles
>>                        leg.txt.ss <- c("Mean", "Min", "Max", "Error Bars",
>> "Median",                                           q.names[n.quantiles], "#
>> of HHs")
>>                        leg.lty.ss <- c(1,2,4,5,3,3,0)
>>                        leg.lwd.ss <- c(2,1,1,1,2,1,0)
>>                        leg.pch.ss <- c(-1,-1,-1,-1,-1,-1,16)
>>                        leg.col.ss <- c("black", "darkolivegreen4",
>> "chartreuse4", "red",
>> "orange",
>>                                                "orange","blue")
>>                }
>>        } else {
>>                leg.txt.ss <- c("Mean", "Min", "Max", "# of HHs")
>>                leg.lty.ss <- c(1,2,4,0)
>>                leg.lwd.ss <- c(2,1,1,0)
>>                leg.pch.ss <- c(-1,-1,-1,16)
>>                leg.col.ss <- c("black", "darkolivegreen4", "chartreuse4",
>> "blue")
>>        }
>>
>>
>>        ##*************************************************
>>        ## Set up vector of possible colors for the lines
>>        ##*************************************************
>>        colorset <- c(3:12, 26:62, 67:79, 81, 83:137, 139, 142:151)
>>
>>        ##*************************************************
>>        ## Set up plotting parameters for the individual
>>        ## household graph
>>        ##*************************************************
>>        ## Create a vector of colors from the colorset
>>        ## above, one for every HHID00
>>        colors.id <- sample(colorset, length(uniq.hh.ids), replace=TRUE)
>>        ## Other parameters
>>        ttl.hh <- paste("Change in Household Wealth Over Time\n (Village",
>>              villnum, ")", sep="")
>>        leg.txt.hh <- c("Old Households", "Split Households")
>>
>>        ##*************************************************
>>        ## Plot household-level summary statistics
>>        ##*************************************************
>>        png(filename=output.png.wlthss, width=10, height=7, units="in",
>> res=300)
>>
>>        ## Set initial plot with mean
>>        plot(hh.wealth.plot.ss$Year,            # x var
>>                hh.wealth.plot.ss$Mean,         # y var (MEAN)
>>                type="l",                               # line graph
>>                xlim=c(0,max.yr),                       # x-axis limits
>>                ylim=c(ylim.min, ylim.max),     # y-axis limits
>>                lty=1,                          # solid line
>>                lwd=2,                          # line thickness
>>                xlab="",
>>                ylab="Wealth",                  # y-axis label
>>                main=ttl.ss)
>>        ## Add MIN
>>        lines(hh.wealth.plot.ss$Year,           # x var
>>                hh.wealth.plot.ss$Min,          # y var (MIN)
>>                type="l",                               # line graph
>>                col="darkolivegreen4",
>>                lty=2)
>>        ## Add MAX
>>        lines(hh.wealth.plot.ss$Year,           # x var
>>                hh.wealth.plot.ss$Max,          # y var (MAX)
>>                type="l",                               # line graph
>>                col="chartreuse4",
>>                lty=4)
>>
>> `       ## Add ERROR BARS if Argument 'err.bars' is TRUE
>>        if (err.bars == TRUE) {
>>                ## Add LOW ERROR BAR
>>                lines(hh.wealth.plot.ss$Year,                   # x var
>>                        hh.wealth.plot.ss$EB.Low,               # y var (EB
>> LOW)
>>                        type="l",                                       #
>> line graph
>>                        col="red",
>>                        lty=3)
>>                ## Add HIGH ERROR BAR
>>                lines(hh.wealth.plot.ss$Year,                   # x var
>>                        hh.wealth.plot.ss$EB.High,              # y var (EB
>> HIGH)
>>                        type="l",                                       #
>> line graph
>>                        col="red",
>>                        lty=3)
>>        }
>>        ## Add QUANTILES if Argument 'n.quantiles' is >= 3
>>        if (n.quantiles >= 3) {
>>                # Cycle through column numbers and draw quantile lines
>>                for (qcol in 1:length(cols.q)) {
>>                        lines(hh.wealth.quantiles$Year, # Plot quantile
>> lines
>>                                hh.wealth.quantiles[,cols.q[qcol]],
>>                                type="l",
>>                                col="orange",
>>                                lty=3)
>>                        }
>>                # Add MEDIAN line
>>                if (plot.med == TRUE) {
>>                        lines(hh.wealth.quantiles$Year, # Plot median
>>                                hh.wealth.quantiles[,col.med],
>>                                type="l",
>>                                col="orange",
>>                                lty=3,
>>                                lwd=2)
>>                }
>>        }
>>        ## Add COUNT
>>        points(hh.wealth.plot.ss$Year,  # x var
>>                hh.wealth.plot.ss$Count,        # y var (COUNT)
>>                type="p",                               # line graph
>>                pch=16,
>>                col="blue",
>>                cex=0.5)
>>        ## Add LEGEND
>>        legend(x="topright",
>>                leg.txt.ss,
>>                lty=leg.lty.ss,
>>                lwd=leg.lwd.ss,
>>                pch=leg.pch.ss,
>>                col=leg.col.ss,
>>                cex=1)
>>
>>        dev.off()
>>
>>
>>        ##*************************************************
>>        ## Plot wealth for individual households
>>        ##*************************************************
>>        png(filename=output.png.wlth, width=10, height=7, units="in",
>> res=300)
>>
>>        ## Create an empty plot
>>        if (log.plot == FALSE) {
>>                plot(hh.wealth.plot$Year, hh.wealth.plot$Wealth,
>>                        type="n",
>>                        xlim=c(0,max.yr),
>>                        main=ttl.hh,
>>                        xlab="",
>>                        ylab="Wealth")
>>        } else {
>>                plot(hh.wealth.plot$Year, hh.wealth.plot$Wealth,
>>                        log="y",
>>                        type="n",
>>                        xlim=c(0,max.yr),
>>                        main=ttl.hh,
>>                        xlab="",
>>                        ylab="Wealth (log scale)")
>>        }
>>
>>        #legend(x=leg.x.coord, y=leg.y.coord,   # Sets the location for the
>> legend
>>        legend(x="topright",
>>                leg.txt.hh,                             # text in the
>> legent
>>                col=c("red", "red"),                    # sets the line
>> colors in the legend
>>                lty=c(1,3),                             # draws lines
>>                lwd=c(1,1),                             # sets line
>> thickness
>>        #       bty="n",                                        # no border
>> on the legend
>>                ncol=2,                                         # makes it
>> a 2-column legend
>>                cex=0.8)                                        # sets the
>> legend text size
>>
>>        ## Loop through IDs and add a line for each
>>        for (id in 1:length(uniq.hh.ids)) {
>>                ## Get the current HH ID
>>                this.id <- uniq.hh.ids[id]
>>
>>                ## Extract the records for the current ID
>>                this.sub <- hh.wealth.plot[hh.wealth.plot$HHID00 ==
>> this.id,]
>>
>>                if (dim(this.sub)[1] > 0) {
>>
>>                        ## Set line type
>>                        if (mean(this.sub$Status) == 0) {
>>                                ltype <- 1
>>                        } else {
>>                                ltype <- 3
>>                        }
>>
>>                        ## Add the line for this ID
>>                        lines(this.sub$Year, this.sub$Wealth,
>>                                type="l",
>> col= colors.id[id],
>>                                lwd=1,
>>                                lty=ltype)
>>                }
>>
>>        }
>>
>>        dev.off()
>>
>> }
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Joris Meys
Statistical Consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

Coupure Links 653
B-9000 Gent

tel : +32 9 264 59 87
[hidden email]
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: error "variable names are limited to 256 bytes" when sourcing code

Brian Frizzelle
Well, that's embarrassing. Thank you for finding that.
Reply | Threaded
Open this post in threaded view
|

Re: error "variable names are limited to 256 bytes" when sourcing code

Duncan Murdoch-2
In reply to this post by Brian Frizzelle
Brian Frizzelle wrote:

> I've written a function that takes some input data output from a
> simulation model and creates some graphs. It's not very complicated
> code, and it works perfectly fine if I just run the code as is.
>
> But I have converted it into a function so we call it externally, and
> when I try to source the code to test the function, I get the error
> message "variable names are limited to 256 bytes". I've tried searching
> online for a solution to this, but everything I have come across deals
> with this error in relation to input data, not a function.
>
> The code is 389 lines long, so I'd rather not paste it here and clog up
> inboxes. If you have an idea as to why this is happening and would like
> to see the code, please email me and I will send it to you.
>  

Current versions of R will tell you which line contained the error.  
Can't you find and post just that one line?  If you aren't seeing an
error report, you could divide and conquer:  edit blocks out of your
file until you can figure out which lines matter.

My guess would be something involving backticks (i.e. things like `x`);
R will see the first one, and collect characters until the next one as a
single identifier.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.