Reactive argument to execute functions

I have a table in flexdashboard whose number of columns can change. I can calculate the alignment of the columns on the fly (the default alignment is treated $23.45as a character vector and thus aligns the value, although it is a number and should be aligned right). The problem is that I cannot pass this alignment back to renderTableas a value alignbecause it is a reactive value.

How to pass reactive alignment back to renderTablefunction argument align? (or an alternative that allows me to display a table with reactive alignment)

MWE

---
title: "test"
output: flexdashboard::flex_dashboard
runtime: shiny
---

```{r}
library(flexdashboard)
library(shiny)
```

Inputs {.sidebar}
-------------------------------------

```{r}
selectInput(
    "ncols", 
    label = "How many columns?",
    choices = 1:5, 
    selected = 5
)
```  

Column  
-------------------------------------

### Test

```{r}
nc <- reactive({input$ncols})
aln <- reactive({substring('lllrr', 1, nc())})

renderTable({
    x <- CO2[1:5, seq_len(nc()), drop = FALSE]
    x[, 1] <- as.character(x[, 1])
    x[3, 1] <- '<b>Mc1</b>'
    x
}, align = aln(), sanitize.text.function = function(x) x)
```

Results in:

 Warning: Error in .getReactiveEnvironment()$currentContext: Operation not 
 allowed without an active reactive context. (You tried to do something 
 that can only be done from inside a reactive expression or observer.)
+3
source share
2

aln() renderText(...) i.e.

renderTable({
    x <- CO2[1:5, seq_len(nc()), drop = FALSE]
    x[, 1] <- as.character(x[, 1])
    x[3, 1] <- '<b>Mc1</b>'
    x
}, align = renderText(aln()), 
   sanitize.text.function = function(x) x)
+7

-, , , . renderTable renderTable({ ... }, ...). . , aln(), reactive . , reactive, , .

, , - renderTable, renderUI. renderUI , . ( .)

renderTable :

renderUI({
  output$table <- renderTable({
      x <- CO2[1:5, seq_len(isolate(nc())), drop = FALSE]
      x[, 1] <- as.character(x[, 1])
      x[3, 1] <- '<b>Mc1</b>'
      x
  }, align = aln(), sanitize.text.function = function(x) x)

  tableOutput("table")
})

: output , , . , renderUI, - renderTable, .

: isolate renderTable. : renderTable , nc(). , renderUI . , renderTable a data.frame , align - . , . nc() renderTable . , renderUI , .

+6

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


All Articles