【问题标题】:Receiving NULL when sending function output to ui.R in server.R将函数输出发送到 server.R 中的 ui.R 时接收 NULL
【发布时间】: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 &lt;- renderPrint({result()})output$predictE &lt;- renderText({result()})
  • 谢谢马克 - 我尝试了这两种方法,但是 R 抛出了一个不能强制类型 'closure' 到类型为 'double' 的向量的错误

标签: r


【解决方案1】:

您似乎并没有真正理解reactive 是什么。我建议你关注tutorial。但是,这里似乎您不需要任何反应,而您需要以某种方式呈现输出。试试这个:

rf <- randomForest(mpg ~., data=my_data)
shinyServer( function(input, output) {

output$predictE <- renderText({
  df_list <-c(as.numeric(input$inputA), as.numeric(input$inputB), as.numeric(input$inputC), as.numeric(input$inputD))
  df <- as.data.frame(df_list) 
  colnames(df) <- c("A", "B", "C", "D")
  prediction <- predict(rf, df)
  result <- as.numeric(prediction)
  result
})
})

【讨论】:

  • 啊,谢谢!我确实需要转置我忘记做的 df。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 2018-09-02
  • 2021-12-06
相关资源
最近更新 更多