【问题标题】:R shiny not taking user input correctlyR闪亮没有正确接受用户输入
【发布时间】:2015-04-28 19:44:10
【问题描述】:

我确定是非常基本的 R 闪亮问题。我想在玩具数据集上运行线性回归,使用它来根据使用的输入预测相应的值,并从给定值和预测值绘制一条线到回归线。

当我运行它时,我得到了错误: 收听http://XXX 警告:“newdata”有 1 行,但找到的变量有 6 行 段错误(newData, 0, newData, newData.pred, col = "red") : 第一个参数无效

所以我意识到这是我处理用户输入的方式。我只是想获取他们给出的数值,将其提供给 predict.lm 并绘制结果线。如果可能的话,我也喜欢文本以提供 predict.lm 输出。

ui.R

 library(shiny)
 (pageWithSidebar(
 headerPanel('Predicty'),
sidebarPanel
(
numericInput('clusters', 'Absorbance', 0.1, min = 0, max = 100, step = 0.00001)
),
mainPanel
(
  plotOutput('plot1')
)
))

服务器.R

 library(shiny)

##  Initial data and linear regression. 
conc <- c(.25, .5, 1, 1.5, 2, 2.5) 
abs <- c(.332, .631, .972, 1.201, 1.584, 1.912) 
results <- data.frame(abs,conc) 
abss <- lm(conc~abs, data = results)


shinyServer(function(input, output) {

output$plot1 <- renderPlot({


 newData.in <- reactive({
inFile <- input$clusters
inFile
 })

 newData <- data.frame(newData.in()) 
newData.pred <- predict.lm(abss, newData) 

plot(abs,conc) 
abline(abss)

segments(newData , 0, newData, newData.pred, col = "red")
 # segments(0, newData.pred[i], clusters()$centers, newData.pred[i], col = i)

  })

})

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您的renderPlot 中不需要reactive,函数已经是reactive。

    predict.lm 在您的输入中没有找到任何列名abs,这会引发第一个警告。

    segments 将坐标作为参数,您输入了 data.frame,它解释了第二个错误。

    你可以这样做:

     output$plot1 <- renderPlot({   
        newData <- data.frame(abs=input$clusters) 
        newData.pred <- predict.lm(abss, newData) 
        abline(abss)   
        segments(newData$abs , 0, newData$abs, newData.pred, col = "red")
    
      })
    

    此外,您可能希望更改绘图的 xlim 或输入的起始值,因为 0.1 不在绘图上

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 2015-05-06
      • 2020-10-11
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      相关资源
      最近更新 更多