How do I assign the working directory for rmarkdown/knitr interactive doc on shinyApps.io?

一笑奈何 提交于 2020-01-17 06:54:26

问题


I have a fully functional interactive shiny doc using knitr/r markdwon.

However, when I try to publish (deploy) it to shinyApps.io, I get an error saying the .PNG file I try to incorporate into the doc (using readPNG from the png package) is unable to open.

I know the problem is related to working directories.

In my original code I assigned a working directory to my folder (i.e., "C:/Users/NAME/documents/...." that contains both my .rmd file and my .png file. However, obviously my folder doesn't exist on the shinyapps.io.

So how exactly do I set my working directory to open the .png file via my doc on shinyapps.io?

I can't find anywhere that explicitly walks through this process. Please help explain this simply/basically for me.

Example:

Assume I have a folder in "C:/Users/NAME/documents/Shiny" that contains 2 files: "shiny.rmd" and "pic.png". I want "pic.png" to be rendered in my shiny.rmd doc.

Currently I have:

---
title: "TroubleShoot"
output:  html_document
runtime: shiny
---

```{r ,echo=FALSE,eval=TRUE}
library(png)
library(grid)
direct <- "C:/Users/NAME/documents/Shiny/"
img <- readPNG(paste0(direct,"pic.PNG"))
grid.raster(img)
```

How do I rewrite my code so it works on shinyApps.io?

Do I need to create and name folders in specific ways to place my 2 files into before deploying?


回答1:


When you paste the c drive to direct, and read the direct into img, you are sending the app/markdown to your local drive. You need to use a relative path to the project. I would suggest something like:

direct <- "./"

Which uses the relative path, not the absolute path.

That said you might be able to skip that step entirely if the .png is in the same folder as the shiny object. just call the file name and format and leave it as that.

Also, check you use of ". Your syntax highlighting indicates your code won't run properly as it thinks some of your functions and variables are character strings because you've misplaced your quotes around read.png(). That's probably just a copy and paste typo though.

In summary, call the image via grid.raster("./pic.PNG"), and brush up on how working directories and relative paths work.

In the absence of knowing exactly how you're 'setting' your working directory:

In rStudio to correct and easiest approach is to make a 'New Project' for every New Project. You can do this as the first step in your project, or you can add a project file to an existing directory.

When you do this it automatically sets your working directory correctly. The way you seem to be doing it is not correct, you are merely supplying a 'path'. If you were to not want to use a project for some reason (I can't think of a case where that would be right), then you can always use setwd(), however, I believe this needs to be used in every session. A true project doesn't have that requirement.

This will then allow you to deploy your app properly as it will update the working directory to the one on the host machine at shinyapps.io when it is initialised there.

Finally, using the correct relative syntax "./path/to/my.file" to reference all your assets, images, data etc, should correct your issue as stated in the question.



来源:https://stackoverflow.com/questions/43529332/how-do-i-assign-the-working-directory-for-rmarkdown-knitr-interactive-doc-on-shi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!