REditorSupport / vscode-R

R Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=REditorSupport.r
MIT License
1.05k stars 122 forks source link

Kniting to html_document and Knitting to html preview freeze at 0% #1277

Open KatlehoJordan opened 1 year ago

KatlehoJordan commented 1 year ago

Describe the bug

I have built a VS Code Dev Container for a colleague to explore R in VS Code in order to convince them to replace RStudio. Within the Dev Container, when I use the commands to knit a .Rmd file to .html or to preview the html in another pane, an information window appears that the document is being knit, but it never advances past 0%. See the screenshots.

Screenshot 2022-12-15 065628

Screenshot 2022-12-15 072201

To Reproduce

  1. Install VS Code
  2. Install Docker Desktop - if on Windows (as I am and my colleague) then this will require WSL2 installation and configuration
  3. Create directory for files
  4. Create Dockerfile; provided below
  5. Create .devcontainer/devcontainer.json; provided below
  6. Create dependencies.R; provided below
  7. Create explore Rmd.Rmd; provided below
  8. Open VS code session in directory with files: code <path to directory with files>
  9. Ensure Docker Desktop is running
  10. Rebuild container without cache (takes ~20 minutes to build on my machine)
  11. Navigate to explore Rmd.Rmd within the Dev Container
  12. Run the code chunks and observe output to the radian console
  13. Try to knit the document from the command palette (ctrl/cmd + shift + p -> 'R: knit Rmd to HTML')
    • Observe the popup window that it tries to knit but does not proceed past 0%
  14. Try to open the preview of the rendered document from the command palette (ctrl/cmd + shift + p -> 'R Markdown: Open Preview')
    • Observe the popup window that it tries to knit but does not proceed past 0%

Can you fix this issue by yourself? (We appreciate the help)

I would like to try to help, but am unsure how to proceed, 😦

Files for reproducing

Dockerfile

FROM r-base:4.2.2

WORKDIR /usr/src

COPY . .

RUN \
    apt-get update && \
    apt-get install -y \
        python3 \
        python3-pip \
        libxml2-dev \
       libfontconfig1-dev \
       libcurl4-openssl-dev \
       libssl-dev \
       pandoc \
       libharfbuzz-dev \
       libfribidi-dev \
       libfreetype6-dev \
       libpng-dev \
       libtiff5-dev \
       libjpeg-dev \
    && \
    Rscript dependencies.R && \
    pip3 install -U radian && \
    echo "alias r=radian" >> /etc/bash.bashrc

CMD [""]

.devcontainer/devcontainer.json

{
    "name": "Existing Dockerfile",
    "context": "..",
    "dockerFile": "../Dockerfile",
    "extensions": [
         "REditorSupport.r",
         "ms-vscode.live-server",
    ],
    "settings": {
        "[r]": {
            "editor.formatOnType": true,
            "editor.formatOnSave": true,
            "editor.formatOnPaste": true,
            "editor.defaultFormatter": "REditorSupport.r"
        },
        "r.bracketedPaste": true,
        "r.plot.useHttpgd": true,
        "files.associations": {
            "*.Rmd": "rmd"
        }
    }
}

dependencies.R

cores_to_use <- max(parallel::detectCores() - 2, 1)

install.packages("remotes", Ncpus = cores_to_use)

packages <- c(
   "languageserver" = "0.3.14",
   "tidyr" = "1.2.1",
   "httpgd" = "1.3.0",
   "ggplot2" = "3.4.0",
   "plotly" = "4.10.1",
   "shiny" = "1.7.3",
   "rmarkdown" = "2.18"
)

for (pkg in names(packages)) {
   remotes::install_version(pkg, packages[pkg], Ncpus = cores_to_use)
}

explore Rmd.Rmd

# Section 1

## Subsection 1.1

# Editing R Markdown
```{r}
m <- lm(mpg ~ ., data = mtcars)
summary(m)
x <- rnorm(100)
y <- rnorm(100)
plot(rnorm(100))
abline(h = 0, col = "red")
plot(rnorm(100))
abline(h = 0, col = "green")
eitsupi commented 1 year ago

Who is the user in the container? This may be a matter of write permission to the directory.

Did you try the Dev Container Template provided by rocker-org? https://github.com/rocker-org/devcontainer-templates/tree/main/src/r-ver

KatlehoJordan commented 1 year ago

It is the official R image hosted on Dockerhub.

I am quite sure that it is not a problem with which base image I am using, but rather something with how the image has been configured.

In earlier iterations wih the same base image rendering html in the side view was working, but I did not commit the repo at that time and I've done lots of changes since so I'm not sure what is wrong with the current setup, 😢.

eitsupi commented 1 year ago

Yeah, this is not a base image issue but a Dev Container configuration issue. Therefore, I recommend using existing templates. And, I would recommend including a minimal reproducible example, not one that takes 20 minutes to build.

KatlehoJordan commented 1 year ago

I agree with your sentiment! I do not like needing 20 minutes to build the container, but I have not found a Dockerfile + .devcontainer/devcontainer.json template that includes this VS Code extension with all features working that is faster to build.

It seems that to get all of the VS Code features working one must install multiple dependencies, hence the long build time. But possibly I am doing it wrong!

Can you recommend a template Dockerfile that includes this VS Code extension with all features working?

The one you linked does not seem to include this extension, nor its dependencies, so I think to get that working would require signifcant modifications to the template and then the same long-build-time problem would arise.

Again, thank you for your feedback, 🙂

eitsupi commented 1 year ago

The one you linked does not seem to include this extension, nor its dependencies, so I think to get that working would require signifcant modifications to the template and then the same long-build-time problem would arise.

The template contains most of them. Please try it once.

KatlehoJordan commented 1 year ago

I will try, thank you.

eitsupi commented 1 year ago

I link the devcontainer.json that I use to create the R package, since the R configurations are included in the base container, the user only needs to keep the non-R configuration. https://github.com/eitsupi/prqlr/blob/0f27151ee4a7511e1c1780345d30e3999e656718/.devcontainer/devcontainer.json

KatlehoJordan commented 1 year ago

I have started working with the rocker template you provided. So far so good. I have not finished the evaluation yet (have not yet tested plotting, shiny, R Markdown, and debugging), but will try to finish that later today.

In any case, I am closing this because we seem to agree that my problem is how I configured the Docker container... And not a bug in this extension.

Sorry for opening an issue with the wrong label, and thank you for your guidance!

KatlehoJordan commented 1 year ago

Update:

Even with the linked rocker container, the RMarkdown knitting to html is not working. The same behavior is observed.

I do believe that some days ago I was able to render the previews to the side and knit with the GUI provided by the extension, but since I cannot replicate there's a chance my memory is wrong.

If my memory is correct though, then it seems I've changed something in my machine that has led to this problem, but I'm not sure what. Commenting out my entire settings.json file does not resolve the problem, so I do not think it's a VS Code problem but rather a system-wide problem.

KatlehoJordan commented 1 year ago

I found the problem!

The extension does not escape characters when generating the path that is used to knit to HTML.

If the path of the .Rmd file contains whitespace, for example, it seems to fail.

Observe this log from when a friend tried it recently:

[VSC-R] explore Rmd.Rmd process started
==> rmarkdown::render('/workspaces/jobSearch/KJ's suggestions/Assignments/Assignment 05/brave new world in R with Docker/explore Rmd.Rmd', output_format = 'html_document')

The ' in KJ's suggestions and the whitespaces used throughout are never escaped.

Conversely, running the following command (escaping those characters with \) from an R terminal works:

rmarkdown::render('/workspaces/jobSearch/KJ\'s\ suggestions/Assignments/Assignment\ 05/brave\ new\ world\ in\ R\ with\ Docker/explore\ Rmd.Rmd', output_format = 'html_document')

I will reopen the issue to catch the attention of the developers since I think I've pointed towards a fix.

ElianHugh commented 1 year ago

Thank you @KatlehoJordan for looking into this! I should have some spare time in the coming days to work on a fix :)

ElianHugh commented 1 year ago

So, as per your comment, further escaping of the path string is needed here: https://github.com/REditorSupport/vscode-R/blob/dc5be834db12ffd6f52650620e8547d358a30e77/src/rmarkdown/knit.ts#L259

ElianHugh commented 1 year ago

OK, so a very simple fix is to replace ' with \\' (which does fix this issue*, thank you!) but this doesn't cover all issues with path names. I'll have to take a closer look at util.ToRStringLiteral as it's gobbling up quotes in the path name as well

github-actions[bot] commented 5 months ago

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] commented 4 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.

tomazfs commented 2 months ago

I have this issue also with word_document target.

tomazfs commented 2 months ago

I've had setting "r.rpath.windows" in settings.json pointing to an old non existing R installation. After correcting this setting kniting is again working O.K.