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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:39:53+00:00 2026-06-06T15:39:53+00:00

I’m working on developing an R package, using devtools, testthat, and roxygen2. I have

  • 0

I’m working on developing an R package, using devtools, testthat, and roxygen2. I have a couple of data sets in the data folder (foo.txt and bar.csv).

My file structure looks like this:

/ mypackage
    / data
        * foo.txt, bar.csv
    / inst
        / tests
            * run-all.R, test_1.R
    / man
    / R

I’m pretty sure ‘foo’ and ‘bar’ are documented correctly:

    #' Foo data
    #'
    #' Sample foo data
    #'
    #' @name foo
    #' @docType data
    NULL
    #' Bar data
    #'
    #' Sample bar data
    #'
    #' @name bar
    #' @docType data
    NULL

I would like to use the data in ‘foo’ and ‘bar’ in my documentation examples and unit tests.

For example, I would like to use these data sets in my testthat tests by calling:

    data(foo)
    data(bar)
    expect_that(foo$col[1], equals(bar$col[1]))

And, I would like the examples in the documentation to look like this:

    #' @examples
    #' data(foo)
    #' functionThatUsesFoo(foo)

If I try to call data(foo) while developing the package, I get the error “data set ‘foo’ not found”. However, if I build the package, install it, and load it – then I can make the tests and examples work.

My current work-arounds are to not run the example:

    #' @examples
    #' \dontrun{data(foo)}
    #' \dontrun{functionThatUsesFoo(foo)}

And in the tests, pre-load the data using a path specific to my local computer:

    foo <- read.delim(pathToFoo, sep="\t", fill = TRUE, comment.char="#")
    bar <- read.delim(pathToBar, sep=";", fill = TRUE, comment.char="#"
    expect_that(foo$col[1], equals(bar$col[1]))

This does not seem ideal – especially since I’m collaborating with others – requiring all the collaborators to have the same full paths to ‘foo’ and ‘bar’. Plus, the examples in the documentation look like they can’t be run, even though once the package is installed, they can.

Any suggestions? Thanks much.

  • 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-06T15:39:55+00:00Added an answer on June 6, 2026 at 3:39 pm

    Importing non-RData files within examples/tests

    I found a solution to this problem by peering at the JSONIO package, which obviously needed to provide some examples of reading files other than those of the .RData variety.

    I got this to work in function-level examples, and satisfy both R CMD check mypackage as well as testthat::test_package().

    (1) Re-organize your package structure so that example data directory is within inst. At some point R CMD check mypackage told me to move non-RData data files to inst/extdata, so in this new structure, that is also renamed.

    / mypackage
        / inst
            / tests
                * run-all.R, test_1.R
            / extdata
                * foo.txt, bar.csv
        / man
        / R
        / tests
            * run-testthat-mypackage.R
    

    (2) (Optional) Add a top-level tests directory so that your new testthat tests are now also run during R CMD check mypackage.

    The run-testthat-mypackage.R script should have at minimum the following two lines:

    library("testthat")
    test_package("mypackage")
    

    Note that this is the part that allows testthat to be called during R CMD check mypackage, and not necessary otherwise. You should add testthat as a “Suggests:” dependency in your DESCRIPTION file as well.

    (3) Finally, the secret-sauce for specifying your within-package path:

    barfile <- system.file("extdata", "bar.csv", package="mypackage")
    bar <- read.csv(barfile)
    # remainder of example/test code here...
    

    If you look at the output of the system.file() command, it is returning the full system path to your package within the R framework. On Mac OS X this looks something like:

    "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/mypackage/extdata/bar.csv"
    

    The reason this seems okay to me is that you don’t hard code any path features other than those within your package, so this approach should be robust relative to other R installations on other systems.

    data() approach

    As for the data() semantics, as far as I can tell this is specific to R binary (.RData) files in the top-level data directory. So you can circumvent my example above by pre-importing the data files and saving them with the save() command into your data-directory. However, this assumes you only need to show an example in which the data is already loaded into R, as opposed to also reproducibly demonstrating the upstream process of importing the files.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.