When I try to knit a RMarkdown document with ggvis plots to Word, I get the following message from Rstudio.
Error: Functions that produce HTML output found in document targeting docx output.
Please change the output type of this document to HTML.
Execution halted
Does there exist an elegant solution (without too much effort) to make this possible, perhaps using export_png and vg2png?
Here is an example:
```{r setup, include=FALSE}
library(dplyr)
library(ggvis)
```
The following table looks fine...
As long as I don't include this plot below
```{r, echo=FALSE, include=FALSE}
p <- pressure %>%
ggvis(x = ~temperature, y = ~pressure) %>%
layer_bars()
export_png(p, file = "hoge.png")
```
![](hoge.png)
If you will use this frequently, you can define hook.
First go to Node JS Download page
After instaling node.js, in terminal run:
npm install vega
Related
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:
I'd expect
#+begin_src plantuml :file foo.png :exports results
foo -> bar: JSON request
foo -> batz : instantiate batz\npassing resources from request
#+end_src
to only produce the resulting image in the exported document, but it exports both the plantuml code that produces it and the resulting image.
Any ideas what I can do to only export the images that are generated by the code block short of commenting out the code blocks after first generating the images?
I'm not allowed to comment so will provide an answer: this works fine for me with org 9.3.7 (i.e. only the image is exported, at least to PDF via LaTeX) so maybe it's a bug in the version of org you are using?
If posting this annoys anybody, I will delete.
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!
I am trying to export a larger number of Matlab figures that are generated in for loop to a single PDF file. Right now the best thing I could come up with is to all print them to a PostScrip file using the -append option like this:
print('Temp_Plots','-dpsc','-append')
After that I could convert the PS file to a PDF file. This workflow was okay until I started to use plots with 2 y axis. Unfortunately it seems like Matlab's PS export cannot properly handle this situation and does not color the lines appropriately.
As there is no -append option for the direct PDF export what other methods do I have to append all my plots to a single file without losing the assigned colors or other hickups?
I would recommend trying out the publish command and push that to its limits first.
Following the documentation:
options = struct('format','pdf','outputDir','C:\myPublishedOutput');`
publish('myCode.m',options);
Take a look at Publishing Markup to see how to get the look you want.
This search brings up some possibly related posts, but none that I saw that directly match your issue.
References:
1. Publishing Markup (Mathworks)
2. Output Preferences for Publishing (Mathworks)
3. Publishing M-Files in MATLAB
4. Publish Your Work in Matlab
I wish to show the knitR code in the final PDF together with its execution result.
I did not find yet a working way to fence "tripple-backticks"{r}..."tripple-backticks" blocks in the knitR code and see them as-is in the final PDF.
The backticks get interpreted whatever I do.
This is for tutorial purpose so that people see how to write knitR markdown. The use of html "pre" tags around the block of code leads to removal of the code.
for instance I wish to see this example fully first and then the result thereof
adding 4 spaces before each line like here does not work in RStudio and knit HTML fails
```{r test-haskell, engine='haskell', engine.path='ghc', cache=TRUE}
[x | x <- [1..10], odd x]
```
follow-up addition
I include here some RStudio Rmd code that leads to unexpected PDF content
in the pdf, the pre-code block simply disappeared.
The only fix I found is to invent a fake tag 'rmd' to fence the pieces of markdown I wish to keep as-is. I suspect this is a pandoc issue rather than knitr, unless there is a better way to fence code in knitr. The code I wish to keep can be any of bash, perl, R, or any other manguage used to process the data in the knitr tutorial.
my pandoc command was:
pandoc --variable=geometry:'top=1.5cm, bottom=1.5cm, left=2cm,
right=1cm' --variable=papersize:'a4paper' --number-sections
--table-of-contents --template=default.latex --highlight-style tango testrmd.md -o testrmd.pdf
Can it come from the template I used (default.latex)? It is the only template I found that meets my needs for vignette-like output. Same about 'tango' which is the only coloring scheme that shows some light background in code blocks.
As you see from the screenshots above I am a new-bee here (both in markdown and latex). Thanks for any help