Rcpp modules - example programs not working [closed] - r

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

Related

Why won't source function work in R Markdown? [closed]

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 last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
More of a conceptual question than a strictly coding one. I have an .R script dedicated to importing and generally cleaning my data. I have several different .Rmd scripts that use the data from the general cleaning .R script to run their specific analyses. I would like to be able to source("DataCleaning.R") at the beginning of each of the .Rmd scripts, that way I could reduce redundancy, but I'm getting this error:
'Pulling' is not recognized as an internal or external command,
operable program or batch file.
I could fix this problem by exporting and importing a .csv, but I'm kind of confused why source() won't work. I've tried it on a few computers now. Works fine in .R but not .Rmd. Would have sworn I've used it in .Rmd in the past. I reread the documentation on it. I couldn't find anyone else reporting this exact error message, but I'm trying to find the file containing the code to take a closer look at 'Pulling'.
QUESTION:
Does source not work in .Rmd, or is this a unique case?
UPDATE:
I managed to get the problem to go away by uninstalling both R and R Studio and reinstalling them. Updating them did not work. The code for source looked fine to me. Still scratching my head a little at this one, because it worked in a regular R script but not R markdown, and was giving me an issue across two different machines, both of which were resolved when I did the full reinstall. I suppose it's fixed for now, but not sure what it could have been.
This question was closed for nonspecificity and not providing reproducible code, but, again, those standards can't really apply in this case because it was a machine-specific problem. I just needed to know whether I was misunderstanding the role of the function. Hopefully the update addresses the critiques, but obviously feel free to close it again if not.
Yes source does work in .Rmd. Here's a reproducible example to prove it:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
fname = tempfile()
writeLines('print("hello")', fname)
```
```{r}
source(fname)
```
The error you are getting must be caused by the content of your external file.

Renamed an R Source file from my R project. Now the other files can't find it [closed]

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.

How to view mplus plots in r

I'm working on a project using programs r and Mplus. On Macs, the Mplus program doesn't allow you to load plots directly but it interfaces with r to do so. Sadly, I've followed the directions provided by the Mplus website but can't seem to locate the "source code" file that they casually refer to. I've searched my computer and the internet for an r file called "mplus.R" to no avail. Does anyone have experience with this?
Here are the two sources from Mplus I've been following:
https://www.statmodel.com/mplus-R/
http://www.statmodel.com/mplus-R/Mplus%20R%20tutorial.pdf
I've loaded the BiocManager package which is required to load the rhdf5 package but there are no functions for mplus listed in that second link. The answer must be in the mplus.R source code but I can't find it anywhere. Any help is much appreciated.
I found the source code right after sending off that question. I won't delete it incase someone might find this useful.
The mplus.R code required to view plots can be find in the first link posted in the question. It's under the section titled "The File mplus.R" but it must be copied and pasted into your r console or script and ran.

having trouble sourcing a file in Rmd [closed]

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"))

User defined function to R library function like avg() [closed]

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. : )

Resources