Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I created a user defined function in R for checking whether a given number is prime or not. How can I make that user defined function as library function like avg or sd?
library(numbers)
isPrime(561)
# [1] FALSE
You will have to change the startup script. Use this site for more information. Customizing R:startup.
There is an Rprofile file that then refers to a .First() function. That user added functions to the .First function. I looked for it on my system and found an Rprofile file in the R folder, but did not find the latter.
I started going through the system files to do it myself. But not knowing exactly what I'm doing can cause more damage in the long run. Screwing something there may require reinstallation of the program.
Perhaps someone knows an easier way. Try it and report back, but I wouldn't recommend messing with system files unless you are very careful and the functions are absolutely necessary to your working environment startup. gl
Update
I found the Rprofile.site file in the R/etc directory. I added a .First() function. On opening RStudio in the next session, the packages I added loaded, but the function I put in it did not.
Update 2
I got it to work. I added this .First function to the Rprofile.site file in the etc folder.
.First <- function(){
library(dplyr)
library(stringr)
source('path/myfinctions.R')
}
And it loaded the packages and the functions that I have in the 'myfunctions.R' file. Thanks for asking this question as I have a place to save the functions that I want to define and load into R automatically. : )
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I was working on R studio, I had a project with 2 .R files.
One is app.R, the other one used to be called test.R, and app.R was able to invoke it's functions without adding anything else, however, I renamed test.R to calculations.R
Now app.R cannot find any of its functions. I already checked the working directory and they're both in the same locations
What can I do to make app.R detect calculations.R?
That sounds to me like you defined your functions in test.R, also ran it there and than started your app. Once you define a function and ran it, no matter what script they are in, this function is within your global environment. That means that you can start your app, without sourcing your test.R script. However, you should follow the advice of #duckmayr, because in programming you rather want to make everything explicit.
In order to only have functions which are defined within your app environment, consider using the following command in the beginning of your script:
rm(list = ls())
This command will clear your enviroment, so that no unneeded functions, packages, objects and the like are within it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am having trouble sourcing a file into my Rmd file.
I have sourced files before and can not figure out why this one won't work. I have my working directory set to the correct spot. I have a file called projects in that I have my "index.Rmd" file and then I have a folder "scripts" with includes "chart_1.R".
So my work flow would be projects -> scripts. In projects I have index.Rmd and in scripts I have chart_1.R
source("scripts/chart_1.R")
I should be able to access the chart_1 file but I am not able to.
It gives me the error "Error in file(file, "rt") : cannot open the connection"
2 solutions...
Preferred Solution
Have you tried the here package? It's made just for things like this, and it might do the trick. Your solution would look like this:
Install the package; it's on CRAN: install.packages("here")
If you're not using R projects, define what "here" means by adding a blank file called .here to the projects folder.
Use code like the following:
source(here::here("scripts", "chart_1.R"))
Or,
library(here)
source(here("scripts", "chart_1.R"))
Basically here works like file.path, in that it generates a filepath. But it always starts with your base directory, according to rules spelled out in the docs.
Maybe this is overkill for your particular problem, but it's a wonderful package and worth trying out. It avoids using absolute paths, which render your code less reproducible if someone else were ever to download the projects folder to check out what you did.
Workaround
The knitr function current_input can get you the location of the .Rmd file, at least when used with the option dir = TRUE. And basename will get get you from a filepath to the location of the containing folder. So you could try this as a workaround:
source(file.path(basename(knitr::current_input(dir = TRUE)), "scripts", "chart_1.R"))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I want to use a function from one script in another script but i either get an erorr or nothing is happend, depend on my code syntax. When i use the source("file_name) i get an erorr, and when i add the if(exists("function_name", mode = "function")) i get nothing..
hope you can help
have a good day
Or
In a different way you can choose your code script file using file.choose (avoiding problems related to the working directory) like this:
source(file.choose())
Terru_theTerror is absolutely right: it looks like there is something wrong with your source folder.
You may check the current name of your working directory with getwd() and check what contains this directory by dir(). If there your source file is placed elsewhere, your should change your current directory or to include the path to your source file by using source():
source_dir_name <- "D:/Work/Sources"
source_file_name <- "file_Name.R"
source_with_path <- paste(source_dir_name,"/", source_file_name, sep = "")
#
setwd(source_dir_name)
source(source_file_name)
# or
source(source_with_path)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to understand how Rcpp modules work. I tried the full example (on page 14) in the Rcpp vignette. However, I don't know where fx_vec is defined, therefore I am not able to run the full example.
Then, I tried to compile the package is Rcpp source code (here). I had to make the following changes to compile the package
I had to delete the zzz.R file in the R folder.
I had to comment the line 48 in stdVector.cpp (//.method( "resize", &vec::resize))
I am able to compile the testRcppModule package now (original source code here), however, I am still not able to run the program modules.R in the test folder. For reference, the package I have been to compile can be found here (note that the package names varies slightly from the original name in Rcpp).
The error I get on running code v <- new (vec) in modules.R is as follows
> library(testRcppmodule)
> v <- new(vec)
Error in .getClassFromCache(Class, where, resolve.msg = resolve.msg) :
object 'vec' not found
You may want to consider posting on rcpp-devel with a fuller example. What you have above is not really self-contained.
As for 'do Modules work' we offer a resounding Sure!! as testing them is
part of every unit test run, see the test script using this fully self-contained example package
many packages using Modules as eg my RcppRedis package, my RcppAnnoy package, my RcppCNPy package etc
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I was a bit surprised that this question hasn't been asked before. (If it has, apologies!) I am trying to find a good text editor with support for rmarkdown documents. Here are my criteria (in no particular order):
syntax highlighting (for both R code and markdown syntax)
ability to send code from document to console for evaluation
free & open source
autocompletion for bibtex entries
cross-platform
I can't seem to find anything that fits the bill. Sublime Text can do all of this, but it's neither free nor open source...
I know a lot of people use RStudio, but its support for actually writing is not very good at all (e.g. bad spellcheck, no support for navigating a file, etc)...
The answer to any question involving editors is of course ... Emacs. In all seriousness:
ESS for R
Polymode (by one of the ESS authors) for mixed mode
markdown mode by Jason Blevins (which is IIRC bundled with recent Emacs versions)
work well for me. I still go back to latex for 'real' papers with bibtex, but folks do this with markdown too so there will surely be a mode. And yes, it is cross-platform.
I use these on Ubuntu, and maintain the ESS package for Debian. So that one is always current. For polymode I just go to Github. It all works of course with MELPA and Emacs package archives.
Atom is free, extensible, and open source.
The only thing I can't get it to do is R code syntax inside a code chunk of an Rmd file (similar to Sublime).
If you have experience with Vim, you should check out Vim-R. I use it exclusively; it sends code to R, and has loads of other awesome features. In terms of syntax highlighting, I don't know if there is any specifically for rmarkdown, but there are vim plugins for markdown, and the syntax highlighting for R files is quite good with Vim-R.
I actually never use markdown in any form, but create .Rnw files and use knitr to create PDF's (via pdflatex). Vim-R will also send relevant knitr commands; e.g., if you want to knit pdf, you type (in vim) \kp. If you want to run it through biblatex, you type \kb.
EDIT: I'm not sure if there is autocompletion for bibtex entries, though. There probably is in vim-latex, but of course that isn't R related. However, you might be able to install both plugins and incorporate that command for your own uses.