bjorn2bewild
bjorn2bewild

Reputation: 1019

Strange behavior in shiny RMarkdown

I'm having some very strange behavior when knitting a shiny RMarkdown HTML document. In the MWE below I simply (1) include a slider input, (2) include a column in the df as the value of the input and then (3) return the df. The MWE below returns the error Error in : Column "b" must be length 1 (the number of rows), not 0. If I take out the multiplication of the input value when I assign a value to b, the document knits with no problem. MWE:

---
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
```

Testing

```{r eruptions, echo=FALSE}
# Input slider
sliderInput("test", "Test", value = 0.5, min = 0, max = 1, width = "100%")

# Pre-define df
output <- reactiveValues(data = data.frame(a = 20))

observe({

  output$data <- output$data %>%
    # Manipulate df
    mutate(b = input$test * 2)

})

# Return df
renderDataTable({output$data})

```

Does anyone know what could be causing this strange behavior?

Upvotes: 0

Views: 69

Answers (1)

Ben
Ben

Reputation: 30474

Here's what I suspect is going on ---

I think that the first time your observe is run through, that input$test is NULL.

If your mutate sets a column to NULL, it would just remove that column.

If you use input$test * 2 that will return an empty vector numeric(0). Trying to add a column with an empty vector would give the error.

If you add req(input$test) at the beginning of your observe function, does it work?

Upvotes: 1

Related Questions