2

I wanted to create an interactive Shiny app where users could input values for a dataframe, and then it would apply that dataframe to a predict in an svm, giving the output of the prediction based on the given values. However, I can't seem to figure out how to save a reactive in the format that I can apply to the predict function. Can anyone help me?

My server.R file:

library(e1071)

X1 <- runif(100)
X2<- runif(100)          
Y <-runif(100) 
df <-data.frame(Y,X1,X2)

svm(Y~X1+X2, data=df, probability=TRUE)

svm.test<-svm(Y~X1+X2, data=df, probability=TRUE)




library("shiny")
shinyServer(
function(input,output,session){

tableStart <- data.frame(X1=4, X2=6)

newEntry <- reactive({
  input$update
  newLine <- isolate(data.frame(X1=input$X1, X2=input$X2))
})
output$table1 <- renderTable({rbind(tableStart, newEntry())})


predictions<-predict(svm.test, newEntry, probability=TRUE)    

My ui.R file:

library("shiny")    
shinyUI(pageWithSidebar(headerPanel("Adding entries to table"),
                    sidebarPanel( sliderInput("X1", 
                                              label = "X1:",
                                              min = 0, max = 10, value = 0),

                                  sliderInput("X2", 
                                              label = "X2:",
                                              min = 0, max = 10, value = 0),
                                 actionButton("update", "Update Table")),
                    mainPanel(tableOutput("table1"))))

Any help you all can give me on how to save the output as a data.frame or vector that can be used in the predict function would be greatly appreciated.

Thanks!

Edited to reflect the omission noted in the first answer.

2 Answers2

1

On page 39 of: http://cran.r-project.org/web/packages/e1071/e1071.pdf which is the documentation for the svm functions, it indicates that the function "predict" will return your results.

so try changing this:

predict(svm.test, newEntry, probability=TRUE) 

to something like this:

predictions <- predict(svm.test, newEntry, probability=TRUE) 

note that your first call:

svm(Y~X1+X2, data=df, probability=TRUE)

does nothing since the next call captures the svm object (svm.test variable)

Furthermore, since you will be returning probabilities, your "predictions" variable will be an array with M rows (# things to predict) and N columns (number of classes)...so be aware of that when constructing a dataframe from the result.

Hope this helps.

user1269942
  • 3,772
  • 23
  • 33
  • Good catch, and thanks! I screwed that up when I put together the code and definitely need to store the predictions in an object. I still don't know how to get the Shiny object to work with the predict function though - it gives me the error message "Error in 1:nrow(newdata) : argument of length 0" –  May 28 '15 at 17:53
  • glad to help. "Any help you all can give me on how to save the output as a data.frame or vector that can be used in the predict function would be greatly appreciated. " is a bit vague...perhaps you can elaborate on what you are seeking to do with the results. – user1269942 May 28 '15 at 17:56
  • Sure - Shiny seems to save the reactive function (the results of newEntry) as something that's not a typical dataframe. I think I read somewhere it saves it as a formula or something similar. When I try to use newEntry as a vector of values to enter into the predict function, it gives me the "Error in 1:nrow(newdata) : argument of length 0" error and I'm not sure how to fix that. –  May 28 '15 at 18:00
  • 1
    @cmurp002 would this be of help: http://stackoverflow.com/questions/23038580/r-shiny-handling-handling-error-from-empty-data-frames – user1269942 May 29 '15 at 16:26
  • 1
    input$update line doesn't look needed. also, newLine in your reactive function does not get returned...if that's what you want....return(newLine) ? – user1269942 May 29 '15 at 16:44
0

I think I found the answer on Hack-R's github (which I found from a search here).

https://github.com/hack-r/coursera_shiny

I don't really understand the code, but I think I can cannibalize it to my full example. Thanks to user1269942 for the help, and to Hack-R for posting his finished product on git.

output$text <- renderPrint({
  {  ds1        <- values$df 
  a <- predict(svm.test, newdata = data.frame(ds1))
  names(a) <- NULL
  cat(a)

  }
})