MYjx
MYjx

Reputation: 4407

Embed more than 1 shiny application in Rmarkdown using shinyAppDir

I want to embed two separate shiny apps in rmarkdown. Where should I put the rmd file?

When I only had one shiny app, I put the rmd in the directory as the shiny app and it worked. The codes are as below:

```{r, echo=FALSE}
shinyAppDir(
 "E:/example/shinyfolder",
  options=list(
    width="100%", height=550
  )
)

```

But when I move the rmd to a upper level of shiny app folder, say, E:/example and use the exact absolute path as I used before, the error said cannot open compressed file './Data/joined1.rda', probable reason 'No such file or directory', My idea is put the rmd in the upper level folder and reference the different apps in subfolders. Any idea about how to fix it?

Upvotes: 2

Views: 934

Answers (1)

Christoph
Christoph

Reputation: 696

This is probably related to your use of relative paths within your shiny application. Since I assume joined1.rda is a data file one of your shiny apps tries to read in, it cannot find it anymore as soon as the shiny app files (server.R and ui.R) are no longer in the same folder as your markdown file. Use paths relative to the path of your markdown file or explicitly set one with setwd(), either in your markdown file or within the app(s).

Assuming a folder/file structure like this...

/parentDir        (markdown.rmd)
    /apps
       /app1      (server.R, ui.R)
       /app2      (server.R, ui.R)
          /data   (joined1.rda)

...and further assuming your working directory is ~/parentDir you can embed your shiny applications with shinyAppDir(".apps/app1") and shinyAppDir("./apps/app2"), respectively.

Within your shiny applications (server.R) you also either use absolute paths to your /data folder (if you really want to use absolute paths at all...), or you use relative paths according to your working directory. So if the second shiny app (in /app2) loads data from your /data folder, you use load("./apps/app2/data/joined1.rda").

Upvotes: 1

Related Questions