1

So, my goal here is to create an Shiny application which takes user's input, changes it into a matrix, then uses a pre-saved Keras model (basically saved weights) to predict a certain variable of his (and prints it). I am new to Keras and Shiny, so I'd appreciate any help. My program shows the user input correctly, but it is evaluating and printing the output where the problems begin. Any help will be appreciated! Code is below:

server <- function(input, output) {
  hisdata <- matrix(cbind(input$VK_001, input$VK_002, input$VK_003, input$VK_004,input$VK_005,input$VK_006,input$VK_006, input$VK_007, input$VK_008, input$VK_009,input$VK_010,input$VK_011, input$VK_012, input$VK_013,input$VK_015, input$VK_017, input$VK_018, input$VK_021, input$VK_022, input$VK_024, input$VK_025, input$VK_027, input$VK_028, input$VK_030, input$VK_031, input$VK_033, input$VK_034, input$VK_036, input$VK_037, input$VK_039, input$VK_040, input$VK_041))
  test_data <- hisdata
  model <- load_model_hdf5("my_model_bft_003.h5")
  test_predictions <- model %>% predict(test_data)
  output$oid1 <- renderPrint({x <- test_predictions[ , 1]
  print(x)
  })
}
ui <- fluidPage(

  # App title ----
  titlePanel("Shiny + KEras!"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: numeric----

      numericInput("VK_001", "Value:", 10, min = 1),
      numericInput("VK_002", "Value:", 10, min = 1),
      numericInput("VK_003", "Value:", 10, min = 1),
      numericInput("VK_004", "Value:", 10, min = 1),
      numericInput("VK_005", "Value:", 10, min = 1),
      numericInput("VK_006", "Value:", 10, min = 1),
      numericInput("VK_007", "Value:", 10, min = 1),
      numericInput("VK_008", "Value:", 10, min = 1),
      numericInput("VK_009", "Value:", 10, min = 1),
      numericInput("VK_010", "Value:", 10, min = 1),
      numericInput("VK_011", "Value:", 10, min = 1),
      numericInput("VK_012", "Value:", 10, min = 1),
      numericInput("VK_013", "Value:", 10, min = 1),
      numericInput("VK_015", "Value:", 10, min = 1),
      numericInput("VK_017", "Value:", 10, min = 1),
      numericInput("VK_018", "Value:", 10, min = 1),
      numericInput("VK_021", "Value:", 10, min = 1),
      numericInput("VK_022", "Value:", 10, min = 1),
      numericInput("VK_024", "Value:", 10, min = 1),
      numericInput("VK_025", "Value:", 10, min = 1),
      numericInput("VK_027", "Value:", 10, min = 1),
      numericInput("VK_028", "Value:", 10, min = 1),
      numericInput("VK_030", "Value:", 10, min = 1),
      numericInput("VK_031", "Value:", 10, min = 1),
      numericInput("VK_033", "Value:", 10, min = 1),
      numericInput("VK_034", "Value:", 10, min = 1),
      numericInput("VK_036", "Value:", 10, min = 1),
      numericInput("VK_037", "Value:", 10, min = 1),
      numericInput("VK_039", "Value:", 10, min = 1),
      numericInput("VK_040", "Value:", 10, min = 1),
      numericInput("VK_041", "Value:", 10, min = 1)
    ),

  # Main panel for displaying outputs ----
  mainPanel(
    h4('Predicted values'),
    verbatimTextOutput("oid1"))

  )
)
shinyApp(ui = ui, server = server)

The desired output is a 1x1 vector of values (predicted value). An input consists of 1x32 (1 row, 32 columns) matrix of numeric values (integers) from 1 to 10.

  • 1
    Welcome to StackOverflow! For code debugging please always ask with a [reproducible](https://stackoverflow.com/q/5963269/1422451) example per the [MCVE](https://stackoverflow.com/help/mcve) and [`r`](https://stackoverflow.com/tags/r/info) tag description, with the desired output. You can use `dput()`, `reprex::reprex()` or built-in data sets for reproducible data. (that's a canned comment). OK, now, in this case perhaps we can find and use a minimal Keras example from the internet and modify the code in your question accordingly? – Hack-R Aug 24 '18 at 19:32

1 Answers1

0

There is a R package call reticulate, that make you able to use python code in R.

library(reticulate)

keras <- import("keras")
# '$' in R is equal to '.' in python
# Equivalent to model = keras.models.load_model("my_model_bft_003.h5") in python
model <- keras$models$load_model("my_model_bft_003.h5")
# Equivalent to model.predict(test_data)  in python
test_predictions <- model$predict(test_data)
Jim Chen
  • 3,262
  • 3
  • 21
  • 35