Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Read a csv in a vignette? #455

Open
ksedivyhaley opened this issue Dec 4, 2016 · 9 comments
Open

Read a csv in a vignette? #455

ksedivyhaley opened this issue Dec 4, 2016 · 9 comments

Comments

@ksedivyhaley
Copy link

I'd like to read data from a csv to produce an example for my vignette. I created a subdirectory called "data" in my vignette folder and then used read.csv("data/test.csv"). This worked while I was knitting a preview in RStudio, but then when I ran check() the check failed with

checking re-building of vignette outputs ... WARNING
Error in re-building vignettes:
...
Warning in file(file, "rt") :
cannot open file 'data/test.csv': No such file or directory
Quitting from lines 35-44 (katehelpr.Rmd)
Error: processing vignette 'katehelpr.Rmd' failed with diagnostics:
cannot open the connection
Execution halted

Is there a way to do this, or should I just re-type the csv data into a tibble in my vignette? It's not a very large data set, so it's not impossible to do so, just a bit clunky.

@samhinshaw
Copy link

Good question! My mind immediately jumps to the way datasets are included in packages. When your package is loaded, can you call the dataset directly (the way gapminder can be called after it is loaded)? If so, is it possible to call the dataset that way in the vignette? Otherwise, it may be possible to use system.file.

These may help:
http://kbroman.org/pkg_primer/pages/data.html
http://r-pkgs.had.co.nz/data.html

@jennybc
Copy link
Member

jennybc commented Dec 5, 2016

You are better off to place such data below inst and then use system.file(), as @samhinshaw has suggested.

Example here: https://github.com/jennybc/repurrrsive

You'll see how I make sure of the stuff below inst in actual functions in the package. If this package has a vignette, I could easily reference those files there.

This means the example will ship with your package but that sounds reasonable in this case.

@ksedivyhaley
Copy link
Author

ksedivyhaley commented Dec 5, 2016

Okay, I've put the files in inst/extdata and my vignette includes the following:

file1 = system.file("extdata", "test.csv", package = "katehelpr")

read.csv(file1) %>%
  <more code>

When I used build_vignettes(), it put my html file in inst/doc as expected, browse_vignettes() works, and the plot produced from file1 looks normal. However, I get the following warning in check():

checking re-building of vignette outputs ... WARNING
Error in re-building vignettes:
...
Warning in file(file, "rt") :
file("") only supports open = "w+" and open = "w+b": using the former
Quitting from lines 35-47 (katehelpr.Rmd)
Error: processing vignette 'katehelpr.Rmd' failed with diagnostics:
no lines available in input
Execution halted

Which, as I understand it, means that the system.file() call in the vignette isn't finding the file.

Given that the vignette appears to have been built normally, can I safely ignore this?

Semi-Related Question: I tried to use Tools > Project Options > Build Tools > Generate documentation with Roxygen > Configure > Use roxygen to generate vignettes but it doesn't appear to be working - there's no html or md left in vignettes/. This doesn't appear to be directly related to the system.file() issue because document() still isn't producing vignette files when the system.file() code is commented out. I'm not too worried about this since it build_vignettes() seems to be working but it would be nice to know what's going on if there's a simple answer.

@jennybc
Copy link
Member

jennybc commented Dec 5, 2016

I'm travelling today so can't do any little experiments to help answer.

Re: the check() warning. Are you 100% sure this arises from the use of the csv and system.file()? If you have to settle for this warning, that's OK. But this feels like something that can be sorted out. Above it feels like you've got some contradictory path information in where you say you've put the file and where you're retrieving it from. Do some print debugging, i.e. listing all files in the place you think you're looking.

Re: second question. It feels like the vignette workflow with devtools and roxygen and RStudio is somewhat in flux. So let this one be a mystery for today. I too would like to know what's optimal, but don't right now.

@ksedivyhaley
Copy link
Author

Pretty sure. The line references match and the warning disappears when I comment out those lines.

file("") also looks like what you would get if system.file() couldn't find what it was looking for and passed a blank string downstream.

In console:

system.file("extdata", "test.csv", package = "katehelpr")
[1] "C:/Users/7ks42/Desktop/Research/R/katehelpr/inst/extdata/test.csv"
> list.files("C:/Users/7ks42/Desktop/Research/R/katehelpr/inst/extdata/")
[1] "test.csv"  "test2.csv"

I also put the relevant code into a test file to see if it produces output. It passes when I run test() but not when I run check(), and I can't figure out how to print debugging code within check().

@samhinshaw
Copy link

Here's an example that might help you troubleshoot, Hadley's readr vignette. Specifically, we see he uses data <- read_csv(readr_example("mtcars.csv")). If we look into what readr_example is:

> readr_example
function (path) 
{
    system.file("extdata", path, package = "readr", mustWork = TRUE)
}
<environment: namespace:readr>

It could be that a separate function must be created so that it exists in your package's namespace, as system.file() would exist in <environment: namespace:base>, making its search path different.

@ksedivyhaley
Copy link
Author

It is still giving me a warning, it's just now system.file() throwing an error instead of returning a blank string (because of the mustWork argument).

@samhinshaw
Copy link

Can you try restarting R/Rstudio and then installing the package and loading it via library()?
Based off these suggestions:
yihui/recharts#8
rstudio/leaflet#52

@ksedivyhaley
Copy link
Author

Restarted R, tried library() and also Build & Reload.

I'm still getting the warning in check().

But all it's doing is preventing the automatic update of the vignette through check(). build_vignette() has been working fine.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants