[英]Passing a list of arguments into a function in R Shiny
注意:我认识到之前已经询问过有关通过 function arguments 作为列表的类似问题,但大多数解决方案依赖于在 function 名称之前使用do.call
并且我没有任何运气。
我有一个简单的仪表板,上面有两个盒子。 我想将下拉菜单参数存储为要在其他框中使用的列表。 我要存储的具体参数是: icon
和width
。
以下是应用程序的工作版本,没有存储在列表中的下拉参数:
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)
我想做的是将盒子参数存储在这样的列表中:
box_params <- list(icon = icon("cogs"),
width = "400px")
然后能够按照以下方式调用这些参数:
dropdown(box_params,
label = "Plot Options Box 1",
numericInput(inputId = "box1", label = "Value", min = 1, max = 5, value = 3))
使用do.call
do.call("dropdown", c(box_params,
label = "Plot Options Box 1",
numericInput(inputId = "box1",
label = "Value", min = 1, max = 5, value = 3)))
- 完整代码
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)
-输出
这是一个实际呈现下拉菜单的版本(如@DJC 的示例代码所示, box()
function 没有dropdownMenu
参数):
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)
我会删除这个:但如果我这样做:
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)
所以这和@akrun的output是一样的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.