Why won't source function work in R Markdown? [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 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.

Related

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.

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

Rcpp modules - example programs not working [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 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

Sink is full when calling rmarkdown::render

I'm following this short tutorial to print my R script directly as an HTML document. It should be pretty straightforward. With a few small changes to the header and comments of an otherwise normal R script, calling the command
rmarkdown::render('/Users/you/Documents/yourscript.R') at the end of an R script should call the knitr::spin function to go from my R script to an Rmd file to the final HTML or PDF.
I'm getting the error:
Error in sink(con, split = debug) : sink stack is full and I'm not sure what to do. All solutions I've found online point out that one needs to close sink()'s after opening them. But since I'm not really using sink() myself, I don't see how or where I should close them.
I'm using R 3.3.0.
This Question asks the same but is downvoted and has no answers.
Well, it turned out I was doing something stupid: I included the command rmarkdown::render('/Users/you/Documents/yourscript.R') within my script and forgot to comment it out. I probably ended doing an infinite recursion. I commented that line out and it worked beautifully.

knitr: Referring to and evaluating external code chunks

How could I refer to and evaluate (in an .Rmd file) specific chunks of R code, located not in a different .Rmd file, but in an R module, containing chunks of code, tagged with ## #knitr chunk_name? Thanks!
I just figured out what the problem was: I have simply forgotten to call read_chunk() function for the R module, containing those external code chunks. So far, everything appears to be working, with the exception, mentioned below.
One problem I'm currently experiencing (and this might be a good separate question, but I'll leave it as is for now) is that knitr doesn't seem to respect working directory and paths, constructed on its basis, using relative paths, such as file.path(getwd(), "data/transform"). I think this contradicts with knitr design, which allows code reuse via chunks in external R modules. What are approaches that people are using to solve this peculiar situation? I believe that it might be a good idea to submit as a feature request.

Resources