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 8459359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:18:35+00:00 2026-06-10T13:18:35+00:00

My R workflow is usually such that I have a file open into which

  • 0

My R workflow is usually such that I have a file open into which I type R commands, and I’d like to execute those commands in a separately opened R shell.

The easiest way of doing this is to say source('the-file.r') inside R. However, this always reloads the whole file which may take considerable time if big amounts of data are processed. It also requires me to specify the filename again.

Ideally, I’d like to source only a specific line (or lines) from the file (I’m working on a terminal where copy&paste doesn’t work).

source doesn’t seem to offer this functionality. Is there another way of achieving this?

  • 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-10T13:18:37+00:00Added an answer on June 10, 2026 at 1:18 pm

    Using the right tool for the job …

    As discussed in the comments, the real solution is to use an IDE that allows sourcing specific parts of a file. There are many existing solutions:

    • For NeoVim, there’s R.nvim.

    • For Emacs, there’s ESS.

    • And of course there’s the excellent stand-alone RStudio IDE.

    As a special point of note, all of the above solutions work both locally and on a server (accessed via an SSH connection, say). R can even be run on an HPC cluster — it can still communicate with the IDEs if set up properly.

    … or … not.

    If, for whatever reason, none of the solutions above work, here’s a small module[gist] that can do the job. I generally don’t recommend using it, though.1

    #' (Re-)source parts of a file
    #'
    #' \code{rs} loads, parses and executes parts of a file as if entered into the R
    #' console directly (but without implicit echoing).
    #'
    #' @param filename character string of the filename to read from. If missing,
    #' use the last-read filename.
    #' @param from first line to parse.
    #' @param to last line to parse.
    #' @return the value of the last evaluated expression in the source file.
    #'
    #' @details If both \code{from} and \code{to} are missing, the default is to
    #' read the whole file.
    rs = local({
        last_file = NULL
    
        function (filename, from, to = if (missing(from)) -1 else from) {
            if (missing(filename)) filename = last_file
    
            stopifnot(! is.null(filename))
            stopifnot(is.character(filename))
            
            force(to)
            if (missing(from)) from = 1
    
            source_lines = scan(filename, what = character(), sep = '\n',
                                skip = from - 1, n = to - from + 1,
                                encoding = 'UTF-8', quiet = TRUE)
            result = withVisible(eval.parent(parse(text = source_lines)))
        
            last_file <<- filename # Only save filename once successfully sourced.
            if (result$visible) result$value else invisible(result$value)
        }
    })
    

    Usage example:

    # Source the whole file:
    rs('some_file.r')
    # Re-soure everything (same file):
    rs()
    # Re-source just the fifth line:
    rs(from = 5)
    # Re-source lines 5–10
    rs(from = 5, to = 10)
    # Re-source everything up until line 7:
    rs(to = 7)
    

    1 Funny story: I recently found myself on a cluster with a messed-up configuration that made it impossible to install the required software, but desperately needing to debug an R workflow due to a looming deadline. I literally had no choice but to copy and paste lines of R code into the console manually. This is a situation in which the above might come in handy. And yes, that actually happened.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some Workflow Foundation 4 ActivityDesigners that I'd like to have interact with
My workflow usually involves me making multiple changes to a file, each of which
I'm currently looking at some light-weight SQL abstraction modules. My workflow is such that
I have the business logic of an workflow-like application in a C# class library,
Lets say I have a sample workflow that does the following: Receive something Run
I have a workflow that has a 'Prompt and Response' dialogue. I can run
Environment: Workflow Foundation 4, SQL persistence store. Requirement: I have a bookmark which has
This workflow is a xamlx file in an asp.net application. I have a CodeActivity
The workflow requested by customer is something like this : The user on registration
The workflow for my Django project should go like this: User specifies parameters for

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.