将参数列表传递给 R Shiny 中的函数
Passing a list of arguments into a function in R Shiny
注意:我知道之前有人问过关于将函数参数作为列表传递的类似问题,但大多数解决方案都依赖于在函数名称之前使用 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)
输出:
我会删除这个:
但如果我这样做:
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 的输出相同?
这是一个实际呈现下拉菜单的版本(box()
函数没有 dropdownMenu
参数,正如@DJC 的示例代码所建议的那样:
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)
注意:我知道之前有人问过关于将函数参数作为列表传递的类似问题,但大多数解决方案都依赖于在函数名称之前使用 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)
输出:
我会删除这个: 但如果我这样做:
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 的输出相同?
这是一个实际呈现下拉菜单的版本(box()
函数没有 dropdownMenu
参数,正如@DJC 的示例代码所建议的那样:
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)