Search code examples
rshinyshinydashboard

Embedding functions into selectizeinput in shinydashboard


I am trying to add functions to the selectizeInput holder in my shinydashboard to use them interactively on my dataframe. Is there a way to display a name for each function (e.g monthly and annual) instead of having the function itself printed out?

ibrary(shiny)
library(shinydashboard)

annual <- function(x){
  (x/lag(x, 12) - 1)*100
  
}

monthly <- function(x){
  (x/lag(x) - 1)*100
  
}

ui <- dashboardPage(
                    dashboardHeader(title = 'Dashboard'),
                    dashboardSidebar(sidebarMenu
                                                (menuItem(tabName = 'Panel1', text = 'Panel 1')
                                                 )
                   ),
                    dashboardBody(
                                  tabItems(tabItem(tabName = 'Panel1',
                                                   fluidRow(box(selectizeInput('select', 'Select', 
                                                                                choices = c(monthly, annual)),height=80,width=4,
                                                               )
                                                            ),
                                                   fluidRow(box(width = 13, height = 655))
                                                    )
                                            )
                                   )
                     )


server <- function(input, output) {
  
  
}


shinyApp(ui, server)

enter image description here


Solution

  • You could use a named vector to add labels for the choices:

    library(shiny)
    library(shinydashboard)
    
    annual <- function(x) {
      (x / lag(x, 12) - 1) * 100
    }
    
    monthly <- function(x) {
      (x / lag(x) - 1) * 100
    }
    
    ui <- dashboardPage(
      dashboardHeader(title = "Dashboard"),
      dashboardSidebar(sidebarMenu
      (menuItem(tabName = "Panel1", text = "Panel 1"))),
      dashboardBody(
        tabItems(tabItem(
          tabName = "Panel1",
          fluidRow(box(selectizeInput("select", "Select",
            choices = c("monthly" = monthly, "annual" = annual)
          ), height = 80, width = 4, )),
          fluidRow(box(width = 13, height = 655))
        ))
      )
    )
    
    server <- function(input, output) {
    
    }
    
    shinyApp(ui, server)
    #> 
    #> Listening on http://127.0.0.1:6875