Why doesn't the table generated from r-exams appear in Moodle? - moodle

I have defined the variables and data necessary to generate a frequency table in RMarkdown, to export it to Moodle, using R-exams:
xsubio<-c(sample(5:35,5))
xsubi<-sort(xsubio)
frecuencias<-sample(0:30 ,5)
tabla<-data.frame(xsubi,frecuencias)
colnames(tabla)<-c(" Edades" ,"Personas")
Through chunk I generate the table:
```{r, eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE}
library(exams)
print(xtable(tabla), include.rownames=FALSE)
```
When exporting to PDF, using exams2pdf, the table is displayed perfectly:
But, when exporting to Moodle, using exams2moodle, the table disappears:
What do I have to do to make it appear?

Problem: You have an exercise with formatting in Markdown and use xtable() to insert a table in LaTeX. This mixture of Markdown+LaTeX is no problem when converting the exercise to PDF. Internally, this preserves the LaTeX table and just converts the Markdown parts to LaTeX as well before rendering the LaTeX to PDF. However, the same is not possible automatically when converting the Markdown exercise to HTML for Moodle. The converter then does not separate the LaTeX part automatically to convert it to HTML.
In short: The different markups must be sufficiently in sync. Markdown+LaTeX markup for PDF output works and Markdown+HTML for HTML output works. But what would be even better is to have the markup fully in sync, i.e., LaTeX+LaTeX or Markdown+Markdown. Such exercises can then be rendered to either PDF or HTML automatically.
Possible solutions:
Markdown+Markdown: Markdown table markup in R/Markdown (Rmd) exercise
I would recommend that you simply produce tables in Markdown via knitr::kable() rather than xtable::xtable(). Thus, the code chunk for the table simply becomes
knitr::kable(tabla, format = "markdown")
and then the tables are rendered correctly in both PDF and HTML-based formats like Moodle.
LaTeX+LaTeX: LaTeX table markup in R/LaTeX (Rnw) exercise
In case you prefer to keep on generating LaTeX tables with xtable() another route would be to change the markup of the R/exams exercise to Rnw (R/LaTeX). See the First steps tutorial on the R/exams web page for how to do that.
Markdown+Adaptive: Choose the right table markup depending on the output format
Using the match_exams_call() function you can determine in a running exercise which exams2xyz() interface is being used to process it. So you could do
type <- if(match_exams_call() %in% c("exams2pdf", "exams2nops")) "latex" else "html"
print(xtable(tabla), type = type, ...)
Then either LaTeX or HTML format is produced by xtable().
All three solutions work in principle but I think Solution 1 (kable()) is the best and most robust in your case.
Bonus remark: If you are using R/exams >= 2.4-0 you can use exams2moodle(...,table = TRUE) to get nicer table formatting in Moodle.

I use the following code snippet (with type = "latex"), when I require a PDF output:
```{r, eval=TRUE, only.contents=TRUE, echo=FALSE, results='asis',
warning=FALSE, message=FALSE, error=FALSE, comment=FALSE}
library(exams)
library(xtable)
print(xtable(tabla), include.rownames=FALSE, type = "latex", comment=F)
```
And, when I require outputs for Moodle, I make use of the following code snippet (with type = "html"):
```{r, eval=TRUE, only.contents=TRUE, echo=FALSE, results='asis',
warning=FALSE, message=FALSE, error=FALSE, comment=FALSE}
library(exams)
library(xtable)
print(xtable(tabla), include.rownames=FALSE, type = "html",
comment=F)
```
Results in PDF:
Results for Moodle:

Related

Inserting non-text items in VS code similar to RTF editors?

I use VS code for lots of things, from actual programming to just taking notes or conspecting. A few features I would really love would be
ability to insert images in between the text
live, cell based auto formatting for text that forms a small table
Both of these can be done in rich text editors like ms word by inserting just images or an excel table.
Now I realize that VS code is rather file format agnostic and it wouldn't make a whole lot of sense to somehow have an excel table in the middle of a .js file but I think there is a way.
For example, JSDoc is a kind of add-on format that lives entirely within js comments. Same could be done with tables and/or images. Of course there wouldn't be a universal way to encode this but just like JSDoc it could be adapted to different language environments, be it a php file or c file or plain text.
For example, in raw text format, an auto formatted table of data could look something like this within a javascript file:
const my_data = [
/*!!! auto_format_csv(Title, Description, Weight) !!!*/
"Apple", "A nice fruit", 1,
"Car", "A motorized vehicle", 2000
/*!!! auto_format_end !!!*/
];
and while editing the file in vs code, it could look something like this:
So to the question part: are there perhaps any extensions that already do this sort of thing? If not, is it possible to create such an extension with the liberties given to extensions as of right now?
I know that vs code is based on electron and open source so in theory, everything is possible but I want to have these features as easily as possible so having framework support for this would likely help a lot.

Different copies of question with table for Moodle with R-Exams

I would like to generate several copies of a question with randomly generated data in order to upload to Moodle and make a quiz. This question would include a table which depends on the data generated each time. How can this be done? I tried using xtable but it generates a table with no margins and format when uploaded to Moodle. I also tried ggpubr but it was not possible to include the table.
I read that one can generate a '.png', for example, and using 'include_supplement' and \includegraphics for .Rnw the file is imported into the file. This is useful when only one copy is generated because only one file would be the one imported. But what happens when multiple copies of the question with different data have to be created at once?
Any help would be appreciated.
Thank you.
Overview
Table formatting is not so straightforward for Moodle, both when starting from an exercise in R/LaTeX format (Rnw, as you do) or in R/Markdown format (Rmd). Below I'm showing a couple of variations of what you can do although I'm not 100% happy with all of them. In all cases the example is static but could be made dynamic in the "usual" way by inserting the random numbers into the respective tables. If you have problems with making one of the solutions dynamic, please let me know.
Plain
When you are starting in Rnw you typically generate a {tabular} object either by hand or via packages like xtable or knitr::kable etc. These are converted to valid HTML and imported into Moodle but the formatting with lines (horizontal and/or vertical) is not preserved. The same is true when starting in Rmd and using plain Markdown markup to code the table (again by hand or via knitr::kable etc.).
Example:
Rnw:
\begin{question}
Consider the following table:
\begin{tabular}{lrr}
\hline
Name & Min & Max \\
\hline
Foo & 0 & 1 \\
Bar & 0 & 100 \\
\hline
\end{tabular}
What is the overall maximum?
\end{question}
\exname{Table}
\extype{num}
\exsolution{100}
\extol{0.01}
Rmd: Would be similar to above but the table in plain Markdown as:
| Name | Min | Max |
|:-----|----:|----:|
| Foo | 0 | 1 |
| Bar | 0 | 100 |
Some other learning management systems (like OpenOLAT for example) offer suitable table classes in their CSS so that we can tweak the <table> in the resulting HTML to <table class="mytable"> (where the "mytable" class would need to be provided in the CSS). I looked around a bit in Moodle's question editor but there doesn't seem to be support for such dedicated CSS table styles. If anyone knows more about this I would appreciate some pointers.
HTML
The best alternative to this is probably to start in Rmd but instead of writing the table in Markdown you can use full HTML directly. This gives you extensive possibilities for styling the cells by hand. There are also various packages that help you with this. Below I'm using a combination of knitr::kable and kableExtra::kable_styling. The latter offers many more options than those that I use below.
Example:
Rmd:
Question
========
Consider the following table:
```{r, echo = FALSE, results = "asis"}
d <- data.frame(
Name = c("Foo", "Bar"),
Min = c(0, 1),
Max = c(0, 100)
)
kableExtra::kable_styling(
knitr::kable(d, format = "html", booktabs = TRUE),
bootstrap_options = "bordered", full_width = FALSE, position = "left")
```
What is the overall maximum?
Meta-information
================
exname: Table
extype: num
exsolution: 100
extol: 0.01
Rnw: I guess the same trick should be possible in Rnw exercises, i.e., include HTML in the LaTeX exercise and preserve that when converting to HTML with pandoc. However, I didn't manage to find the right flag for that. So this currently works just from Rmd exercises.
LaTeX
You can also typeset the table with LaTeX and use pdfLaTeX for rendering and then convert the output to PNG or SVG. This is supported by the tex2image() function in the exams package. This can be used in both Rnw and Rmd exercises and the resulting image has to be included in the exercise. The disadvantage is that the fonts etc. differ between the table and the main question (and you have to play with the fontsize and resolution in tex2image()). Moreover, this is relatively slow because pdfLaTeX has to be run on each exercise with such a table.
Example:
Rnw:
\begin{question}
Consider the following table:
<<echo=FALSE, results=hide>>=
tab <- '\\begin{tabular}{lrr}
\\hline
Name & Min & Max \\\\
\\hline
Foo & 0 & 1 \\\\
Bar & 0 & 100 \\\\
\\hline
\\end{tabular}'
tex2image(tab, name = "tab", dir = ".", pt = 8, resize = 250)
#
\includegraphics{tab.png}
What is the overall maximum?
\end{question}
\exname{Table}
\extype{num}
\exsolution{100}
\extol{0.01}
Rmd: The same code chunk generating the image could be used in Rmd. Just the \includegraphics would need to be replace by the corresponding ![]() Markdown.
CSS
Yet another option to render the table in Moodle is to insert a custom stylesheet with a class for the <table class="..."> to be rendered. A worked example is provided by Kenji Sato in his blog at: https://www.kenjisato.jp/en/post/2020/07/moodle-bordered-table/. We plan to integrate this with a couple of typical classes in exams2moodle() so that the CSS does not have to be inserted in every exercise manually. However, we did not yet get round to impelement this.
Sometimes we use quick and dirty hack.
$$
\begin{matrix}
\text{Name} & \text{Min} & \text{Max} \\
\ldots
\end{matrix}
$$
This hack has at least two advantages :)
It can be applied in a big team where everyone knows latex but only some guys know markdown.
It works both from .Rmd and .Rnw files. The plain \begin{tabular} way works only from .Rnw files.
Obvious con:
It's dirty!

GitHub Wiki doesn't support HTML tables anymore?

Back in the days GitHub Wiki supported embedding HTML tables, but now all our tables are not rendered anymore. Is support for HTML tables officially dropped (can't find corresponding news or blog post)?
GitHub does support table tag but it's not as extensible as in any HTML file. It's pretty much limited to what one may need for a wiki. In my view, it's pointless to use <table> because they are tiring compared to other markdowns that GitHub wiki use.
Markdown -
| First Header | Second Header |
| ------------- | ------------- |
| Content Cell | Content Cell |
| Content Cell | Content Cell |
Preview -
Equivalent HTML -
<table>
<tr><th>First Header</th><th>Second Header</th></tr>
<tr><td>Content Cell</td><td>Content Cell</td></tr>
<tr><td>Content Cell</td><td>Content Cell</td></tr>
</table>
PS. Here is a post on tables for markdown.
2017:
Back in the days GitHub Wiki supported embedding HTML tables, but now all our tables are not rendered anymore.
2022: they should be rendered now:
Updates to Markdown pasting on GitHub
May 19, 2022
We've made some updates to how paste formatting works in Markdown-enabled fields on GitHub.
For example, in code editors and on gists, you'll now be able to paste URLs on selected texts that will render as Markdown links like [...](https://...) by using the keyboard shortcut cmd|ctl + v..
The following paste formatting changes have been made to pull requests, issue comments and wikis:
Spreadsheet cells and HTML tables will render as Markdown tables
Any copied text containing links will render the links in Markdown.
All of this formatting can be disabled when pasting using the keyboard shortcut: cmd|ctl + shift + v or cmd|ctl + shift + Alt + v.
According to GitHub's Markdown spec, the Markdown parser permits most any raw HTML. The important thing is that the tags must begin at the start of the line. There are also rules which change the behavior when the raw block contains blank lines in some situations. Prior to adopting the current spec, I'm not sure if they were as strict about that, but that could be a reason for the change (some example input would help narrow down the possibilities).
And as another answer suggests, the GitHub Flavored Markdown spec includes a tables extension, so you can create tables natively in Markdown. This eliminates the need to structure your own HTML to the whims of the Markdown parser.
However, that is only the beginning of GitHub's processing of your input. After passing your input through the Markdown parser, there are four additional steps of processing taken on the output generated by Markdown as documented in the github/markup project. The most likely culprit is step two, which sanitizes the HTML. If your raw HTML doesn't match the expectations of the very narrow sanitizer, then it will get stripped out. The specifics of the santitizer are not documented, but the code is available to review and pretty easy to follow (even for those of use who aren't very familiar with Ruby).
Interestingly, the Markdown parser is sure to output HTML that the santitizer allows through, and in fact, tables are allowed. However, if you are using raw HTML rather than Markdown tables for more flexibility, then it is likely the extras that Markdown doesn't give you are causing the sanitizer to eat your tables (for example, you only get limited attributes, and improperly nests tags are stripped). In other words, raw HTML tables can only be limited to the basic features you already get with Markdown tables. Given the simplicity of Markdown tables over raw HTML, most people just use the markdown tables. YMMV.

Include *prewritten* documentation in Doxygen

To distinguish this question from Doxygen: Adding a custom link under the "Related Pages" section which has an accepted answer that is not a real answer to the question, I specifically add prewritten to the question.
What I want:
Write one document tex file (without preamble, since this file will be \input-ed into a full document)
Import the document into Doxygen's HTML output.
Using Doxygen to produce tex file will probably not work, since it does too much layout work [This holds for its HTML output too like empty table rows 2015]. If Doxygen takes some other input that can easily be transformed into LaTeX, that will do.
You can easily add an already existing Latex file to your doxygen documentation using \latexonly\input{yourfile}\endlatexonly.
I would assume you put it e.g. under a doxygen \page.

Is there an option to control output page orientation (using knitr->pander->pandoc->docx)

I am playing with Tal's intro to producing word tables with as little overhead as possible in real world situations. (Please see for reproducible examples there - Thanks, Tal!) In real application, tables are to wide to print them on a portrait-oriented page, but you might not want to split them.
Sorry if I have overlooked this in the pandoc or pander documentation, but how do I control page orientation (portrait/landscape) when writing from R to a Word .docx file?
I maybe should add tat I started using knitr+markdown, and I am not yet familiar with LaTex syntax. But I'm trying to pick up as much as possible while getting my stuff done.
I am pretty sure the docx writer has no section breaks implemented, also as far as I understand --reference-docx allows for customizing styles and not the page layout (but I might also be wrong here), this is from pandocs guide on --reference-docx:
--reference-docx=FILE
Use the specified file as a style reference in producing a docx file.
For best results, the reference docx should be a modified version of a
docx file produced using pandoc. The contents of the reference docx
are ignored, but its stylesheets are used in the new docx. If no
reference docx is specified on the command line, pandoc will look for
a file reference.docx in the user data directory (see --data-dir). If
this is not found either, sensible defaults will be used. The
following styles are used by pandoc: [paragraph] Normal, Title,
Authors, Date, Heading 1, Heading 2, Heading 3, Heading 4, Heading 5,
Block Quote, Definition Term, Definition, Body Text, Table Caption,
Image Caption; [character] Default Paragraph Font, Body Text Char,
Verbatim Char, Footnote Ref, Link.
Which are styles that are saved in the /word/styles.xml component of the docx document.
The page layout on the other hand is saved in the /word/document.xml component in the <w:sectPr> tag, but pandoc's docx writer ignores this part as far as I can tell.
The docx writer builds by default a continuous document, with elements such as headers, paragraphs, simple tables and so on ... much like a html output.
Option #1 (doesn't solve the page orientation problem):
The only page layout option that you can define through styles is the pageBreakBefore which will add a page break before a certain style
Option #2 (seems elegant but hasn't been tested):
Recently the custom writer has been added that allows for a custom lua script, where you should be able to define how certain Pandoc blocks will be written into the output file ... meaning you could potentially define section breaks and page layout for a specific block inserting the sectPr tag into the document. I haven't tried this out but it would be worth investigating. On pandoc github you can check out a sample lua script file for custom html output.
However, this means, you have to have lua installed, learn the language, and it is up to you if you think its worth the time investment.
Optin #3 (a couple of clicks in Word might just do):
As you will probably spend quite some time setting up how to insert sections and what would be the right size, margins, and figuring how to fit the table to such a layout ... I recommend that you use pandoc to put write your document.docx, that you open in Word, and do the layout by hand:
select the table you want on the landscape page
go to Layout > Margins
> select Apply to: Selected text
> choose Page Setup > select Landscape
Now a new section with a landscape orientation should surround your table.
What you would anyway also probably want to do is styling the table and table caption a little (font-size,...), to achieve the best result (all text styling can be already applied with pandoc where --reference-docx comes handy).
Option #4 (in situation when you can just use pdf instead of docx):
As far as I could figure out is that with pandoc does a good job with tables in md -> docx (alignment, style, ... ), in tex -> docx it had some trouble sometimes. However if your option allows for a pdf output latex will be your greatest friend. For example your problem is solved as easily as just using
\usepackage{pdflscape}
and adding this around your table
\begin{landscape}
...
\end{landscape}
This are the options that I could think of so far.
I would always recommend using the pdf format for reports, as you can style it to your liking with latex and the layout will stay the way you want it to be.
However, I also know that for various reasons word documents are still the main way of reviewing manuscripts in many fields ... so i would most likely just go with my suggested option 3, mostly cause it is a lazy and quick solution and because I usually don't have many documents with tons of giant tables with awkward placement and styling.
Good luck ;-)
Based on Taleb's answer here and some officer package functions, I created a little gist that one can use like this:
---
title: "Example"
author: "Dan Chaltiel"
output:
word_document:
pandoc_args:
'--lua-filter=page-break.lua'
---
I'm in portrait
\endLandscape
I'm in landscape
\endPortrait
I'm in portrait again
With page-breaks.lua being the file hosted here: https://gist.github.com/DanChaltiel/e7505e62341093cfdc489265963b6c8f
This is far from perfect (for instance it won't work without the last portrait section), but it is quite useful sometimes.