Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing a list of arguments into a function in R Shiny

Note: I recognize that similar questions about passing function arguments as a list have been asked previously, but most solutions rely on using do.call ahead of the function name and I have not had any luck with that approach.

I have a simple dashboard with two boxes on it. I would like to store the dropdown menu parameters as a list to be used in other boxes. The specific parameters I would like to store are: icon, and width.

Below is a working version of the application without the dropdown parameters stored in a list:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(title = "Box 1",
        dropdownMenu = dropdown(icon = icon("cogs"), 
                                width = "400px",
                                label = "Plot Options Box 1",
                                numericInput(inputId = "box1", label = "Value", min = 1, max = 5, value = 3)
        )
    ),
    box(title = "Box 2",
        dropdownMenu = dropdown(icon = icon("cogs"), 
                                width = "400px",
                                label = "Plot Options Box 2",
                                numericInput(inputId = "box2", label = "Value", min = 1, max = 5, value = 3)
        )
    )
  )
)

server <- function(input, output) { }

shinyApp(ui, server)

What I would like to do is store the box parameters in a list like this:

box_params <- list(icon = icon("cogs"), 
                   width = "400px")

And then to be able to call these parameters along these lines:

dropdown(box_params,
         label = "Plot Options Box 1",
         numericInput(inputId = "box1", label = "Value", min = 1, max = 5, value = 3))

>Solution :

Use do.call

do.call("dropdown", c(box_params, 
                      label = "Plot Options Box 1",
                      numericInput(inputId = "box1", 
      label = "Value", min = 1, max = 5, value = 3)))

-full code

library(shiny)
library(shinydashboard)
library(shinyWidgets)
box_params <- list(icon = icon("cogs"), 
                   width = "400px")



ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(title = "Box 1",
        dropdownMenu = do.call("dropdown", c(box_params, list(label = "Plot Options Box 1",
                                                              numericInput(inputId = "box1", label = "Value", min = 1, max = 5, value = 3))))
    ),
    box(title = "Box 2",
        dropdownMenu = do.call("dropdown", c(box_params, list(label = "Plot Options Box 2",
                                                              numericInput(inputId = "box2", label = "Value", min = 1, max = 5, value = 3))))
    )
  )
)

server <- function(input, output) { }

shinyApp(ui, server)

-output

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading