How to create "special" (source) file

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to create "special" (source) file

eesteves
Dear All,

I'm a Marine Biologist using R (in a most informal and applied way) for a couple of years. Actually, I've been using R to analyse the results supporting my thesis. I would like to put together (in the same file) the data (.txt files) and the scripts files (.R files) for each chapter. How can I do this?

Thanks in advance, Eduardo Esteves
        [[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
Reply | Threaded
Open this post in threaded view
|

Re: How to create "special" (source) file

Brian Ripley
On Mon, 2 Jan 2006, Eduardo Esteves wrote:

> Dear All,
>
> I'm a Marine Biologist using R (in a most informal and applied way) for
> a couple of years. Actually, I've been using R to analyse the results
> supporting my thesis. I would like to put together (in the same file)
> the data (.txt files) and the scripts files (.R files) for each chapter.
> How can I do this?

With some difficulty.  What I suggest would be a better idea is to read
in your data files and dump() then, then edit the dumps into the scripts.
However, you can do things like

## preamble
galaxies <- scan()
  9172  9350  9483  9558  9775 10227 10406 16084 16170 18419 18552 18600
18927 19052 19070 19330 19343 19349 19440 19473 19529 19541 19547 19663
19846 19856 19863 19914 19918 19973 19989 20166 20175 20179 20196 20215
20221 20415 20629 20795 20821 20846 20875 20986 21137 21492 21701 21814
21921 21960 22185 22209 22242 22249 22314 22374 22495 22746 22747 22888
22914 23206 23241 23263 23484 23538 23542 23666 23706 23711 24129 24285
24289 24366 24717 24990 25633 26690 26995 32065 32789 34279

hist(galaxies)

but you can't use source() on such a script.

Perhaps an even better idea is to put together a small package which has
your data in mypkg/data and the scripts in mypkg/inst/scripts.

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[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
Reply | Threaded
Open this post in threaded view
|

Re: How to create "special" (source) file

Gabor Grothendieck
In reply to this post by eesteves
1. You can use dput.  Suppose x is a data frame.  Then dput(x)
will output x as R code.   e.g.

> dput(head(iris))
structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
    Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4,
    1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2,
    0.2, 0.2, 0.4), Species = structure(c(1, 1, 1, 1, 1, 1), .Label =
c("setosa",
    "versicolor", "virginica"), class = "factor")), .Names = c("Sepal.Length",
"Sepal.Width", "Petal.Length", "Petal.Width", "Species"), row.names = c("1",
"2", "3", "4", "5", "6"), class = "data.frame")

You can now copy and paste iris.head <- ...the output you just got...
into your script, i.e. you can now put this into your script:

iris.head <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
    Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4,
    1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2,
    0.2, 0.2, 0.4), Species = structure(c(1, 1, 1, 1, 1, 1), .Label =
c("setosa",
    "versicolor", "virginica"), class = "factor")), .Names = c("Sepal.Length",
"Sepal.Width", "Petal.Length", "Petal.Width", "Species"), row.names = c("1",
"2", "3", "4", "5", "6"), class = "data.frame")


2. Another more readable way is to do this:

Lines <- "Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          5.1         3.5          1.4         0.2  setosa
          4.9         3.0          1.4         0.2  setosa
          4.7         3.2          1.3         0.2  setosa
          4.6         3.1          1.5         0.2  setosa
          5.0         3.6          1.4         0.2  setosa
          5.4         3.9          1.7         0.4  setosa"
iris.head <- read.table(textConnection(Lines), header = TRUE)


On 1/2/06, Eduardo Esteves <[hidden email]> wrote:
> Dear All,
>
> I'm a Marine Biologist using R (in a most informal and applied way) for a couple of years. Actually, I've been using R to analyse the results supporting my thesis. I would like to put together (in the same file) the data (.txt files) and the scripts files (.R files) for each chapter. How can I do this?
>
> Thanks in advance, Eduardo Esteves

______________________________________________
[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