Modify a function in an R package to fit for purpose

128 views Asked by At

The shinydashboard package has three types of menus – messages, notifications, and tasks. I want to use the notifications menu for alerts. But I have to make some modications to the function. The image below is a demo of the notification menu from the package website (link). I want to remove the line that says "you have 3 notifactions" or change it to "you have three alerts". I want also to disable the toggle functionality and repalce the three icons at the top by "Today's Alerts" or something similar. The function to do this in the "dropdownMenu" function in shinydashboard. I can see the function source code by using

             library(shinydashboard)
             body(dropdownMenu)

but need help how to edit and use the modified source code.

enter image description here

1

There are 1 answers

1
Metamorphic On BEST ANSWER

I had to solve a similar problem in revising the answer I made for More efficient plot functions in R when millions of points are present?. Here's a version which is adapted to your needs:

my_dropdownMenu = function(...,
  type = c("messages", "notifications", "tasks"),
  badgeStatus = "primary", icon = NULL, .list = NULL)
{
  message("Got here!")
  # ...
  # (COPY AND PASTE body(mydropdownMenu) HERE)
  # (and then make your modifications)
}

# helper function, see
# https://stat.ethz.ch/pipermail/r-help/2008-August/171217.html
rebindPackageVar = function(pkg, name, new) {
  # assignInNamespace() no longer works here, thanks nannies
  ns=asNamespace(pkg)
  unlockBinding(name,ns)
  assign(name,new,envir=ns,inherits=F)
  assign(name,new,envir=globalenv())
  lockBinding(name,ns)
}

# make sure we can call non-exported functions (like validateStatus())
environment(my_dropdownMenu) = asNamespace("shinydashboard")

# now rebind the dropdownMenu function
rebindPackageVar("shinydashboard", "dropdownMenu", my_dropdownMenu);

Example output. The "Got here!" shows us that our modified version is being executed, and the rest is the normal output of the function:

> dropdownMenu()
Got here!
<li class="dropdown messages-menu">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown">
      <i class="fa fa-envelope"></i>
...