How to simply multiply 2 numeric values from Shiny Widgets

Hi all,

Learning Shiny
and using the basic Shiny example (as an RMD),
ref: waiting time btw geyser eruptions.

Runs great!.

But now,
I am trying to do something very simple,
just multiply 2 numeric values:

  • one num value from the dropdown menu widget
  • the other num value from the slider widget

in that example code.

But, after I click [RUN DOCUMENT] in Rstudio,
I keep getting the mssg:
"Error: non-numeric argument to binary operator"...

Here's what my test "shiny.Rmd" file
looks like:
(my attempt to multiply the 2 values
is at the end...).

title: "shiny"
author: "rw"
date: "7/9/2021"
output: html_document
runtime: shiny

Here are two Shiny widgets

selectInput("n_breaks", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 20)

sliderInput("bw_adjust", label = "Bandwidth adjustment:",
              min = 0.2, max = 2, value = 1, step = 0.2)

...that build a histogram.

  hist(faithful$eruptions, probability = TRUE,
       breaks = as.numeric(input$n_breaks),
       xlab = "Duration (minutes)",
       main = "Geyser eruption duration")

  dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")

# a test to do R ops (ie: multiply) w/ 2 widget num values.
  nb <- reactive(input$n_breaks)
 bw <- reactive(input$bw_adjust)

  nb * bw   # nope...

I tried the whole afternoon...(several variations),
including searching in StackO. No luck...

I must be missing something basic in the code.
Could somebody help me, pls?.

latest R, Rstudio, Chromium browser
Ubuntu Linux 20.04

Most of the time this error comes from trying to do a mathematical operation on a character string. In your case, this is because selectInput always returns a character value, so is returning e.g. "10" instead of 10. Coercing the value of input$n_breaks to a numeric before trying to multiply it should resolve the error, i.e..:

nb <- reactive(as.numeric(input$n_breaks))
nb() * bw()

This is why the histogram works - you've used as.numeric.

Edit: in addition to this, the value of the reactives also need to be accessed with nb() and bw() rather than nb and bw, as per @nirgrahamuk answer

1 Like

Thanks, cnbrownlie for the suggestion.

nb <- reactive(as.numeric(input$n_breaks))
bw <- reactive(as.numeric(input$bw_adjust))
nb * bw

but still get the error mssg... :frowning:
"Error : non-numeric argument to binary operator...".

nb is not numeric, it is a reactive. The numeric value can be accessed with nb() from within some reactive contect such as a reactive or observe.
If you want to see the multiplication on the console then add the following into the server code.

cat(req(nb())* req(bw()),"\n")

Thanks for responding renirgrahamuk!.

In my .RMD file (see my 1st post this thread),
I now tried, as you suggested:

nb <- reactive(as.numeric(input$n_breaks))
bw <- reactive(as.numeric(input$bw_adjust))

#nb * bw
cat(req(nb())* req(bw()),"\n")

In my Rstudio Console,
I always see the output:

[1] 6

It's always the SAME result:
(2 * 3 = 6),
no matter which value you choose
in the Menu and Slider widgets...

the results of the multiplication
should appear in the Shiny window itself,
not in the Rstudio Console.

There's gotta be an easy way
to do this simple operation...


Thanks to BOTH:
cnbrownlie AND nirgrahamuk

Either Solution works fine!.
(the system won't allow me
to mark both as "Solutions"...)

Bonus Question:
how do I easily determine if a Shiny Input widget,
returns a numeric or a "character" result?.

Please, keep in mind,
my script is NOT a Shiny file,
but it's an RMD file ,
with embedded Shiny input_ and react_ widgets!.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.