简体   繁体   中英

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:

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

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

在此处输入图像描述

Here is a version which actually renders the dropdown menu (The box() function doesn't have a dropdownMenu argument as @DJC's example code suggests):

结果

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

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

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(title = "Box 1",
        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",
        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)

I will delete this: but If i do:

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

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

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

server <- function(input, output) { }

shinyApp(ui, server)

I get this: 在此处输入图像描述

So this is the same of @akrun's output?

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