I'm creating a package in Julia and have followed the Package Development section of the Docs.
One of my functions opens and reads in a data file (mydata.txt) that I'm storing in the package directory.
Everything works pretty great when I run the Julia from the package directory but not so great when I run the tests or run Julia from a different directory because it doesn't know where to find that data file.
I thought I could just do something like:
datapath = Pkg.dir("MyPkg") * "/data/"
to get an absolute path to the file but it still doesn't seem to work.
What is the correct way to provide an absolute file path for data in a package?
As of Julia 1.0, the answer by Imanol Luengo will produce a warning:
Warning: Pkg.dir(pkgname, paths...) is deprecated; instead, do import PackageName; joinpath(dirname(pathof(PackageName)), "..", paths...)"
so while it still works, it will stop working in a future version. The replacement suggested in the warning message seems to work:
joinpath(dirname(pathof(MyPkg)), "..", "data")
Inside the package you don't need the import.
In order to properly handle multi-platform directory files and paths, use Julia's built-in joinpath method:
joinpath(Pkg.dir("MyPkg"), "data", "mydata.txt")
The resulting path will be valid in every platform.
In Julia 1.7 and above, you can simply do:
datapath = pkgdir("MyPkg", "data")
mytxtpath = pkgdir("MyPkg", "data", "mydata.txt")
# etc. - any number of path components are allowed as further arguments
For earlier versions (from either 1.3 or 1.4 version), replace Pkg.dir in #ImanolLuengo's answer with pkgdir:
joinpath(pkgdir("MyPkg"), "data", "mydata.txt")
Related
Currently I am trialing a package that requires defining a path to where an external application is located. This application (for anyone curious) does not have a formal install and is simply a .app file to be placed in any given folder.
In R, I need to define a path to this file location, e.g.,
program_path <- ~/Desktop/Folder/dsi_studio.app
However, R interprets this as a folder, and thus when I try to run a few functions within the package, it claims the program_path I fed it is a directory and not a program/application.
Is there any way to force R to read this path as an application and not as a folder? I even went as far as defining program_path as the .app's Unix Executable File (i.e., dsi_studio.app/Contents/MacOS/dsi_studio), but no dice. I must be missing something here.
Thanks for any help!
With that code you might be getting a result that is a formula rather than a text/character object. Try putting quotation marks around that file path. The ~ is actually a function to create an R formula object. You also might need path.expand since the ~ in MacOS is a shortcut for /Users/user_name/. Try instead:
program_path <- "~/Desktop/Folder/dsi_studio.app"
Using python, if I need the absolute path from the context of the current running script all I need to do is to add the following in the code of that script:
import os
os.path.abspath(__file__)
This is very useful as having the absolute path I can then use os.path.join to form new absolute paths for my project components (inside the project directory tree) and more interesting is that everything will continue to work without any problem no matter where the package directory is moved.
I need to achieve the very same thing using R programming, that is obtaining the absolute path of the current running R script ( = the absolute path of its file on the disk). But trying to do the same in R turns out to be quite challenging, at least for me as a rather beginner in R.
After a lot of googling, I tried to use the reticulate package to call Python from R but __file__ is not available there, then I found a few threads on Stackoverflow suggesting to play with the running Stack and others suggesting the use of normalizePath. However none of these worked for me when the entire project package is transferred from one directory to another.
Therefore, I would like to know if for example you have the following file/directory tree
base_dir ( = /home/usr1/apps/R/base_dir)
|
|
|___ myscript.R (this is my R script to be run)
|___ data (this is a directory)
|___ sql (this is a directory)
Is there any solution allowing to add something in the code of myscript.R so that inside the script the program can always know that the base directory is /home/usr1/apps/R/base_dir and if later this base directory is moved to another directory then there is no need to change the code and the program would be able to find correctly the new base directory?
R has in general no way of finding this path, because there is no equivalent to Python’s __file__ in R.
The closest you can get is to look at commandArgs() and laboriously extract the script filename (which requires different handling depending on how the script was launched!). But this will fail if the script was executed in RStudio, and it will fail after calling setwd().
Other solutions (such as the ‘here’ package) rely on heuristics and specific project structures.
But luckily there’s actually a solution that will always work: use ‘box’ modules.
With modules, you’ll always be able to get the path of the current script/module via box::file(). This is the closest equivalent to Python’s __file__ you’ll get in R, and it always works — as long as you’re using ‘box’ modules consistently.
(Internally the ‘box’ package requires complex logic to determine the value of the file() function in all circumstances; I don’t recommend replicating it, it’s too complex. For the curious, the bulk of the relevant logic is in R/loaded.r.)
If you are running the script using Rscript you can use getwd().
#!/usr/bin/Rscript
getwd()
# or assign it to a variable
base_dir = getwd()
you can run it from the command line using one of the following
./yourscript.R
# or
Rscript yourscript.R
Note however, this only works if you run the script from inside the folder, the file is in.
cd ~
./script.R
# "/home/usr1"
cd /
/home/usr1/script.R
# "/"
For a more elaborate option you could consider https://stackoverflow.com/a/55322344/3250126
Please help - using R, how would I search for a specific file/folder on all drives (hard drives as well as attached USB drives)?
For example, I'm looking for a directory named "MyFiles", and it could be anywhere on my C:, or on my USB (E:). I'd like to know all the tree locations of the directory.
Thanks for any advice!
Messed up a bit in the comment as I misread the thread (you need dirs). You can still do this with list.files() tho. I mocked up a directory structure looking for directories named "data" but also included a file named "data":
(pre <- list.files("/var/tmp/a", "data", recursive=TRUE, full.names=TRUE, include.dirs=TRUE))
## [1] "/var/tmp/a/data" "/var/tmp/a/l/data" "/var/tmp/a/q/data"
(/var/tmp/a/l/data is actually just a file)
But, you only need/want directories, so if you have a fairly modern R install and the purrr package installed then you can do:
purrr::keep(pre, dir.exists)
## [1] "/var/tmp/a/data" "/var/tmp/a/q/data"
I see it's an old question, but now we can use the fs package that according to the tidyverse blog and the package vignette "provides a cross-platform, uniform interface to file system operations" and "smooth over some of the idiosyncrasies of file handling with base R functions".
Here's how we can accomplish this task with fs:
fs::dir_ls(path = c("C:/", "E:/"), type = "directory", glob = "*MyFiles", recurse = TRUE)
There are a couple of added advantages to using this approach:
If we have a general sense of where this directory should be in the folder hierarchy, we can recurse upto a certain level instead of searching the entire tree. We just need to add the argument recurse = #num_levels_to_recurse) to the above code.
Also, if we want to list all directories except MyFiles, we can add the argument invert = TRUE to the above code.
These two options are not available in list.files() and list.dirs() functions of base R. Check out this document for a thorough comparison between fs functions and base R functions for file system operations.
I want to find the location of the script .R files which are used for computation in R.
I know that by typing the object function, I will get the code which is running and then I can copy and edit and save it as a new script file and use that.
The reason for asking to find the foo.R file is
Curiosity
Know what is the algorithm used in the numerical computations
More immedietly, the function from stats package I am using, is running results for two of the arguments and not the others and have to figure out how to make it work.
Error shown by R implies that there might be some modification required in the script file.
I am looking for a more general answer, if its possible.
Edit: As per the comments so far, here is the code to compute spectrum of a time series using autoregressive methods. The data input is a univariate series.
x = ts(data)
spec.ar(x, method = "yule-walker") 1
spec.ar(x, method = "burg") 2
command 1 is running ok.
command 2 gives the following error.
Error in ar.burg.default(x, aic = aic, order.max = order.max, na.action = na.action, :
Burg's algorithm only implemented for univariate series
I did try specify all the arguments correctly like na.action=na.fail, order.max = NULL etc but the message is the same.
Kindly suggest possible solutions.
P.S. (This question is posted after searching the library folder where R is installed and zip files which come with packages, manuals, and opening .rdb, .rdx files)
See FAQ 7.40 How do I access the source code for a function?
In most cases, typing the name of the function will print its source
code. However, code is sometimes hidden in a namespace, or compiled.
For a complete overview on how to access source code, see Uwe Ligges
(2006), “Help Desk: Accessing the sources”, R News, 6/4, 43–45
(http://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf).
When R installs a package, it evaluates all the ".R" source files and re-saves them into a binary format for faster loading. Therefore you typically cannot easily find the source file.
As has been suggested elsewhere, you can simply type the function name and see the source code, or download the source package and find the source there.
library(plyr)
ddply # prints the source for ddply
# See the content of the R directory for plyr,
# but it's only binary files:
dir(file.path(find.package("plyr"), "R"))
# [1] "plyr" "plyr.rdb" "plyr.rdx"
# Get the source for the package:
download.packages("plyr", "~", type="source")
# ...then unpack and inspect the R directory...
.libPaths() should tell you all of your current library locations. It's possible to have more than one installation of a package if there are two libraries but only the one that is in the first library will be used. Unless you offer the code and the exact error message, it's not likely that anyone will be able to offer better advice.
I think you are asking to see what I call the source code for a function in a package. If so, the way I do it is as follows, which has worked successfully for me on the three times I have tried. I keep these instructions handy in a few places and just copied and pasted them here:
To see the source code for a function in Program R download the package containing the function. Specifically, download the file that ends in "tar.gz". This is a compressed file. Expand the compressed file using, for example, "WinZip". Now you need to open the uncompressed file that ends in ".tar". Download the free software "7-Zip". Click on the file "7zFM.exe" and navigate to the directory containing the ".tar" file. You can extract the contents of that ".tar" file into a new folder. The contents consist of R files showing the source code for the functions in the R package.
EDIT:
Today (July 8, 2012) I was able to open the 'tar.gz' file using the latest version of 'WinZIP' and could copy the contents (the source code) from there without having to use '7-Zip'.
EDIT:
Today (January 19, 2013) I viewed the source code for functions in base R by downloading the file
'R-2.15.2.tar.gz'
To download that file go to the http://cran.at.r-project.org/ webpage and click on that file in this line:
"The latest release (2012-10-26, Trick or Treat): R-2.15.2.tar.gz, read what's new in the latest version."
Unzip the file. WinZip will work, or it did for me. Then search your computer for readtable.r or another base R function.
agstudy noted here https://stackoverflow.com/questions/14417214/source-file-for-r-function that source code for read.csv is located in the file readtable.r, so do not expect every base R function to have its own file.
I'm creating an R package and I need it to include a couple of non R script files which get called by one of my functions. I need these script files to be distributed with the package, naturally. So that leaves me with two questions:
a) In which directory of the package
tree should I place these files? b) Is that location mandatory or just convention?
Do I need to change any other
settings or configurations or will
they just get copied to the
directory mentioned in #1 and then I
can figure out the path using
system.file()?
I've tried to find the answer in the Writing R Extensions document, but it didn't jump out at me. And, of course, I didn't read the whole thing. Am I being too honest here?
I think you want either exec/ at the top-level (even though that is labeled 'still experimental, or subdirectory of inst as everything in inst/ gets copied verbatim into the package.
A quick example from the packages I have expanded in source is gdata which has inst/perl, inst/xls and inst/bin. These you could then call from R itself by computing the path of the installed package using system.file().