0
votes

I am learning Shiny and want to run the following basic function. The function simply identifies the class of an object specified in its first (and only) argument.

check_data_type = function(sample_variable) {
  type = class(sample_variable)
  if (type=='numeric') {
    print("Data type is numeric")
    output = 1
  } else {
    print(paste0('Data type is ', type))
    output = 2
  }
  return(output)
}

I want to specify the argument in Shiny via a textInput function as follows:

library(shiny)

ui <- fluidPage(
  textInput(inputId = "keystroke_input", label = "Enter one keystroke here", 
value = NULL),
  textOutput(outputId = "keystoke_class"),
  actionButton("go","Run Function")
)

server <- function(input, output) {
  observeEvent(input$go,
  output$keystoke_class <- renderText({
    check_data_type(input$keystroke_input)
  })
  )
}

shinyApp(ui = ui, server = server)

However, when the check_data_type program is specified via a Shiny UI, the program always classifies the value I enter via the textInput field as "character."

The textInput function, as its name might suggest, appears to be automatically classifying any value it receives as "character" before it is evaluated by the check_data_type function.

I assume this is what's happening because if instead I try to run the following simple arithmetic function within Shiny...

square_the_number = function(sample_Variable) {
  return(sample_variable^2)
}

...I need to first deliberately convert the value entered via the textInput function to a numerical value via the as.numeric() function. To illustrate, when I call the above function within the Shiny server function (see second-to-last line in block below) the program executes properly. Otherwise it will return "Error: non-numeric argument to binary operator."

ui <- fluidPage(
  textInput(inputId = "keystroke_input", label = "Enter one keystroke here", 
value = NULL),
  textOutput(outputId = "keystoke_class"),
  actionButton("go","Run Function")
)

server <- function(input, output) {
  observeEvent(input$go,
  output$keystoke_class <- renderText({
    square_the_number(as.numeric(input$keystroke_input))
  })
 )
}

shinyApp(ui = ui, server = server)

Is there any way to get the textInput function to be class-agnostic so that it will properly classify numeric values via the aforementioned check_data_type function?

I considered the numericInput function but that forces you to specify a drop-down menu and I'd like to keep the input field open-ended.

2
have you tried numericInput() ? - amrrs
yes, as indicated at the end of my original post. The problem with numericInput is - lrclark
Isn't my field open? - amrrs

2 Answers

1
votes

numericInput() is what you are looking for.

ui <- fluidPage(
  numericInput(inputId = "keystroke_input", label = "Enter one keystroke here", 
            value = NULL),
  textOutput(outputId = "keystoke_class"),
  actionButton("go","Run Function")
)

server <- function(input, output) {


  observeEvent(input$go,
               output$keystoke_class <- renderText({
                 (input$keystroke_input)^2
               })
  )
}

shinyApp(ui = ui, server = server)
0
votes

In R shiny we have several options to provide inputs. We have numeric input option also. Check the link that shows all the available options. Also code for each of those is provided. http://shiny.rstudio.com/gallery/widget-gallery.html

If you are new to shiny, you can learn through this tutorial https://shiny.rstudio.com/tutorial/