W W
W W

Reputation: 799

print in showNotification the column mean from reactiveValue which is data.frame

I have reactive value which is data.frame and want to:

  1. update only 1 column of the reactive value
  2. print the mean of only 1 column of the reactive value

I don't uderstand why my code doesn't work:

library(shiny)

ui <- fluidPage(actionButton("printMean", "print mean"), 
                actionButton("setZeroToSepal.Length", "setZeroToSepal.Length"))

dt <- iris

server <- function(input, output){
  values <- reactiveValues(x = dt)

  observeEvent(input$printMean, {
    d <- values$x
    showNotification(mean(d$Sepal.Length), type = "default")
  })

  observeEvent(input$setZeroToSepal.Length, {
    values$x$Sepal.Length <- rep(0, nrow(values$x))
  })
}

shinyApp(ui, server)

the error is: Error in $: $ operator is invalid for atomic vectors

Upvotes: 1

Views: 64

Answers (1)

SymbolixAU
SymbolixAU

Reputation: 26258

The issue is with showNotification. I believe it's expecting a character, although it's not well documented, and looking through the source code I can't confirm this. You can use

showNotification(as.character(mean(d$Sepal.Length)), type = "default")

and it should work.

Upvotes: 1

Related Questions