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

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

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

更多细节...我正在研究一个学校项目,我们正在尝试找出如何将Microsoft Azure Machine Learning Studio与R集成。这非常令人兴奋。我已经为Azure Machine Learning Studio创建了一个接收输入的端点,以便它可以运行预测算法,我正在尝试与此连接。

不过,我对构建Shiny Web应用程序还很陌生。这是我当前的代码:

应用程序

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))

最佳答案

想通了,如果有人好奇的话。相当基本的问题,但这对我来说是新的。

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

我没有发布代码,因为在这个问题上似乎没有很多兴趣。如果需要,请索取代码。

关于r - Shiny Webapp-调用一个单独的R脚本并返回这些结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40646575/

10-11 22:48