 # how to extract numericInput() value and use it as integer?

i am doing some simple calculations using R and presenting them in shiny where some variables of the calculations should be inserted by the user.

in the ui <-
I have
Q1 <- numericInput("myInput1", "question1", value= 1)

I want to extract the Q1 numeric integer value which in this case number 1 and use it in another equation, for example:

B= Q1+1

the Q1 is a numericInput inside shiny ui and a user could change the value by typing a different value ex: number 2 . the new value should be used to calculate Q1 value which is 2 + 1 = 3 the value of B

when I run the app, I face this error message:
Error in Q1 + 1 : non-numeric argument to binary operator

thank you in advance for looking at my problem and appreciate your help

thanks
Ahmed Ali
PhD candidate
Chung Ang University

The value of the numericInput is part of the input list, You refer to it with its inputId, myInput1, as input\$myInput1. Here is a very simple example. Note that the calculations are done in the server section of the app.

``````library(shiny)

ui <- fluidPage(
titlePanel("Title"),
mainPanel(
numericInput("myInput1", label = "Number", value = 1),
plotOutput("Plot")
)
)

server <- function(input, output) {
output\$Plot <- renderPlot({
Yvals <- seq(from = input\$myInput1, to = input\$myInput1 *2, length.out = 5)
plot(1:5, Yvals)
})

}

shinyApp(ui = ui, server = server)
``````
1 Like

so in the below example:
in ui<-
user insert "myInput1" value then
in server<- B = input\$myInput1+2

my questions is: how can I use the "B" value as integer in further integer calculations. for example: I want to use B+2= C then C+2=D finally print D as final result in shiny?

concept in shiny: user insert myInput1 value and get the result of D

library(shiny)

ui <- fluidPage(
titlePanel("Title"),
mainPanel(
numericInput("myInput1", label = "Number", value = 1),
uiOutput("ui")
)
)

server <- function(input, output) {

output\$ui <- renderUI( {
tagList(
tags\$h2("Display Result in %"),
numericInput("obs1", "B", value = input\$myInput1+2))
})
}

shinyApp(ui = ui, server = server)

If you just want to display the result of a calculation, you can use renderUI as shown below.

``````library(shiny)

ui <- fluidPage(
titlePanel("Title"),
mainPanel(
numericInput("myInput1", label = "Number", value = 1),
uiOutput("ui"),
uiOutput("ui2")
)
)

server <- function(input, output) {

output\$ui <- renderUI( {
tagList(
tags\$h2("Display Result in %"),
numericInput("obs1", "B", value = input\$myInput1+2))
})
output\$ui2 <- renderUI({
C <- input\$myInput1 + 2
D <- C^2
tags\$div(paste("The processed input is", D))
})
}

shinyApp(ui = ui, server = server)
``````
2 Likes

Dear FJCC
thanks alot. that solved the problem

have a blessed day

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