0
votes

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
maybe worth considering that rmarkdown files, when compiled treat the folder they are in as the root working directory, and do not tend to respect the project set working directory unless you model them as such. Can you map out the exact structure of your directories for both the .Rmd and .png?DaveRGP
@DaveRGP what do you mean by map out?theforestecologist
meta.stackexchange.com/questions/147467/… that said, i think I've understood your directory strucutre from closer reading of your text. Just helps to have visuals, which as an R user you know already ;pDaveRGP

1 Answers

0
votes

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.