How obtain usual output from package I created? - r

I have an issue with the output of the functions from the package I created!
When I run a function from my package the results are visualized in a new window.
Output of my package that is impossible to copy
For information, in my code I use function "View" as usual.
There is a specif option to visualize outputs in the script bartool?
The following is the output I would like to have:
Usual R visualization
Thank you in advance,

I've created a package that contains only one function.
my_view <- function() {
df <- data.frame(x=1:5, y=11:15)
View(df)
}
After building it (ctrl + shift + b in RStudio) I've called the function and the dataframe is shown in new tab in RStudio.
What version of R and RStudio do you have? It is 3.6.3 and 1.2.5033 in my case. Could you try creating new package with only this function and check how the data is displayed?

Related

How to accurately use the epi.kappa() function in R?

I am attempting to perform a kappa statistic test on 2 indices I created. I've found that there are multiple packages in R that have functions for this and am trying to compare two functions: the epi.kappa() function in the epiR package and the cohen.kappa() function from the psych package.
I was able to successfully use the cohen.kappa() function with my data however, I continue to get errors when using the epi.kappa() function. My code is as follows
library(epiR)
kap.dat = matrix(c(275,78,305,154),nrow=2,byrow=TRUE)
colnames(kap.dat) = c("I1-0","I1-1")
rownames(kap.dat) = c("I2-0","I2-1")
epi.kappa(kap.dat,method="cohen",alternative= "two.sided",conf.level=0.95)
The error I get is:
Error in epi.kappa(kap.dat,method="cohen",alternative="two.sided", :
object `pO.p` not found
Check that you've got the latest version of epiR installed and update if necessary. Latest version on CRAN is 2.0.50. Type help(epi.about) once package loaded --- version of package will be shown at the bottom of the page.

BiodiversityR::accumcomp.long function not found

I am trying to use the function accumcomp.long in the Biodiversity.R package in order to adjust my species accumulation curves in ggplot.
However, calling the function accumcomp.long gives me an error, suggesting that this function perhaps does no longer exists.
accum.long1 <- accumcomp.long(Accum.1, ci=NA, label.freq=5)
Error in accumcomp.long(Accum.1, ci = NA, label.freq = 5) :
could not find function "accumcomp.long"
The function BiodiversityR::accumcomp.long
returns
Error: 'accumcomp.long' is not an exported object from 'namespace:BiodiversityR'
I am not certain whether this is an issue with the package, the way the package is loaded into my R space, or perhaps unrelated to the package simply the format of my data.
My community and environment files are of dataframe class. I have tried loading them as .csv files as well as .rda files, but the error remains.
??accumcomp.long
renders "No results found".
I am using R version 3.5.3 (2019-03-11)
Any suggestions as to what else I could try?
Thanks in advance!
I re-installed the package BiodiversityR and included force = T (as per this thread https://community.rstudio.com/t/could-not-find-function-error-even-though-fun-is-in-namespace-and-has-an-rd-file/91243). I then reloaded the package. I am not sure what force = T does, but the accumcomp.long function now works.

R:"Note: no visible binding for global variable" after devtools:use_data

I want to make a dataframe available to users of a package, as well as to functions of the package.
Starting from a new package I've used
devtools::use_data_raw("my_data")
This creates the file data_raw/my_data.R, which I edit as
my_data <- data.frame(x = runif(3), y = runif(3))
devtools::use_data(my_data, overwrite = TRUE)
After running the code above, a file data/my_data.Rda is created.
According to Hadley Wickham's R Packages every file under data/ is exported, and if I try
load_all()
my_data
I can see that this is the case. However if I now try to use the my_data dataframe inside a function in the package, say like R/test_my_data.R
test_my_data <- function {
my_data
}
and then I run
devtools::check()
I get the infamous no visible binding for global variable my_data NOTE.
I appreciate there are many questions already on this topic but many are related to the cases where a tidy evaluation function is used, or data from another package is referred to. Why is R CMD check failing on the example above, and what's the correct way of sorting this out?
I'm aware that the
utils::globalVariables("my_data")
solution will avoid this NOTE, but I'd like to know if there's a proper way of informing R CMD check that my_data actually exists.

Why does using "<<-" in a function in the global workspace work, but not in a package?

I'm creating a package using devtools and roxygen2 (in RStudio), however after I've built the package my function no longer works as intended. Yet, if I load the function's .R file and run the function from there in RStudio, it works perfectly. I've created another package using this method before and it worked fine (13 functions all working as intended from my other package), yet I cant seem to get this new one to work.
To start creating the package I start with:
library("devtools")
devtools::install_github("klutometis/roxygen")
library(roxygen2)
setwd("my parent directory")
create("triale")
All is working fine so far. So I put my .R file containing my function in the R folder under the triale folder. The .R file looks like this:
#' Trial Z Function
#'
#' This function counts the values in the columns
#' #param x is the number
#' #keywords x
#' #export
#' #examples
#' trialz()
trialz = function(x) {w_id= c(25,x,25,25,25,1,1,1,1,1);
wcenter= c(rep("BYSTAR-1",10));
df1 <<- data.frame(w_id, wcenter);
countit <<- data.table(df1);
view <<- countit[, .N, by = list(w_id, wcenter)];
View(view)}
Again if I were to just run the code from the .R file, and test the function it works fine. But to continue, next I enter:
setwd("./triale")
document()
The triale documentation is updated, triale is loaded, and the NAMESPACE and trialz.Rd are both written so that trialz.Rd is under the man folder, and NAMESPACE is under the triale folder as intended. Next I install triale:
setwd("..")
install("triale")
Which I know works because I get the following:
Installing triale
"C:/PROGRA~1/R/R-31~1.3/bin/x64/R" --vanilla CMD INSTALL \
"C:/Users/grice/Documents/R/triale" \
--library="C:/Users/grice/Documents/R/win-library/3.1" --install-tests
* installing *source* package 'triale' ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
*** arch - x64
* DONE (triale)
Reloading installed triale
Package is now built, so I do the following:
library("triale")
library("data.table")
Note whenever I load the package data.table I get the following error message:
data.table 1.9.4 For help type: ?data.table
*** NB: by=.EACHI is now explicit. See README to restore previous behaviour.
However it doesnt seem to affect my function. So now its time to test my function from my package:
trialz(25)
This goes through, and I of course get a populated df1, and countit, but for whatever reason view is always empty (as in 0 obs. of 0 variables).
So I test my work using the dummy code below:
>trialy = function(x) {wid= c(25,x,25,25,25,1,1,1,1,1);
wc= c(rep("BYSTAR-1",10));
df2 <<- data.frame(wid, wc);
countitt <<- data.table(df2);
viewer <<- countitt[, .N, by = list(wid, wc)];
View(viewer)}
>trialy(25)
Even though this is the same exact code with just the names changed around it works. Dumbfounded I open trialz.R and copy the function from there and run it as below, and that works:
> trialz = function(x) {w_id= c(25,x,25,25,25,1,1,1,1,1);
wcenter= c(rep("BYSTAR-1",10));
df1 <<- data.frame(w_id, wcenter);
countit <<- data.table(df1);
view <<- countit[, .N, by = list(w_id, wcenter)];
View(view)}
> trialz(25)
Since I've created a package before I know my method is solid (that package had 13 dif. functions, all of which worked). I just don't understand how a function can work fine as written, yet when I package it, the function no longer works.
Again here is where it stops working as intended when using my package:
view <<- countit[, .N, by = list(w_id, wcenter)];
View(view)}
And my end result should look something like this, if my package worked:
wid wc N
1 25 BYSTAR-1 5
2 1 BYSTAR-1 5
Can anyone explain why view is never populated after I package my function? I've tested it as much as I know how, and my results should be reproducible for anyone thats willing to try it for themselves.
Thanks, I appreciate any feedback.
Your problem here is that "<<-" does not create variables in the global environment but rather in the parent environment. (See help("<<-").)
The parent environment of a function is the environment in which it has been defined. In the case where you defined your function directly in your workspace, this parent environment actually is the same as your workspace environment (namely: .GlobalEnv), which is why your variables are assigned values as you expect them to. In the case where your function is packaged, however, the parent environment is the package environment and not the .GlobalEnv! This is why you do not see your variables being assigned values in your workspace.
Refer to the chapter on environments in Hadley's book and How R Searches and Finds Stuff for more details on environments in R.
Note that doing this would not be considered a proper debugging technique, to say the least. In general, you never want to use the "<<-" operator.
For options on debugging R code, see, e.g., this question. I, in particular, like the debugonce function very well. See ?debugonce.
I forgot one important part when editing my description file in that I for got to add
Imports: data.table
Also the NAMESPACE file needed to include the data.table package as an import as well, like so:
import(data.table)
export(Z)
export(AS) .... etc.
Doing this ensures that whenever a function within your package uses a function from another package, that (second) package is called up before your code is executed.

Group_by function not working with R markdown

I'm trying to create a pdf from an R markdown script which says this:
test <- group_by(trials, SubjID)
number <- summarise(test, nsubj=n())
sum(number$nsubj != 12)
but when I click on Knitpdf I get the following error:
error in eval(expr,envir,enclos): could not find function "group_by" Calls:
<Anonymous>...handle->withCallingHandlers->withVisible->eval->eval Execution halted
I have dplyr installed and it works when I send the information to the console but not when I press knitPDF.
You need to add below library import at top in R markdown-
library(dplyr)

Resources