How to wrap a function around a reactive expression in R Shiny?

Minimal working example

Let's say I want to have a custom version renderDataTablethat I will name myRenderDataTableand work by wrapping around 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)
    }
))

Problem

Unfortunately, it myRenderDataTabledoesn't seem to react like that renderDataTable. Pressing the button Increase inputshould change the table values, but this is not so.

So what's wrong?

Attempt: Call Transfer reactive:

Execution output$table1 <- reactive(myRenderDataTable(input$button))) leads to:

Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error : evaluation nested too deeply: infinite recursion / options(expressions=)?

Attempt: Call Transfer observe:

Execution observe(output$table1 <- myRenderDataTable(input$button))did not affect the problem

+4
source share
2 answers

, input$button " " - input$button + 1 2 . input$button, :

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))
  }
))
+3

, , render *. , , renderDataTable, , , , renderDataTable. , , , (.. funciton ):

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))
    }
))
+1

Source: https://habr.com/ru/post/1538778/


All Articles