如何在R中围绕反应式表达式包装函数? 最小工作示例

如何在R中围绕反应式表达式包装函数? 最小工作示例,r,shiny,R,Shiny,假设我想要一个自定义版本的renderDataTable,我将其命名为myRenderDataTable,并通过环绕renderDataTable工作: library(shiny) runApp(list( ui = basicPage( actionButton("button", "Increase input"), tabsetPanel( tabPanel("table1", shiny::dataTableOutput(

假设我想要一个自定义版本的
renderDataTable
,我将其命名为
myRenderDataTable
,并通过环绕
renderDataTable
工作:

library(shiny)
runApp(list(
    ui = basicPage(
        actionButton("button", "Increase input"),
        tabsetPanel(
            tabPanel("table1", shiny::dataTableOutput("table1")),
            tabPanel("table2", shiny::dataTableOutput("table2")),
            tabPanel("table3", shiny::dataTableOutput("table3"))
        )
    ),
    server = function(input, output) {
        myRenderDataTable <- function(a) {
            renderDataTable(
                data.frame(x = a, y = a^2, z = a^3),
                options = list(bPaginate = as.logical(a %% 2))
            )
        }
        output$table1 <- myRenderDataTable(input$button)
        output$table2 <- myRenderDataTable(input$button + 1)
        output$table3 <- myRenderDataTable(input$button + 2)
    }
))
尝试:将调用传递到:
执行
观察(output$table1我认为您低估了render*函数的魔力。从这个示例来看,我认为您不需要自定义的
renderDataTable
函数,我认为您需要一个自定义函数来构建一个表,然后可以将它传递给内置的
renderDataTable
。我认为这就是您想要的如果需要,包装的顺序正好相反(即,反应式表达式中的自定义函数):

库(闪亮)
runApp(列表(
ui=基本页面(
操作按钮(“按钮”,“增加输入”),
选项卡面板(
选项卡面板(“表1”,dataTableOutput(“表1”),
选项卡面板(“表2”,dataTableOutput(“表2”),
tabPanel(“表3”,dataTableOutput(“表3”))
)
),
服务器=功能(输入、输出){

myDataTable问题在于,
input$button
被“急切地”评估-即,
input$button+1
第一次运行时评估为2,然后再也不会更改。您可以通过显式地将其设置为反应式,使其每次更改时都进行评估:

library(shiny)
runApp(list(
  ui = basicPage(
    actionButton("button", "Increase input"),
    tabsetPanel(
      tabPanel("table1", shiny::dataTableOutput("table1")),
      tabPanel("table2", shiny::dataTableOutput("table2")),
      tabPanel("table3", shiny::dataTableOutput("table3"))
    )
  ),
  server = function(input, output) {
    myRenderDataTable <- function(a) {
      renderDataTable(
        data.frame(x = a(), y = a()^2, z = a()^3),
        options = list(bPaginate = as.logical(a() %% 2))
      )
    }
    output$table1 <- myRenderDataTable(reactive(input$button))
    output$table2 <- myRenderDataTable(reactive(input$button + 1))
    output$table3 <- myRenderDataTable(reactive(input$button + 2))
  }
))
库(闪亮)
runApp(列表(
ui=基本页面(
操作按钮(“按钮”,“增加输入”),
选项卡面板(
tabPanel(“table1”,Shining::dataTableOutput(“table1”),
tabPanel(“table2”,Shining::dataTableOutput(“table2”),
tabPanel(“表3”,闪亮::dataTableOutput(“表3”))
)
),
服务器=功能(输入、输出){

myRenderDataTable谢谢,但在您上面发布的代码中,我看不出您对我最初所做的做了任何更改。此外,我确实需要一个自定义的
renderDataTable
函数-我已经修改了我在问题中发布的代码以反映这一点。具体来说,我现在希望datatable具有分页(
bPaginate
选项)动态设置(我知道有点做作)。alexwhan我希望你不介意,我编辑了你的代码以符合你的解释。谢谢@hadley,但是看起来
bPaginate
选项即使在
input$按钮更改后仍然没有得到评估。表不会从初始分页状态更改。
library(shiny)
runApp(list(
    ui = basicPage(
        actionButton("button", "Increase input"),
        tabsetPanel(
            tabPanel("table1", dataTableOutput("table1")),
            tabPanel("table2", dataTableOutput("table2")),
            tabPanel("table3", dataTableOutput("table3"))
        )
    ),
    server = function(input, output) {
        myDataTable <- function(a) {
            data.frame(x = a, y = a^2, z = a^3)
        }
        output$table1 <- renderDataTable(myDataTable(input$button))
        output$table2 <- renderDataTable(myDataTable(input$button + 1))
        output$table3 <- renderDataTable(myDataTable(input$button + 2))
    }
))
library(shiny)
runApp(list(
  ui = basicPage(
    actionButton("button", "Increase input"),
    tabsetPanel(
      tabPanel("table1", shiny::dataTableOutput("table1")),
      tabPanel("table2", shiny::dataTableOutput("table2")),
      tabPanel("table3", shiny::dataTableOutput("table3"))
    )
  ),
  server = function(input, output) {
    myRenderDataTable <- function(a) {
      renderDataTable(
        data.frame(x = a(), y = a()^2, z = a()^3),
        options = list(bPaginate = as.logical(a() %% 2))
      )
    }
    output$table1 <- myRenderDataTable(reactive(input$button))
    output$table2 <- myRenderDataTable(reactive(input$button + 1))
    output$table3 <- myRenderDataTable(reactive(input$button + 2))
  }
))