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

Will not include image if path has a space #107

Closed
vnijs opened this issue Oct 9, 2023 · 13 comments
Closed

Will not include image if path has a space #107

vnijs opened this issue Oct 9, 2023 · 13 comments
Assignees
Labels

Comments

@vnijs
Copy link

vnijs commented Oct 9, 2023

Images are not being shown if the absolute or relative path has a space. Reproducible example below. I don't remember ever seeing this issue before and it doesn't happen with rmarkdown. I'm using this in combination with knitr. Is there a workaround that does not require moving to Rmarkdown?

Thanks

# works fine 
dir.create("figure")
dest <- "figure/some-image.png"
download.file("https://radiant-rstats.github.io/docs/model/figures_model/regress_catalog_F_critical.png", dest)

file_path <- "figure/some-image.png"
file.exists(file_path)
md <- paste0("\n## Including Plots\n\nYou can also embed plots, for example:\n\n![plot of chunk pressure](", file_path, ")\n")
cat(markdown::mark_html(text=md), file="report.html")
browseURL("report.html")

# fails to include image in html
dir.create("test directory/figure", recursive = TRUE)
dest <- "test directory/figure/some-image.png"
download.file("https://radiant-rstats.github.io/docs/model/figures_model/regress_catalog_F_critical.png", dest)

file_path <- "test directory/figure/some-image.png"
file.exists(file_path)
md <- paste0("\n## Including Plots\n\nYou can also embed plots, for example:\n\n![plot of chunk pressure](", file_path, ")\n")
cat(markdown::mark_html(text=md), file="report-space.html")
browseURL("report-space.html")

# spaces in names not an issue with rmarkdown
cat(md, file="report-space-rmarkdown.md")
rmarkdown::render("report-space-rmarkdown.md", output_format = "html_document", output_file = "report-space-rmarkdown.html")
browseURL("report-space-rmarkdown.html")

image

@yihui yihui added the wontfix label Oct 10, 2023
@yihui
Copy link
Member

yihui commented Oct 10, 2023

That's expected from the CommonMark specs: https://spec.commonmark.org/0.30/#link-destination The link destination should not contain spaces unless it's enclosed in <>. So either avoid using spaces, or enclose the path in <>.

![plot of chunk pressure](<test directory/figure/some-image.png>)

@yihui yihui self-assigned this Oct 10, 2023
@yihui yihui closed this as completed in 61387fd Oct 10, 2023
@vnijs
Copy link
Author

vnijs commented Oct 10, 2023

Thanks for the update @yihui. Is there clean way to enforce this behavior by knitr (i.e., add <> to all image paths)? I tried to create a simple example but the application where this issue comes up is when knitr generates the path to an image created by ggplot, for example, and uses the full path which may have a space in it. If I could force knitr to always add <> for images that would fix the problem.

@yihui
Copy link
Member

yihui commented Oct 11, 2023

I'd like to figure out why the full path came in first. A minimal reprex would help. I guess you are using rmarkdown::render(), but knitr::knit2html(force_v1 = TRUE) should suffice.

@vnijs
Copy link
Author

vnijs commented Oct 11, 2023

The relevant code section is linked below. This is part of a rather large shiny app. In the app students generate code using clicks. The code goes into a shinyAce editor. From there they can use knitr to get a preview of what the report with look like. If all looks good, they can save the report. Saving uses Rmarkdown. The preview uses knitr::knit and markdown::mark_html so the generated output can be nicely embedded in the shiny app without any styling issues.

To make sure students can knit while working inside a shiny app, the working directory has to be changed since they may not have write privileges to the directory where shiny app is installed. To address this issue the app sets the working directory to a temp directory. I have been using this setup with students for a long time and this year there were a few students using windows where the temporary directory has a space because the student's user name had a space (e.g., John Doe). For example, the temp directory might be "c:/Users/John Doe/AppData/Local/Temp/". When knitr creates the markdown file for mark_html to convert to HTML. mark_html thinks the file does not exist due to the space in the path and so the markdown code is shown instead of the figure.

knit_it section

@yihui
Copy link
Member

yihui commented Oct 11, 2023

It makes sense to use the temp dir as the working directory, but I still don't understand why the image path is a full absolute path (in other words, why relative paths can't be used).

@vnijs
Copy link
Author

vnijs commented Oct 11, 2023

Just to be clear, I'm not manually setting the path. knitr is creating the (full) paths when a temp directory is provided. Suppose the temp file is represented by the first path below and the project folder is the 2nd path below. Would knitr figure out the relative path for those two? On macOS the temp directory might look like the 3rd path below. I don't see how would knitr would create a relative path here.

c:/Users/John Doe/AppData/Local/Temp/abc.png

c:/Users/John Doe/classes/mgta455/assignment1/

/var/folders/p7/87yc4ndx5gl1ylnk262xnds80000gn/T//RtmpDDSZ3V

/Users/johndoe/classes/mgta455/my assignment 1/

@yihui
Copy link
Member

yihui commented Oct 11, 2023

If you are not setting the figure paths, I can't think of a reason why they are absolute. By default, R plots are written to the figure/ directory under the current working directory, and you should get relative paths of the form figure/chunk-label.png. As I said above, a minimal reprex can help a lot.

@vnijs
Copy link
Author

vnijs commented Oct 11, 2023

OK. I'll work on that. In the mean time, is there a way to force knitr to add <> around the path?

@yihui
Copy link
Member

yihui commented Oct 11, 2023

You could change the path via the chunk option fig.process:

knitr::opts_chunk$set(fig.process = function(x, options) {
  paste0('<', x, '>')
})

but this should be done for ![]() output only. In some cases, knitr would output HTML <img> tags instead of Markdown. It depends on your use case and can be complicated, so I'd like to figure out the root problem as I mentioned first.

@vnijs
Copy link
Author

vnijs commented Oct 12, 2023

The problem that led me to post this issue is that some students were seeing output from knitr in the shiny app that looks like the below. 99% had no problems. The issues these students were seeing seems to be caused by the fact that there is a space in their username.

image

You can't actually create a username with a space on macOS, so i tried on Windows with the below code.

report <- "
## Hi there

    ```{r}
    plot(1:10)
    ```
"

tdir <- tempdir()
owd <- setwd(tdir)

print(tdir)

md <- knitr::knit(text = report, quiet = TRUE)
print(md)

html <- markdown::mark_html(text = md, template = FALSE, meta = list(css = ""), output = FALSE)

report_file <- paste0(owd, "/report.html")
cat(html, file = report_file)
browseURL(report_file)

setwd(owd)
getwd()

But when I run that I still see that tempfile provides a path without spaces anyway.

user-name-with-space-windows

In sum, I don't understand why these students are seeing paths that show the space but I can't seem to reproduce even on windows after adding a space to my username. If you have ideas, please let me know. I'll reach back out to the students as well.

@yihui
Copy link
Member

yihui commented Oct 12, 2023

Okay, let me investigate tomorrow. Thanks!

@vnijs
Copy link
Author

vnijs commented Oct 13, 2023

I found the cause and it was on my end. For some reason I added the fig.path line below in the app years ago. I can't recall what the reason for the addition was. The current issues with full paths being used was fixed by removing the fig.path line. Apologies for not seeing the issue on my end sooner.

knitr::opts_chunk$set(
  echo = FALSE,
  comment = NA,
  cache = FALSE,
  message = FALSE,
  warning = FALSE,
  error = TRUE,
  fig.path = normalizePath(tempdir(), winslash = "/"), # culprit
  dpi = 144,
  screenshot.force = FALSE
)

@yihui
Copy link
Member

yihui commented Oct 13, 2023

Great! Thanks a lot for letting me know! I'm relieved that it was not due to a bug somewhere in knitr or markdown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

2 participants