In every Script I paste the same path to get some functions/r-scripts. Is there a possibility to save this path globally and permanently? It should be independently of the workingspace or other loading data paths and should be available on every new R-Session.
Thanks.
1) Place this in your .Rprofile file. The messsage line is optional but will allow you to confirm that .Rprofile was run when you start R.
message("Hello")
options("mypath" = "...path goes here...")
and then in your script get it using the following which will use that path if the mypath option was defined or the current directory otherwise. Omit the second argument if you don't want to have a default.
getOption("mypath", ".")
2) You could also just put this in your .Rprofile
mypath <- "...your path goes here..."
and then just refer to mypath in the script but that will cause mypath to clutter up your workspace which you may not want.
3) R also will read the environment variables defined in .Renviron so if that file contained:
MYPATH="...path goes here..."
then the script could get it via Sys.getenv("MYPATH") .
See ?Startup for more information on .Rprofile and .Renviron .
4) You could also create an exported shell variable on Linux or environment variable on Windows and then read it in the script using Sys.getenv .
Related
I am trying to provide a way of running unattended R scripts through Rscript. The problem I am having is that the default output for graphics is a PDF file in the current directory. I would like to redirect this output to a separate folder but without having to change the script too much.
If my script is something simple as:
args = commandArgs(trailingOnly = TRUE)
mydata <- get some data frame somehow
plot(tayside)
And I execute the following commandline:
Rscript.exe --vanilla --default-packages=RODBC,graphics,grDevices sample.R > C:\temp\sample.Rout
I get a Rplots.pdf in the current folder and the sample.Rout file in the C:\temp\ folder.
Is there a way to specify an output folder and have Rscript put all output there? I have tried playing with the pdf.options(...) to pre-pend a default folder to the file parameter but no can do.
Ok, apparently it was easier than I thought, no need to use pdf.options() but simply pdf() at the top of the file (after getting the arguments):
pdf(paste0(args[1], "MyFile.pdf"))
or, for multiple files:
pdf(paste0(args[1], "MyFile%03d.pdf"), onefile=FALSE)
I have a collection of functions in a file called some_functions.R and saved in a SVN directory in C:\blah1\blah2\Rcodes\some_functions.R . I have several Rprojects which uses this code file. Say a R project is available in the directory C:\blah1\blah2\Rprojects\project1. I can use hard coded path to refer the file and it works.
source("C:/blah1/blah2/Rcodes/some_functions.R")'
But I would like to set the path as environmental variable.
Looking at How to unfold user and environment variable in R language? and setting the home directory in windows R I add the following line in the RProfile.site file
Sys.setenv(R_CODE_PATH = "C:/blah1/blah2/Rcodes")
and in the project1.Rnw file
source("R_CODE_PATH/some_functions.R")
But the project file can not read the some_functions.R file. I tried with %R_CODE_PATH% without any luck.
Not sure what I'm missing here. Any help is much appreciated.
You retrieve environment variables using Sys.getenv(). Try:
r_code_path <- Sys.getenv("R_CODE_PATH")
Then, for example:
source(paste(r_code_path, "some_functions.R", sep = "/"))
I would use the .Renviron config file to define environment variables. Put it in whatever directory the R command Sys.getenv("HOME") returns and include lines like this:
R_CODE_PATH=C:/blah1/blah2/Rcodes
When I do this in RStudio or RGUI:
fix(SomeFunction)
(or using edit()) I can see the function's code in Notepad. Is there any way that I could change this so that the code preview opens in Notepad++ rather than plain old Notepad? And similarly, is there anyway that I could force View(SomeDataFrame) to open up in Excel?
fix and edit functions invoke the editor defined in the "editor" argument.
By default, that argument is set to getOption('editor') as shown in the edit function documentation.
Therefore, you can either pass the notepad++ path as function argument, i.e. :
path <- "C:\\Program Files (x86)\\Notepad++\\Notepad++.exe"
fix(somefunction,editor=path)
or set notepad++ as default editor by changin R options for the current session i.e. :
path <- "C:\\Program Files (x86)\\Notepad++\\Notepad++.exe"
options(editor=path)
# from now on, all calls to fix and edit will open notepad++ as default editor...
fix(somefunction)
N.B.
If you want to set the new option as the default for all the next sessions, you should edit Rprofile.site script in RHome\etc path as explained here.
You could try something like this to create a temporary .csv and open in Excel.
I want to be able to source() a file which includes a different file in its same directory, but I don't want to have to set the working directory from the R-prompt before running this file:
> getwd()
[1] "/Users/myser"
> source("/Users/myuser/workspace/myproject/myfile.r")
Inside /Users/myuser/workspace/myproject, there would be myfile.r and my-utils.r. myfile.r calls source('my-utils.r') from within it.
Other programming languages can determine the current file's path. Does R have something similar? Example:
cur_dir <- sys.get_current_file_path()
source(file.path(cur_dir, "my-utils.r"))
source("/Users/myuser/workspace/myproject/my-utils.r", chdir=TRUE)
When chdir option is set to true and the source file parameter is a full path, the directory of file will be used as the working directory while sourcing the file.
NOTE: cur_dir <- sys.get_current_file_path() doesn't make much sense because pathnames are not unique.
What is the correct method for enter data(d=read.table("WHAT GOES HERE IF YOU HAVE A MACBOOK ") if you have a mac computer?
Also what does the error code list below mean:
d=read.table(“Firststatex.notepad”,header=T)
Error: unexpected input in "d=read.table(‚"
Two usage errors:
You don't use data() to read in to R datasets held in external files. data() is an R function to load datasets that are built in to R and R packages. read.table("foo.txt") will return a data frame object from the file "foo.txt", which you can assign to an object within R using the assignment operator <-, e.g.
DF <- read.table("foo.txt")
As for "what goes here...", you need to supply a file system path from the current directory to the directory holding the file you want to read in. If the file "foo.txt" is in the current working directory, you can just provide the file name with extension as I did above. If the file is in another directory you need to supply the path to the file name and the file name, for example if the file "foo.txt" is located in the directory above the current directory, you would supply "../foo.txt". If it were in a directory myData located in the directory above the current directory you could us "../myData/foo.txt". So paths can be relative to the current directory. You can also use the fully qualified path on your file system hierarchy.
An alternative is to use the file.choose() function in place of the file name string. This will allow you to navigate to the file you wish to load interactively using a native file selection dialogue. This is what happens on Windows and I suspect also on Mac; not much different happens on Linux. For example:
DF <- read.table(file.choose())
You should probably look for specific help for your operating system if you are not familiar with how to specify file names and paths.
I get the same error when copying and pasting in the code you provide. The problem comes from the fact that you are using fancy, curly quotes “Firststatex.notepad” rather than one of the three sets of accepted quote marks: ` , ", and '; each of these is acceptable, i) "Firststatex.notepad", ii) 'Firststatex.notepad', and iii) `Firststatex.notepad` Just because the quotes you used look like quotes to you or I, these aren't quotes as far as most computer programs recognise. MS Word often inserts these quotes when you enter " for example, as do many other applications.