-
Notifications
You must be signed in to change notification settings - Fork 78
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
Comments
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 ![plot of chunk pressure](<test directory/figure/some-image.png>) |
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. |
I'd like to figure out why the full path came in first. A minimal reprex would help. I guess you are using |
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 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. |
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). |
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/ |
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 |
OK. I'll work on that. In the mean time, is there a way to force knitr to add <> around the path? |
You could change the path via the chunk option knitr::opts_chunk$set(fig.process = function(x, options) {
paste0('<', x, '>')
}) but this should be done for |
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. 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. 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. |
Okay, let me investigate tomorrow. Thanks! |
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
|
Great! Thanks a lot for letting me know! I'm relieved that it was not due to a bug somewhere in knitr or markdown. |
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
The text was updated successfully, but these errors were encountered: