r/css/md VS R markdown (in One)

Hello,

Do you advise r/css/md in separate files, and then use codes in the R file such as the one below to create an html file base on the md and css file and then from the html file convert to pdf file Or is it better to have all of it (r, md, css) in one file such as an Rmd file then just knit it to either html or pdf ?

markdownToHTML ("a.md", "a.html", stylesheet = "a.css")

I like the first choice since is very organized, but how do I incorporate the data in the md file unless it is static but what if I preferred to read an file or data from an external source then how to make the md file adapt to that or how do I use R code in the first scenario to manipulate the data in the md file ? Could you kindly please post an example code for the first scenario.

Maybe the second choice is better and more ideal, I don't know, please advise ?

If you want to mix text and code in publication purposes I would really go with Rmarkdown format instead of trying to reproduce what is does.

In a Rmd file, you'll be able to put text and chunk of code. You'll even able to include inline css if you want using css code chunk (you'll also point to an external css file). When you compile the document what it does is

  • Converting Rmd to md file, executing all the R code, replacing by the input and result (depending on the knitr option), saving figures for plots, ... This is the work of knitr :package:
  • Then it will convert md file to html, using css provided. This is the work of pandoc converter.

This works very well, this is pretty powerful and customisable. Unless you have a very strong specifity and con against using Rmarkdown, I would definitely use it.

2 Likes

Great, Thanks for the information. I will try it out.

It seems R markdown doesn't use Latex.
It's either we knit it to PDF or knit it to HTML.

What if I want to create a Latex file and then from the Latex file create a PDF file.
How can I do this, is there an example code ?

Rmarkdown is using Latex for creating PDF file.

Rmarkdown :arrow_right: Markdown :arrow_right: Tex :arrow_right: PDF

  • knitr does the conversion to markdown
  • pandoc convert to a tex file then Latex is used to produce PDF file.

Read the book chapter about PDF output and you'll see mention of Latex and all customisations. You'll find examples.

1 Like

That’s great. Good to know.
Thanks for the information and the book reference.

So I created an R markdown file. Then I Knit it to HTML, that looks fine, html file is produced.
But when I tried to Knit it to PDF, a TEX file appears and not the PDF file.
Not sure why a TEX file is produced, normally a PDF file is produced.

When I use this bottom code I get no result (normally a PDF file is produced):

rmarkdown::pandoc_convert("f.html, output = "d.pdf")

Please advise ?

It is very difficult to help with only these informations. Read the book and follow the advice to understand how it works.
To produce PDF you need to have latex installed. For best working with Rmarkdown features, tinytex is adviced

Otherwise, it should work fllawlessly with last RStudio version and Rmarkdown.

Know that rmarkdown::pandoc_convert() is advanced used and you need to master pandoc as it is just a wrapper to call directly pandoc converter without any of Rmarkdown treatment.

I found out why it created TEX file but not PDF.


It said NO TEX installation detected (Tex is required to create PDF output). 
You should install a recommended Tex distribution for your platform:

windows: MikTEX (Complete) - http: //miktex.org/2.9/setup
(NOTE: Be sure to download the Complete rather than Basic installation)

I thought that if I install tinytex that I don't need MikTEX and it would work.

Please advise.

Yes it should work. Did you have another installation of latex on your system ?

Did you follow tinytex step to install a latex distribution as describe in the linked I posted above ?

install.packages('tinytex')
tinytex::install_tinytex()
# to uninstall TinyTeX, run tinytex::uninstall_tinytex() 

Look at tinytex::tinytex_root() to see where it is installed.

I use the code to install and also use the code to uninstall. It said TinyTeX does not seem to be installed after trying to install.
And to look for where is install
TinyTeX::uninstall_tinytex()

I get results as “ “

I don’t know if restriction are the reason from it being install.

What the installation says? There is also a reinstall function. It is really hard to debug like that but it is supposed to be easy and it works pretty easily on my side.

If you provide some detailed step and error message we may help.

When I run code install.packages(‘tinytex’)

I get message: Error in install.packages : updating loaded packages

Restarting R session

This is a binary version available but the source version is later: binary source needs_compilation

tinytex 0.11 0.12. FALSE

It gave a lot of long messages.
Hard to type everything out.

So here you are at the installation of tinytex not Latex.

You need to install this packages in a clean session. Restart R and try again.

Let me look into it.
Thanks for your advise.

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.