Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive argument to render functions

I have a table in a flexdashboard whose number of columns can change. I can compute the alignment of the columns on the fly (default alignment treats $23.45 as a character vector and thus left aligns the value though it is a number and should be right aligned). The problem is I can't pass this alignment back to renderTable as a value to align because it's a reactive value.

How can I pass the reactive alignment back to the renderTable function's align argument? (or an alternative that let's me render a table with a 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.)
like image 793
Tyler Rinker Avatar asked Dec 10 '22 08:12

Tyler Rinker


1 Answers

Try wrapping aln() in 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)
like image 123
Weihuang Wong Avatar answered Jan 05 '23 10:01

Weihuang Wong