Martin Schmelzer
Martin Schmelzer

Reputation: 23899

RStudio Addin: Gadget not working (Dependency issue)

my problem is related to the development of a package. So a minimal reproducible example would mean that you should create that minimal package as well. I am basically trying to build a simple RStudio gadget. Here follows the content of the only .R script within this package:

#' Gadget
#'
#' A short description
#'
#' @usage myAddin()
#' @import shiny
#' @import miniUI
#' @import shinyFiles
#' @import rstudioapi
#' @export


myAddin <- function() {

  requireNamespace("shinyFiles")
  requireNamespace("miniUI")
  requireNamespace("shiny")

  ui <- miniPage(gadgetTitleBar("My Addin"),
                 miniTabstripPanel(miniContentPanel(HTML("<center>"),
      shinySaveButton("backup", "Save file", "Save file as...", filetype = list(RData = "RData"))
    )


  server <- function(input, output, session) {
    volumes <- getVolumes()

    shinyFileSave(input, "backup", roots = volumes,
      session = session, restrictions = system.file(package = "base"))

    observeEvent(input$backup, {
      input <- parseSavePath(volumes, input$backup)
      stopApp()
    })

    observeEvent(input$done, {
      stopApp()
    })
  }
  viewer <- dialogViewer("My Addin", width = 350,
    height = 500)
  runGadget(ui, server, viewer = viewer)
}

In order to make the gadget available in RStudio you have to add the file addins.dcfin the subdirectory inst/rstudio/ within your package root directory (so myAddin/inst/rstudio/addins.dcf). It's content is

Name: myAddin
Description: My first Addin
Binding: myAddin
Interactive: true

If you manage to build this package yourself and install it you will find the addin myAddin in the context menu:

enter image description here

If you start the addin you will see the following:

enter image description here

Now if you click the Save file button a dialog should pop up in which you can select a path and a name to save a file. This will look like this:

enter image description here

Unfortunately this will not work. In the last picture above I made the explicit call library(shinyFiles) before starting the addin. If I don't do this the Save button will not work. Importing shinyFiles or explicitly referring to its functions with :: will not do the job. This is because the package itself (myAddin) is not attached when I start the addin (this is the call RStudio executes when starting the addin: myAddin:::myAddin()).

My question therefore is: how can I make the button work without explicitly attaching the package shinyFiles before starting the addin?

I hope I made my point clear. If you need further information just let me now. I am still very new to package development so please keep that in mind.

Upvotes: 1

Views: 669

Answers (1)

DeanAttali
DeanAttali

Reputation: 26323

After some investigation, I figured out this is a bug with shinyFiles, and I filed a bug report. You can read the bug report for the details.

As for a temporary workaround until it gets properly fixed by the package author: You can add addResourcePath('sF', system.file('www', package='shinyFiles')) inside your myAddin() function. Basically, the problem is that shinyFiles is looking for its JS and CSS files relative to a path that it creates only when the package is attacked, but because the package is never attached, that path doesn't get created, so you can use the same line that shinyFiles uses to do that.

edit: I made a pull request to the package to fix this, so hopefully in a few hours/tomorrow you'll have a working solution without having to do anything

edit 2: the fix has been merged. If you download the latest version of shinyFiles from github, it should work

Upvotes: 3

Related Questions