Shiny Webapp - 调用单独的R脚本并返回这些结果

时间:2016-11-17 04:11:31

标签: r azure machine-learning shiny

这是我正在尝试做的事情:

构建一个闪亮的Web应用程序,用户可以执行以下操作: 1)将数据输入文本字段, 2)单击提交按钮, 3)提交运行单独的R脚本 4)R脚本运行POST调用,检索生成的JSON 5)R脚本将结果返回到闪亮的Web应用程序 6)Web应用程序显示结果

这是我的问题: 如何从闪亮的网络应用中调用单独的R脚本? 如何将结果返回到我的闪亮网络应用程序?

更多详细信息......我正在开展一个学校项目,我们正在努力弄清楚如何将Microsoft Azure机器学习工作室与R集成。这非常令人兴奋。我为Azure机器学习工作室创建了一个接收输入的终点,因此它可以运行预测算法,我正在尝试与之连接。

我很擅长构建Shiny Web应用程序。这是我目前的代码:

app.r

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Hello Shiny!"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
        selectInput("term", 
                  label = "Term Length", 
                  choices = list("36 months" = "36 months", "60 months" = "60 months"), 
                  selected = 1),
        textInput("my_text", 
                  label = "Text input",
                  value = "Enter text..."),
        numericInput("my_num",
                  label = "Numeric input", 
                  value = 1),
        submitButton(text="Submit"),
        actionButton("do", "Click Me")
      ),

      # Show a plot of the generated distribution
      mainPanel(
        textOutput("text1")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {



   output$text1 <- renderText({
     paste("You have selected ", input$term)
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

用于POST调用机器学习工作室的R脚本:

library("RCurl")
library("rjson")

# Accept SSL certificates issued by public Certificate Authorities
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))

h = basicTextGatherer()
hdr = basicHeaderGatherer()

req =  list(
  Inputs = list(
    "input1"= list(
      list(
        'id' = "1",
        'member_id' = "1",
         ## other variables go here

      )
    )
  ),
  GlobalParameters = setNames(fromJSON('{}'), character(0))
)

body = enc2utf8(toJSON(req))
api_key = "my_personal_key" # Replace this with the API key for the web service
authz_hdr = paste('Bearer', api_key, sep=' ')

h$reset()
curlPerform(url = "my_url",
            httpheader=c('Content-Type' = "application/json", 'Authorization' = authz_hdr),
            postfields=body,
            writefunction = h$update,
            headerfunction = hdr$update,
            verbose = TRUE
)

headers = hdr$value()
httpStatus = headers["status"]
if (httpStatus >= 400)
{
  print(paste("The request failed with status code:", httpStatus, sep=" "))

  # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
  print(headers)
}

print("Result:")
result = h$value()
print(fromJSON(result))

1 个答案:

答案 0 :(得分:2)

想出来,如果有人好奇的话。这是一个基本问题,但这对我来说是新的。

我不需要单独的R脚本。相反,我复制了该代码并将其粘贴到&#34; ObserveEvent()&#34;在服务器端代码中。 &#34; ObserveEvent()&#34;监视要单击的按钮,然后我从用户那里获取输入并将它们发送到Azure机器学习工作室的API POST调用中。然后,在&#34; ObserveEvent()&#34;中,我调用&#34; renderText()&#34;获取结果并将其保存到输出变量,然后传递给UI。

我没有发布代码,因为这个问题似乎没什么兴趣。如果您愿意,请索取代码。