How to include an external file in a Moodle question with R/exams? - r

In order to include statistical tables when using R-exams, I know that one can just use the option pages inside the function exams2nops(). But when using exams2moodle() how should one proceed?
In Moodle one can upload a file within a question and add a link to the embedded file. Is it possible to do it through R exams?

You can easily include various kinds of supplementary files in R/exams and then export them to Moodle or other learning management systems. Two steps need to be taken:
While processing the .Rmd or .Rnw exercise the supplementary file(s) need to be created in the current working directory (which is a temporary directory by default). This can either be done be creating the file via some R code or by copying an existing file. The convenience function include_supplement() simplifies this.
The supplementary file then needs to be included as a link in the question text. In .Rmd this would be something like [myfile.pdf](myfile.pdf) and in .Rnw exercises \url{myfile.pdf}.
An example for such an inclusion is the lm exercise template shipped along with the package, see: http://www.R-exams.org/templates/lm/. This exercise creates a .csv file on the fly within R and then includes it.
An example for the include_supplement() function is available in the Rlogo exercise template that copies the R logo image from the R system and then includes it in the exercise. See: http://www.R-exams.org/templates/Rlogo/.
Final comment: For a distribution table it would also be possible to include this directly as an HTML table in Moodle. For example, you could generate suitable Markdown or LaTeX code within the R code of the exercise.

Related

How can I call any arbitrary object generated at any arbitrary point of one Rmarkdown file in another Rmarkdown file?

To preface this, I think my question is related to this, but it's not exactly the same: How to source R Markdown file like `source('myfile.r')`?
Basically, I perform most of my data cleaning and analysis in Rmarkdown files because the visual separation between chunks of code and my own comments on what should be done for the analysis/cleaning is very helpful to me. It also helps that within Rstudio if you run a table, df, then it'll display an interactive snippet of it in the document. This is all very helpful in complicated cleaning/analyses. So in other words, I'd like to develop in one R-Markdown file and write in another R-Markdown file. Splitting/writing the code into a source.R file is not ideal, unless there was a very automated and reproducible way to do it.
The issue is that for reports, I'd sometimes like to take specific objects that were generated from these lengthy data-cleaning and analyses files in Rmarkdown. For example, let's say that during my data-cleaning in Rmarkdown-file-1, there was a particular table that was giving me trouble problematic.df and that I'd like to call in my report or possibly perform further manipulations in my report (Rmarkdown-file-2).
So ultimately I think this is the question:
How can I call any arbitrary object generated at any arbitrary point of one Rmarkdown file in another Rmarkdown file?
Obviously, the above would be the ideal, but it sounds unreasonable, so perhaps this is a better question/request:
How can I call any arbitrary objects generated by the end of one Rmarkdown file in another Rmarkdown file?
Upon further reflection, my question might already be answered in the post I linked, but it's been a while since that question was posted and perhaps there are new solutions or perspectives on this issue.

Parse YAML front matter with R

I want to use YAML for SAS program documentation in a manner similar to how R studio uses YAML. Can I put YAML at the top of my program, then read it into R as some object? The program file would just be a text file called program_1.sas. There would be a bunch of these in a directory or two.
I can then use the object in an R notebook to produce a readable document describing a set of SAS programs. I would have to find a function to display the object in a readable format.
I noticed that this issue is addressed for languages like python and JAVA, but I am hoping to do it in R. Obviously this code has already been written. For example the function yaml_front_matter must do this, but it does not seem to be documented.
If I could also write/change the YAML header that would be a big plus.
Here is an example:
Project: Disease Burden
Directory: /net1/program/
Purpose: Extract Data
The only extra piece is that I would have to comment out this header and also identify it
/*
---
Project: Disease Burden
Directory: /net1/program/
Purpose: Extract Data
---
*/
I could then create markdown like this
# Project: Disease Burden
Directory
: /net1/program/
## Purpose
Extract Data

Including Rnw files within a package

I am writing a package and the sole purpose of this package is to create reports. I am using knit to generate the reports from a .Rnw file. This all happens within a function in the package. e.g.
create_report <- function(data) {
knit2pdf(from = "myreport.Rnw", to = "myreport.tex")
# The Rnw in the knit2pdf function uses the data passed to this function
}
My question is simple. Where within my package folders do I store the .Rnw file? Currently my package has the following folders:
.Rproj.user
data
man
R
I am just not sure where my Rnw scripts should go? Do I need another folder called LaTeX for example? This is like having a separate folder for C++ scripts, for example.
Note, I am not looking to create a vignette. I know how to do this. This package is used to do some data manipulation and then generate a report on the data.
I have tried to lay everything out as clearly as I can as some questions I have asked on here before have been misinterpreted. Please ask if anything is unclear.
To answer this question:
Include the .Rnw files in ./pkgname/inst/latex then when you build the package, the ./latex folder will go to the root level of the package. You can then extract the .Rnw files using system.file("latex", "mytemplate.Rnw", package = "pkgname").

How to remove .tex after running knit and texi2pdf

I have an R data frame that I run through knitr using the following code:
knit('reportTemplate.Rnw', 'file.tex') # creates a .tex file from the .Rnw one
texi2pdf('file.tex') # creates a .pdf file from the .tex one
Inside my R script, I want to remove 'file.tex' from my computer folder afterwards. How do I achieve this? It is important that I do this within my .R file, since those lines are actually inside a loop that generates 1000 different reports from that template.
There are a family of functions in R that allow the user to interact with the computer's file system. Run ?files to see functions that make it possible to, for instance, create, rename and remove files.
As noted by Josh O'Brien, in a comment to the OP, in this specific case, the command to be used is file.remove('file.tex').

Create and save R's default codebooks as a pdf

If I load data(mtcars) it comes with a very neat codebook that I can call using ?mtcars.
I'm interested to document my data in the same way and, furthermore, save that neat codebook as a pdf.
Is it possible to save the 'content' of ?mtcars and how is it created?
Thanks, Eric
P.S. I did read this thread.
update 2012-05-14 00:39:59 PDT
I am looking for a solution using only R; unfortunately I cannot rely on other software (e.g. Tex)
update 2012-05-14 09:49:05 PDT
Thank you very much everyone for the many answers.
Reading these answers I realized that I should have made my priorities much clearer. Therefore, here is a list of my priorities in regard to this question.
R, I am looking for a solution that is based exclusively on R.
Reproducibility, that the codebook can be part of a automated script.
Readability, the text should be easy to read.
Searchability, a file that can be open with any standard software and searched (this is why I thought pdf would be a good solution, but this is overruled by 1 through 3).
I am currently labeling my variables using label() from the Hmisc package and might end up writing a .txt codebook using Label() from the same package.
(I'm not completely sure what you're after, but):
Like other package documentation, the file for mtcars is an .Rd file. You can convert it into other formats (ASCII) than pdf, but the usual way of producing a pdf does use pdflatex.
However, most information in such an .Rd file is written more or less by hand (unless you use yet another R package like roxygen/roxygen2 help you to generate parts of it automatically.
For user-data, usually Noweb is much more convenient.
.Rnw -Sweave-> -> .tex -pdflatex-> pdf is certainly the most usual way with such files.
However, you can use it e.g. with Openoffice (if that is installed) or use it with plain ASCII files instead of TeX.
Have a look at package knitr which may be easier with pure-ASCII files. (I'm not an expert, just switching over from Sweave)
If html is an option, both Sweave and knitr can work with that.
I don't know how to get the pdf of individual data sets but you can build the pdf of the entire datasets package from the LaTeX version using:
path <- find.package('datasets')
system(paste(shQuote(file.path(R.home("bin"), "R")),"CMD",
"Rd2pdf",shQuote(path)))
I'm not sure on this but it only makes sense you'd have to have some sort of LaTeX program like MikTex. Also I'm not sure how this will work on different OS as mine is windows and this works for me.
PS this is only a partial answer to your question as you want to do this for your data, but if nothing else it may get the ball rolling.
The help page that is displayed when entering ?mtcars is generated from an .Rd file, which is a LaTeX-like file that is used for all of R's help pages. Although .Rd files are LaTeX-like, you don't actually need to know LaTeX to read or write them. The actual mtcars.Rd file is available here: http://commondatastorage.googleapis.com/jthetzel-public/mtcars.Rd , which can be viewed with any text editor.
.Rd files included in the ./man directory of a package are converted to .html files when installing the package. They are converted by functions in the "tools" package.. If you would like functionality like ?mtcars for your datasets, you would need to create a package for them. That might sound complicated if you have never created a package before, but it is easy enough to learn and will make you a better R programmer. There are a number of examples of dataset-only packages on CRAN, for example msProstate: http://cran.r-project.org/web/packages/msProstate/index.html . Consider downloading the package source to see how it is organized.
For more information on creating your own packages, writing .Rd files, and building packages:
http://cran.r-project.org/doc/manuals/R-exts.html, especially "1.1.5 Data in packages".
Edit
And if you want to convert the .Rd file in your package to a .pdf, you can do so when building your package, but you will need a LaTeX compiler. If you are on Windows, see here: http://cran.r-project.org/bin/windows/Rtools/ .
You can't create a PDF with just R; you need to use other software that creates PDFs.
You could use a combination of utils::promptData, tools::Rd2HTML, and a simple custom function to open the created HTML file in the users' browser.
It would probably be easier to just make a package containing your data sets. Look at the "datasets" package for an example.
It looks like that if you want to generate a pdf, an external tool like LaTeX is always needed. I would recommend using a simple ASCII text format to generate such a file. In principle the .Rd files are also ASCII text, but I do not find them particularly readable.
Instead, I would recommend using a plain text ASCII format such as Markdown (which is e.g. used on StackOverflow) to write the text file. Such a file is already much more readable than an .Rd formatted file, and as a bonus it can quite easily be processed into a PDF should you choose to do so later on. The knitr package I think is capable of generating PDF files from Markdown sources. In addition, knitr allows you to mix in R code in the Markdown text. This code can be evaluated and the results (even figures) added to the resulting PDF.
In practice you can use sprintf to generate character vectors that you can pipe to a file in order to dynamically generate the markdown text. Just write the template one time, and mark the places for the text you want to add later like this:
base_text = "
First header
============
This document was generated on %s, by %s.
"
text_forfile = sprintf(text, some_date, some_name)
Just dump the text in text_forfile to a .md file and your done, no external tools needed. See this post on SO for how dump text to a file.

Resources