【问题标题】:Problems with 'slider' function in RR中“滑块”功能的问题
【发布时间】:2014-10-06 05:26:11
【问题描述】:

今天我对 R 中的函数 'slider' 有某种问题(使用 R studio 3.1.1)。 这是错误的文本:

滑块错误(62, 74, step = 0.5):未使用的参数(step = 0.5)

我正在尝试在没有此参数的情况下使用此函数,但它显示了一个新错误:

tclVar(sl.defaults[i]) 中的错误:

参数“sl.defaults”丢失,没有默认值

然后送我去回溯! 哪里出了问题?

附言这是我的代码:

    library(manipulate)
library(UsingR)
data(galton)

myHist <- function(mu) {
 hist(galton$child, col = "red", breaks = 100)
 lines(c(mu, mu), c(0, 150), col = "green", lwd = 5)
 mse <- mean((galton$child - mu)^2)
 text(63, 150, paste("mu = ", mu))
 text(63, 150, paste("MSE = ", round(mse, 2)))
}

manipulate(myHist(mu), mu = slider(62, 74, step = 1))

【问题讨论】:

    标签: r compiler-errors syntax-error


    【解决方案1】:

    您正在使用“aplpack”库中的滑块功能。

    您需要指定滑块功能来自操纵。

    manipulate(myHist(mu), mu = manipulate::slider(62, 74, step = 1))
    

    要查看此内容,请尝试在控制台中仅输入不带参数的函数。

    > slider
    function code ...
    <environment: namespace:aplpack>
    

    编辑:我还使用 ggplot2 重写了您的 myHist 函数,它比基本绘图包更强大。这是一个很好的学习包,因此希望此代码可以帮助您入门。

    myHist <- function(mu) {
    
      mse <- mean((galton$child - mu) ^ 2)
      qplot(galton$child, binwidth = 0.1) + geom_vline(xintercept = mu, color = "red") + 
        annotate("text", x = 65, y = 150, label = paste("mu = ", mu)) +
        annotate("text", x = 65, y = 145, label = paste("MSE = ", round(mse, 2)))
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 2016-02-09
      • 1970-01-01
      • 2021-04-02
      相关资源
      最近更新 更多