【发布时间】:2015-02-19 05:25:00
【问题描述】:
我的 server.R 代码:
rf <- randomForest(mpg ~., data=my_data)
shinyServer( function(input, output) {
df_list <- reactive ({ c(as.numeric(input$inputA), as.numeric(input$inputB), as.numeric(input$inputC), as.numeric(input$inputD)) })
df <- reactive ({ as.data.frame(df_list) })
reactive ({ colnames(df) <- c("A", "B", "C", "D") })
prediction <- reactive({ predict(rf, df) })
result <- reactive({as.numeric(prediction)})
output$predictE <- reactive ({result})
})
我的 ui.R 代码:
library(shiny)
shinyUI(pageWithSidebar (
headerPanel("Prediction"),
sidebarPanel(
numericInput(inputId="inputA", label = "", value = 1.0),
numericInput(inputId="inputB", label = "", value = 1.0),
numericInput(inputId="inputC", label = "", value = 1.0),
numericInput(inputId="inputD", label = "", value= 1.0),
submitButton("Submit")
),
mainPanel(
p('Output'),
textOutput('predictE')
)
) )
我正在尝试从 ui.R 中获取一些值,并使用它们来生成基于 RF 模型的预测,并将输出发回。
我可以毫无问题地将 input$inputA 发送回 ui.R,因为我测试了 output$predictE
如果我在 shinyServer 函数内执行 R 命令行中的步骤,预测会产生预期的结果。
不知道哪里出错了。
【问题讨论】:
-
您能否也发布您的
ui.R代码和dput一些数据? -
尝试
output$predictE <- renderPrint({result()})或output$predictE <- renderText({result()}) -
谢谢马克 - 我尝试了这两种方法,但是 R 抛出了一个不能强制类型 'closure' 到类型为 'double' 的向量的错误
标签: r