Archive

Archive for October, 2021

R Markdown installation for Windows

October 20, 2021 Leave a comment

Problem

You want to use R Markdown under Windows too. How to install it?

You also want to use Python codes in the .Rmd files. You also need VS Code support.

Solution

  • Download and install R: https://cran.r-project.org/bin/windows/base/ . If you have a 64-bit system, then select the 64-bit installation option (i.e., don’t install the 32-bit version). Default installation path: C:\Program Files\R\R-4.2.2 .
  • Add C:\Program Files\R\R-4.2.2\bin to the PATH.
  • Also add C:\Program Files\R\R-4.2.2\bin\x64 to the PATH (supposing that you installed the 64-bit version).
  • Download and install RStudio Desktop: https://www.rstudio.com/products/rstudio/download/ . Default installation path: C:\Program Files\RStudio .
  • Launch R Studio, and select File -> New File -> R Markdown. It’ll offer to install some required packages. Accept.
  • In the R console, issue the following command:
    tinytex::install_tinytex()
  • Now you should be able to render an .Rmd file to PDF inside R Studio.
  • Add C:\Program Files\RStudio\bin\pandoc to the PATH. If you don’t have this folder, then:
    • Try to find Pandoc on your system with the following command in CMD: Rscript -e "rmarkdown::find_pandoc()" . If found, add its path to the PATH.
    • If Pandoc was not found on your system, then download and install it from https://pandoc.org/installing.html . Normally, the installer adds pandoc to the PATH. If not, add its folder to the PATH manually.
  • In the R console, install some packages:
    install.packages("reticulate")
    install.packages("languageserver")
  • For VS Code, install the extension “R” by REditorSupport.
  • Install Miniconda too ( https://docs.conda.io/en/latest/miniconda.html ). Put it to C:\miniconda3 (the path cannot contain spaces). Launch the installer in admin mode.
  • Add C:\miniconda3 and C:\miniconda3\condabin to the PATH.
  • In the R console, issue the following commands:
    library(reticulate)
    reticulate::py_discover_config()
    It should find your miniconda installation.
  • If you want to have the command “make”, follow these steps:
    • Visit https://nuwen.net/mingw.html and download the latest installer ( mingw-18.0.exe ). Install it to C:\MinGW .
    • Add C:\MinGW\bin to the PATH.

Here is a test document: https://github.com/jabbalaci/blog-assets/tree/master/20210929-Rmd . Now you should be able to render it both in VS Code, and by issuing the “make” command.

Acknowledgement

I want to say thanks to one of my students, Csaba Süvöltős, who helped me updating this post for Windows 10.

Links

Categories: windows 10 Tags: , ,

Show desktop

October 20, 2021 Leave a comment

Problem

Under Ubuntu, how to minimize all windows? That is, you want to see the desktop. Under Windows it’s Super + d. I want to use this very same shortcut, I don’t want to learn a new one.

Solution

I found the solution here. In short: go to Settings -> Keyboard Shortcuts. There, under Navigation, locate “Hide all normal windows”. Assign the desired shortcut to it. Done.

Categories: ubuntu Tags: , ,

[R Markdown] use inline Python code

October 18, 2021 Leave a comment

Problem

You want to use inline Python code in R Markdown. Example:

1 + 1 = `r 1+1`         # 1 + 1 = 2

1 + 1 = `python 1+1`    # 1 + 1 = python 1+1

As you can see, it only works with R :( What to do?

Solution

Python is not supported in inline code, but I have the following workaround solution:

```{r setup, include=FALSE}
library(reticulate)
```

```{python include=FALSE}
result = 1 + 1
```

1 + 1 = `r py$result`    # 1 + 1 = 2

py$result means: take the value of the Python variable called result.

execute a command when a file changes

October 18, 2021 Leave a comment

Problem

You want to execute an action when a file changes. For instance, when your .tex file changes, you want to recompile it immediately and update the resulting PDF.

Solution

The command entr can be used for this. It can be installed using your package manager.

Here is an example Makefile for R Markdown files:

all:
	Rscript -e "rmarkdown::render('./test.Rmd')"

watch:
	ls *.Rmd | entr make all

How to read it: “Monitor all .Rmd files. If any of them changes, execute ‘make all’ immediately”.

Alternatives

Another alternative is wgo, which is very new but quite promising. While entr receives its file list to be monitored via its standard input, wgo uses command-line arguments for this purpose. Example:

all:
	Rscript -e "rmarkdown::render('test.Rmd')"

watch:
	wgo -file=.Rmd make all

It does the same thing.

Links

Categories: linux Tags: , , , , ,

[manjaro] /snap is missing

October 15, 2021 Leave a comment

Problem

Under Manjaro, you install some packages via snap but the folder /snap is missing. WTF?

Solution

$ sudo ln -s /var/lib/snapd/snap /snap

Categories: linux Tags: ,