-
-
Notifications
You must be signed in to change notification settings - Fork 334
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
Including image using blogdown #45
Comments
just store the image in (Ps.: the |
@maxheld83 Thank you for your response. I did what you proposed. But I am still not being able to see the image. Btw I am using wamp server to view my blog generated from blogdown. |
@janeshedev I had problems with this too some time ago and meant to get back to it. As a hack, I found that creating an img folder in public and then putting images in there and referencing this way worked
But I think this is likely to cause more problems so I'm not recommending it |
As @maxheld83 pointed out, I'd also recommend using the A relative path like @pssguy pointed out also works, but it is dependent on the location of the |
Well, using an absolute path also has its drawback. For example, it works if you publish the website to example.com, but it won't work if you publish to a subdirectory like example.com/foo/ (in this case, the absolute path has to be |
Thanks for pointing out the advantages / disadvantages of both the approaches. |
The problem I have with using the
And the directory structure is:
Then RStudio will display the image without giving me an annoying error that the image cannot be found. However, with the default settings, blogdown + Hugo will then create:
Since the It seems like what should happen is for Hugo to copy over I ended up solving this issue by making the following changes in
Now blogdown/hugo creates:
And everything works. I hope this helps anyone else struggling with getting images to work... and please let me know if there is a better way to do this! |
@skadauke That is the best way if you do not use the Serve Site addin to preview your site in the RStudio Viewer but want to preview images right in the source editor. |
@yihui I guess I want both :) |
I have finished the relevant documentation for this issue: |
@yihui is there a way to make What I understand now is that in order to include an external picture into my post, I need to do either one of 2 things:
Any better solutions you might suggest? |
I'm going to second the issues here. I've just spent over 3 hours trying to get this to work. I'm including files in my Rmd so I can generate plots as well as external images. The files fail if they can't find the path from within RStudio but once I get those to pass, the generated paths are preventing things from showing up. The docs aren't clear, or maybe they are to someone who is not trying to do this at 1am... but that's usually my baseline. Here's my use case: readRDS("myfile.RDS") <-- what path can I use that both RStudio and Hugo/blogdown will find and not error? my image <-- what path can I use that both RStudio and Hugo/blogdown will find and render correctly? |
Hi @missaugustina, Messaged you on twitter too. As suggested in this thread, my recommendation is put all static files, like data files (.csv, .RDS, etc.) and images (.jpeg, .gif, .png) into the ImagesFor images referenced in a post, we discussed how to reference a file in that folder in the blogdown demo site and in the book. You can also make files within the The short story is that if you are in a post, Hugo looks for all external files in the Data filesNow data files are a bit trickier, because your .Rmd post looks first in the same folder that the .Rmd file is, so it looks in
Your two examplesreadRDS("myfile.RDS") <-- what path can I use that both RStudio and Hugo/blogdown will find and not error? Answer:
my image <-- what path can I use that both RStudio and Hugo/blogdown will find and render correctly? Answer:
Does that help? Let me know 😍 |
@yihui A RMarkdown should work in local rendering and rendered website in same way.
What's the best way to do that plumbing work?
If you think this direction is good, I'll try to look at the code and get a PR when I got more time. |
I started to use Blogdown two days ago, and this was exactly an issue bothered me when I tried to create my first post. After several trials, I realize that Yihui and most of the people use Mac OS, while I am using Windows 10. And I believe Blogdown behaves differently in Windows OS. When I create a new post by using "Addins"->"New Post", Blogdown creates the *.RMD file in "../content/post" instead of "../static/post" folder, while ".." is my project folder. Under the "../static/post" folder, it actually contains the folder "2015-07-23-r-rmarkdown_files", which should be copied or compiled by RMarkdown during the installation of the package. Once I save my images in "../content/post/img", then those images are available in my post. |
Hugo 0.32 start to support page bundles, which can include various assets with post together. However it's quite complex, and I don't think it's easy to use or familiar for Rmd workflow. One simple hack could be just automate the manual tasks as much as possible:
|
Version 0.6 of blogdown will include a new "Insert Image" RStudio Addin that makes it much easier to add images to blog posts. The feature is already available in the GitHub version of blogdown -- which you can install via The addin was contributed by @lcolladotor and there is a nice introduction to it on his blog. Hopefully this feature will help reduce the friction around the |
That will be great for the workflow of creating new post from scratch inside RStudio. Though I feel it's kind of extra effort because inserting an image in markdown format used to simple and don't need a GUI. blogdown is limited by how |
@gadenbuie I'm wondering if it's such a great idea to create functionality for blogdown only that should really be universal to all of R Markdown. At rstudio::conf I had briefly spoked to @yihui and suggested a feature that would allow dragging+dropping an image file or pasting an image from clipboard directly into the RStudio editor - and RStudio would automatically place a copy of the image in the correct place and insert the code to link to the image. |
This is the script I used to convert existing rmd:
# it's better to use - or _ in file name instead of white space.
# press tab in link to refresh the rstudio preview
# need absoluate path because servr changed current working directory.
process_rmd <- function(rmd_path, site_path = ".") {
# assume asset folder has same name of rmd name
file_name <- basename(rmd_path)
asset_folder <- substr(file_name, 1, nchar(file_name) - 4)
# change relative path of assets folder to absolute path
# to deal with encoding, there are some internal functions. just assume utf 8 here.
# input_lines <- rmarkdown:::read_lines_utf8(rmd_path, getOption("encoding"))
input_lines <- readLines(rmd_path)
input_lines <- gsub(paste0("](", asset_folder, "/"),
paste0("](/", asset_folder, "/"),
input_lines, fixed = TRUE)
post_path <- file.path(site_path, "content", "post", file_name)
writeLines(input_lines, post_path)
# copy asset folder to static/. also assume using / instead of \
asset_path <- file.path(dirname(rmd_path), asset_folder)
static_path <- file.path(site_path, "static")
file.copy(asset_path, static_path, recursive = TRUE)
} |
I think the best way to fix this is to update RMarkdown code, just find out where it says a new post should be saved in "contents" folder, and change that to "static", to keep the consistency for all OS. |
Oh @skadauke you have come here. Apparently I didn't remember your name at rstudio::conf when you mentioned the idea to me, but I did remember your idea and mentioned it when @lcolladotor implemented the "Insert Image" addin: #272 (comment) |
I am struggling with this behaviour... I have R code that renders complex math (mathjax doesn't handle well) to pdf, trims the pdf and saves to png, all in the working directory. This works fine if I knit the file locally, but fails when using serve site. I have worked around this by creating the folder specified by slug, and within the Rmd pointing all outputs to that, but it seems rather inelegant... |
… line command to build.R as per https://blogdown-demo.rbind.io/2017/09/06/adding-r-markdown-documents-of-other-output-formats/ and rstudio/blogdown#45 (comment) with at this stagge relative image reference (??moon reader doesn't auto update).
I am struggling to include the image using blogdown. Specifically where should I store my image ?
I am using the following structure but the image is not displayed. I have saved the image on the
post
folder.The output looks like:
![image](https://cloud.githubusercontent.com/assets/3325086/22296210/4b45871c-e2ce-11e6-84fd-4fee7d1c3c6a.png)
Can anyone tell me where should I store the image and the proper syntax to use?
The text was updated successfully, but these errors were encountered: