这是我正在尝试做的事情:
构建一个 Shiny 的网络应用程序,用户可以在其中执行以下操作: 1)将数据输入文本字段, 2)点击提交按钮, 3) 提交运行单独的 R 脚本 4) R 脚本运行 POST 调用并检索结果 JSON 5) R 脚本将结果返回到 Shiny 的 Web 应用程序 6) Web应用程序显示结果
这是我的问题: 如何从我 Shiny 的网络应用程序中调用单独的 R 脚本? 如何将结果返回到我 Shiny 的网络应用程序?
更详细一点...我正在开展一个学校项目,我们正在尝试找出如何将 Microsoft Azure 机器学习工作室与 R 集成。这非常令人兴奋。我已经为 Azure 机器学习工作室创建了一个端点来接收输入,以便它可以运行预测算法,我正在尝试与之连接。
不过,我对构建 Shiny 的网络应用程序还很陌生。这是我当前的代码:
应用程序.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))
最佳答案
如果有人好奇的话,我已经弄清楚了。这是一个相当基本的问题,但对我来说是新的。
我不需要单独的 R 脚本。相反,我复制了该代码并将其粘贴到服务器端代码的“ObserveEvent()”中。 “ObserveEvent()”监视要单击的按钮,然后我获取用户的输入并将其通过 API POST 调用发送到 Azure 机器学习工作室。然后,在“ObserveEvent()”中,我调用“renderText()”来获取结果并将其保存到输出变量,然后将其传递到 UI。
我没有发布代码,因为似乎对这个问题没有太多兴趣。如果您愿意,请请求代码。
关于r - Shiny Webapp - 调用单独的 R 脚本并返回这些结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646575/