Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:10:32+00:00 2026-06-17T15:10:32+00:00

I typically run a lot of simulations in R . In between simulations, some

  • 0

I typically run a lot of simulations in R.
In between simulations, some parts of the R
code would change. Typically, i keep alongside
the simulation results a .txt file containing
the definition of every function used in that
simulation. To make that .txt file, i simply
run this line:

for(j in 1:length(ls()))    print(c(ls()[j],eval(as.symbol(ls()[j]))))
out<-capture.output(for(j in 1:length(ls()))    print(c(ls()[j],eval(as.symbol(ls()[j])))))
cat(out,file=paste("essay_4_code.txt",sep=""),sep="\n",append=FALSE)

right after loading all the function in my env.
In the resulting text file however the R functions
are not in a format that R can interpret as functions.
To understand why, here is a simple example:

rm(list=ls())
foo1<-function(x){
  sin(x)+3
}
foo2<-function(x){
  cos(x)+1
}
foo3<-function(x){
  cos(x)+sin(x)
}

would yield:

[[1]]
[1] "foo1"

[[2]]
function (x) 
{
    sin(x) + 3
}

[[1]]
[1] "foo2"

[[2]]
function (x) 
{
    cos(x) + 1
}

[[1]]
[1] "foo3"

[[2]]
function (x) 
{
    cos(x) + sin(x)
}

So, in a nutshell, i would want to make essay_4_code.txt R-readable

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:10:33+00:00Added an answer on June 17, 2026 at 3:10 pm

    You could use

    dump(lsf.str(), file="essay_4_code.R")
    

    This will create an .R file with all of the function definitions in the current search space.

    edit:

    from the related question posted by @JoshuaUlrich in the comments:

    ...dump("f") will only save the function definition of f, and not its environment. 
    If you then source the resulting file, f will no longer work correctly [if it 
    depends on variables in the environment in was previously bound to].
    

    Alternatively, you can you use the save function to save the function in a binary format readable with the load function. This preserves your functions’ environment bindings, but you lose the ability to read the resulting file yourself.

     do.call(save, c(as.list(lsf.str()), file='essay_4_code.Rd'))
    

    Upon loading in a fresh session, functions previously bound to the global environment will be bound to the current global environment, while functions that were bound to a different environment will carry that environment with them.

    rm(list=ls())
    # function bound to non-global environment
    e <- new.env()
    e$x <- 10
    f <- function() x + 1
    environment(f) <- e
    # function bound to global environment
    y <- 20
    g <- function() y + 1
    # save functions
    do.call(save, c(as.list(lsf.str()), file='essay_4_code.Rd'))
    
    # fresh session
    rm(list=ls())
    
    load('essay_4_code.Rd')
    f()
    # [1] 11
    g()
    # Error in g() : object 'y' not found
    y <- 30
    g()
    # [1] 31
    ls()
    # [1] "f" "g" "y"
    

    And if you just wanted to inspect the bodies of the function in ‘eassay_4_code.Rd’:

    e<-new.env()
    load('essay_4_code.Rd', e)
    as.list(e)
    # $f
    # function () 
    # x + 1
    # <environment: 0x000000000a7b2148>
    # 
    # $g
    # function () 
    # y + 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the code below the output is DelegateDisplayIt, typically repeated 1-4 times.
I'm looking at some optimized, low level, cross platform, concurrency code designed to run
Typically we run javascript code to set any value: document.getElementById('id_name').value = ...; But I
So I typically run this command a lot: ssh -L 5901:myUser@computer.myHost.edu:5901 I use it
I typically run a script each night that updates my code from SVN, then
Typically when you run any program, during execution time what are different storages available
Typically in Django I can find out what queries are being run against the
I have a large library of existing powershell scripts, primarily to run some complicated
I have this massive query that I can typically run in under 2 minutes.
Would appreciate some insight isolating this, some semi-repeatable crashes in an iPhone app of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.