Alternative to global for passing variable to shiny app?

I get the following note when I run devtools::check()

checking R code for possible problems ... NOTE
ShinyChatbot: no visible binding for global variable ‘.Chatbot.title’
ShinyChatbot: no visible binding for global variable ‘.Chatbot.api_key
ShinyChatbot <- function(api_key, workspace, title="Chatbot powered by Watson") {
  .GlobalEnv$.Chatbot.title <- title
  .GlobalEnv$.Chatbot.api_key <- api_key
  .GlobalEnv$.Chatbot.workspace <- workspace
  on.exit(suppressWarnings(rm(list = c(.Chatbot.title, .Chatbot.api_key, .Chatbot.workspace), envir=.GlobalEnv)))
  shiny::runApp(system.file('chat', package='IMWatson'))
}

I need to pass those variable to my app in order and I don't know how to do it without using a global or how to keep the globals and solve the devtools::check() problem.

This previous question is related but the utils::suppressForeignCheck did not solve the problem for me.

Did you try the other solution based on the answer by hadley? That is - add a call to globalVariables.

I was able to solve this by changing my function a bit:

ShinyChatbot <- function(api_key, workspace, title="Chatbot powered by Watson") {
  server_path <- system.file("chat/server.R", package = "IMWatson")
  source(server_path, local = TRUE)
  ui_path <- system.file("chat/ui.R", package = "IMWatson")
  source(ui_path, local = TRUE)
  server_env <- environment(server)

  # Here you add any variables that your server can find
  server_env$title <- title
  server_env$api_key <- api_key
  server_env$workspace <- workspace
  ui <- server <- NULL # avoid NOTE about undefined globals
  server_env <- environment(server)
  app <- shiny::shinyApp(ui, server)
  shiny::runApp(app)
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.