Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering R code blocks in Typst #2283

Open
1 task
aksigkvgithub opened this issue Aug 26, 2023 · 3 comments
Open
1 task

Rendering R code blocks in Typst #2283

aksigkvgithub opened this issue Aug 26, 2023 · 3 comments
Labels

Comments

@aksigkvgithub
Copy link

Dear Sir,

You might be already aware of a new Typst markup language. It has very good math support and a good pdf rendering.

Fortunately, it has Markdown type source code block type. So, I tried to render R code block embedded into such an R code block in a Typst file, through knitr::knit() function , and then compiled this rendered file through Typst compiler.

Everything was rendered well into pdf of Typst except the image rendered by knitr in the/figure subdirectory, probably because knitr rendered Markdown type referencing and Typst has a different image referencing syntax.

So, I wish to know if the image referencing as per Typst syntax can be added into knitr for Typst file types.

Thanks and regards,

Dr. A. K. Singh


By filing an issue to this repo, I promise that

  • [ x] I have fully read the issue guide at https://yihui.org/issue/.
  • [ x] I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

@yihui yihui moved this to To discuss / To plan in R Markdown Team Projects Aug 26, 2023
@cderv
Copy link
Collaborator

cderv commented Aug 28, 2023

@aksigkvgithub could you share a full example of how you tried to use knitr for a typst document ?

For now we have no specific support and I am curious of how you tried to use it. Especially an example related to

Fortunately, it has Markdown type source code block type. So, I tried to render R code block embedded into such an R code block in a Typst file, through knitr::knit() function , and then compiled this rendered file through Typst compiler.

This would help be sure we understand correctly what you are trying to do.

Also, just so you are aware, there is a tool called Quarto which uses knitr under the hook for R users, and it supports producing a typst output from a markdown syntax document (called a .qmd file). See https://quarto.org/docs/prerelease/1.4/typst.html
this is currently available in pre-release only if you want to try it: https://quarto.org/docs/download/prerelease.html

Within this .qmd you can write Markdown syntax, and use R code chunk that will be evaluated using knitr. The whole rendering will produce a typst document using Pandoc. Quarto brings some feature support in addition to knitr and Pandoc.

Just sharing in case you don't know about it. We are still interested into your use case with (I suppose) .typ file.

@aksigkvgithub
Copy link
Author

Ok. I shall submit below the files created and obtained in the process, sequentially to make example workflow clrear:

  1. I use Windows system. So, for Windows I downloaded Typst software from Github. From the downlaod, I put the typst.exe on the systems path in the Environment variables.

  2. Please create a folder, say, named as "Analysis of Covariance".

  3. Within this folder, please keep the following file, after donloading:

2. ANCOVA in CRD.r.txt

  1. After downloading this .txt file, please rename it to .typ format, to make it a typst file. This file contains a small typst document with some R codes in a Rmarkdown block format. And this code block uses following data file. Please downlaod that file too:
    Leprosy.txt

  2. After downloading this data file, please rename this .txt file to .csv format, as it comtains comma separated data, which the R codes from within its block, will read. And please keep this data file too in the same "Analysis of Covariance" folder, after renaming to .csv format.

  3. Then open R console, in Windows of course as I use it, that will require installation of R for sometime, if not already downlaoded, which is open source and can be downloaded from CRAN repository. And on the R console, please make it sure to install "knitr" package, which may require installation of some other dependable packages, which generally R installs automatically mostly. But sometimes, when it cannot, then it indicates in its messages to install them first, then "knitr" will be installed.

  4. After successful intallation of "knitr" package, please run the following command, which will verify whether "knitr" has been successfully installed or not:

    library(knitr)
    

If package "knitr" is successfully installed, it will simply load that library and show the R prompt. Otherwise it will throw some error message.

  1. Now, on the R console, please run the following command, which uses "knitr" package:

knitr::knit("2. ANCOVA in CRD.r.typ","2. ANCOVA in CRD.typ")

  1. Then R will read the data from "leprosy.csv" file and create some output within Markdown type of blocks format as per the instructions of R codes present in the file "2. ANCOVA in CRD.r.typ" and finally create another typst file "2. ANCOVA in CRD.typ" as per instruction given on R console. I am giving that file below, which was created by R in the .txt format as follows:

2. ANCOVA in CRD.txt

  1. Please download this file and rename it from .txt format to .typ format to make it typst file too. In this file all the R output are created in Markdown format.

  2. Finally, please compile this file named file "2. ANCOVA in CRD.typ" into typst, by simply typing following command on the Windows command prompt console, invoked in the same "Analysis of Covariance" folder, as typst.exe is already on the path:

typst compile "2. ANCOVA in CRD.typ"

  1. The pdf file that is exported by typst in the same folder is as follows:

2. ANCOVA in CRD.pdf

  1. In this file all the R output come out in a clean format. But, the plot is missing despite the plot reference,
    plot of chunk unnamed-chunk-1, is present in the "2. ANCOVA in CRD.typ" file. The plot is created by R in the /figure subdirectory, and at the same time it creates the reference as indicated above. The plot which is created in the subdirectory is as follows:

unnamed-chunk-1-1

  1. Everything is already supported by typst, except that the plot from the /figure subdirectory is not fetched, despite that plot is being referenced by R in the Markdown type of image referencing syntax.

  2. I feel that the image referencing in typst is different for image / figure with and without caption, as is illustrated at the web page https://typst.app/docs/tutorial/writing-in-typst/

  3. So, my request is, whether in the knitr package can image referencing be made like that of typst for .typ files, so that R users can also use Typst documentation for articles and books, along with rendering of R codes code blocks, as Typst has some very good stuff for math users.

Thanks and regards,

Dr. A.K. Singh

Repository owner deleted a comment from aksigkvgithub Aug 29, 2023
@cderv cderv moved this from To discuss / To plan to Backlog in R Markdown Team Projects Sep 5, 2023
@ziyuanding
Copy link

Hi Dr. A.K. Singh @aksigkvgithub
hope it's not too late, here I got a solution for this: you can try to hook the plot function of knitr, and construct the typst style image with chunk options. This can be done by adding below code chunk at the top of your typ file.

# this chunk should be at the beginning of your paper. chunk option should be {r, echo=FALSE}
local({
hook_old <- knitr::knit_hooks$get("plot")

knitr::knit_hooks$set(plot = function(x, options) {
  #print(options)
  image_path <- paste(options$fig.path, options$label, '-', options$fig.cur, '.', options$fig.ext, sep="")
  image_caption <- options$fig.cap
  result_string <- sprintf("#figure(\n  image(\"%s\", width:%s),\n  caption: [%s],\n)",image_path, options$out.width, image_caption)
})
})

I only passed caption and width, but possibilities are there, you can try your own things.

The workflow I used is:

  1. write typ file(let's say "feed-me-to-knitr.typ") with above code at the top.
  2. run R command: knit("feed-me-to-knitr.typ", "feed-me-to-typst.typ")
  3. run typst compile "feed-me-to-typst.typ" final.pdf to get the final pdf.

all files mentioned are packaged into this zip:
typst-r.zip

@cderv cderv added the typst label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants