简体   繁体   中英

Modify a function in an R package to fit for purpose

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.

在此输入图像描述

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>
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM