Linking a R Script file and a R Markdown file

I have been stuck trying to call variables from my rscript file to my r markdown file for hours. They are in the global environment and I can see the data frames display in visual mode but it won't let me knit to html since it "cannot find the variable named".

On a similar note, when I try to call a variable inline with text
The number of goals scored isr goals_scored
it shows up as...
The number of goals scored is r goals_scored
with the only difference that the r code being surrounded by a rectangle.

Thanks for any help

RMarkdown runs in its own environment.

On the inline text, you might be missing backticks.

De global environment is only available when you run the code interactively but when you knit an Rmd document the code gets executed in a clean environment other than the one you are currently working on so the mentioned data frame doesn't exist there. You need to include the necessary code to import the data frame into memory, in your Rmd document itself, for example, if the data frame comes from a csv file you would include something like this: your_data_frame <- read.csv("path_to_file.csv").

Thank you so much for your quick response!

So the data frame I have is created through 300 lines of code in my r script file that also creates 20 more data frames. Is there a function I can call that will bring these data frames to my rmd file (since it's not in csv form)? Or do I need to move my 300 lines of code out of rscript and into a rmd?

Thank you very much

Yes, you could use the source() function to execute your R script from within your Rmd file.


@sevejimenez you can find an example of that in here:

And also if you want the script content as part of a chunk you could use the new file= option

You can even read a script in several chunk to use throughout a document

Hope it helps

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.