I have a script in .First(). I saved it to .RData and put it in a folder to which I point in a Windows shortcut of Rterm.exe 64-bit (like described here). When I open the shortcut I get the error "could not find function..", even for base functions..
like so:
.First <- function() {
plot(rnorm(100))
}
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252
[3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C
[5] LC_TIME=German_Austria.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_2.15.0
Files with .RData extenstions are often assigned as data rather than source files. The problem however is that the .First scripts can only use functions in the base package unlees the other packages are first loaded.
.First <- function() { require(graphics)
plot(rnorm(100))
}
?Startup
Personally, I would name this with a .R extension.
Related
My custom package created with devtools installs, loads, displays help files for the functions, but does not provide the functions. This solution does not seem to be relevant, as my package is installed in .libPaths().
# from the parent directory of the created package
install.packages("mypkg", repos = NULL, type = "source")
# ...
# * DONE (mypkg)
require(mypkg)
# Loading required package: mypkg
?my.fun # displays the function help documentation correctly
my.fun()
Error: could not find function "my.fun"
What causes this behaviour and how to fix it?
sessionInfo()
#R version 3.3.0 (2016-05-03)
#Platform: x86_64-apple-darwin13.4.0 (64-bit)
#Running under: OS X 10.13.1 (unknown)
#locale:
#[1] cs_CZ.UTF-8/cs_CZ.UTF-8/cs_CZ.UTF-8/C/cs_CZ.UTF-8/cs_CZ.UTF-8
#attached base packages:
#[1] stats graphics grDevices utils datasets methods base
#other attached packages:
#[1] mypkg_0.1 devtools_1.13.4
#loaded via a namespace (and not attached):
#[1] tools_3.3.0 withr_2.1.0 memoise_1.0.0 git2r_0.19.0 digest_0.6.9
Did you export the function? To check if it's in the package run:
mypkg:::my.fun()
I'm trying to force myself to do more unit testing on some data analysis projects where I don't really want to develop a package. So, I've been playing with the testthat R package. I have a code folder and inside it is a src and a test folder. In the src folder I have the file add.R that has this function:
add <- function(x,y){
x+y
}
In the test folder I have the file test-add.R that contains this:
library(testthat)
test_that("adding numbers", {
expect_equal(add(2,3), 5)
expect_equal(add(5,5), 10)
})
The following works fine...
> source('code/src/add.R')
> test_file('code/test/test-add.R')
But I'd like to be able to use the auto_test function since as the project grows source/test_file will get tedious. But when I try auto_test I get this...
> auto_test('code/src', 'code/test')
Error in find_reporter(reporter) : attempt to apply non-function
I'm sure I'm missing something simple, but what?
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.1 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] testthat_0.11.0.9000 knitr_1.12.3
loaded via a namespace (and not attached):
[1] lazyeval_0.1.10 R6_2.1.2 tools_3.2.3
[4] withr_1.0.1 memoise_1.0.0 crayon_1.3.1
[7] digest_0.6.9 devtools_1.10.0.9000
It seems that this is a problem of testthat version 0.11.0.9000. If I install this version from github, I get the same error message as you do:
auto_test('code/src', 'code/test')
## Error in find_reporter(reporter) : attempt to apply non-function
But with version 0.11.0 that can be installed from CRAN, your example runs fine:
auto_test('code/src', 'code/test')
## ..
## DONE
We have a problem using RMarkdown on multiple operating systems.
Initially, an .Rmd file is created on a Linux system (Ubuntu 12.04 LTS) and then pushed to a GitHub repo.
It can be compiled ("knitted") without problems on this system.
It is then pulled on a Windows 7 machine with RStudio installed.
There, when trying to compile, the following error shows up:
Error in yaml::yaml.load(front_matter) :
Reader error: invalid leading UTF-8 octet: #FC at 66
Calls: <Anonymous> -> parse_yaml_front_matter -> <Anonymous> -> .Call
Execution halted
When creating another .Rmd file on the Windows system, it works flawlessly.
When creating another .Rmd file on the Windows system, and copying everything but the first few lines of the "problematic" file to the other .Rmd file, and compiling this file, it works flawlessly.
I compared both files in HEX (in Sublime) on both operating systems: They are EXACTLY the same.
Has somebody else seen that error before?
Update: It seems as if a German Umlaut ("ü") is causing the problem, as its UTF-8 "Escaped Unicode" is \uFC, according to http://www.endmemo.com/unicode/unicodeconverter.php
In general, it seems that Unicode is not correctly recognized by either R, RStudio or knitr on Windows. When I type in some Umlauts in a new .Rmd file, and knit it, I get output such as "öää". In RStudio > Tools > Global options, I set the Default text encoding to "UTF-8". And I also did that for R, in the RProfile.site file (options(encoding="UTF-8")).
Update 2: library(rmarkdown); sessionInfo() gives
R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252
[4] LC_NUMERIC=C LC_TIME=German_Switzerland.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rmarkdown_0.4.2
loaded via a namespace (and not attached):
[1] digest_0.6.8 htmltools_0.2.6 tools_3.1.2
on Windows 7, whereas, on Ubuntu, it is:
R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rmarkdown_0.3.10
loaded via a namespace (and not attached):
[1] digest_0.6.8 htmltools_0.2.6 tools_3.1.2
I already suspect the problem to be the diverging locale... how do I fix this?
I am extremely late to this, but I solved the issue by changing the options encoding back to "native":
options(encoding="native")
And changing the default windows encoding to UTF-8 (which opened the pandora box of a non-negligible number of other issues related to the encoding of other programs; so, treat with caution).
While working on a new project in R, I wrote the following code:
sp500 <- get.hist.quote("^GSPC",start=(today <- Sys.Date())-735,quote="Cl")
lsp500 <- log(sp500)
rlsp500 <- diff(lsp500)
The problem is the diff() function, it produces the following error:
Error in MATCH(x, x) : could not find function "MATCH"
All other code executes without problems. I'm using RStudio and R version 2.15.2 (2012-10-26) -- "Trick or Treat" on Mac OSX 10.8.2.
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tseries_0.10-30
loaded via a namespace (and not attached):
[1] grid_2.15.2 lattice_0.20-10 quadprog_1.5-4 tools_2.15.2 zoo_1.7-9
What am I missing?
tseries::get.hist.quote returns a zoo object by default, but the tseries package doesn't attach zoo, so zoo::MATCH isn't found. I assume zoo::MATCH is used in diff.zoo or one of the functions called by it.
Attaching zoo (via library(zoo)) will fix the problem.
During the installation process I have set R to not restore the workspace and now want to undo this -
how is it done?
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252
[3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C
[5] LC_TIME=German_Austria.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
If you are using RGui from a windows shortcut, edit the short cut to remove the --no-restore flag.
If you are using R through, Rstudio, look under Tools-->Options-->General and select the appropriate option
For other IDEs there should be similar ways to set start-up options.
The other option would be to reinstall R the way you would like it to be done.