R RStudio重置调试/功能环境 [英] R RStudio Resetting debug / function environment

查看:608
本文介绍了R RStudio重置调试/功能环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用函数时,我试图阻止R显示功能代码和环境信息。该功能是由教师提供的Coursera R Programming的作业的一部分。这是行为:



R脚本:

  makeVector< -  function(x = numeric()){
m < - NULL
set< - function(y){
x<< - y
m< NULL
}
get < - function()x
setmean< - function(mean)m< - mean
getmean< - function() b $ b列表(set = set,get = get,
setmean = setmean,
getmean = getmean)
}

我在控制台中运行以下操作:

 > x<  -  1:10 
> makeVector(x)

并获取:

  $ set 
function(y)
{
x< - y
m< - NULL
}
< environment:0x000000000967dd58>

$ get
function()
x
< environment:0x000000000967dd58>

$ setmean
函数(平均值)
m< - 平均
<环境:0x000000000967dd58>

$ getmean
function()
m
< environment:0x000000000967dd58>

似乎RStudio正在返回功能代码和环境信息,而不是执行该功能。以前,我运行了debug(ls)和undebug(ls)作为测验的一部分 - 这是我的预感,debug()命令与行为有关。



要解决问题,我已经尝试过:




  • 删除包含RStudio设置的RStudio-Desktop文件夹。
    这将我的外观和全局选项恢复为默认值,但
    函数调用行为仍然发生。

  • 卸载并重新安装R和RStudio。有人知道为什么RStudio正在显示功能代码和环境,而不是在...之上。执行功能?



    我非常感谢帮助!谢谢!

    解决方案

    首先,这与Rstudio无关:Rstudio只是一个IDE,这将是非常奇怪的如果它以某种方式设法混淆你的代码,不是吗?你所看到的行为是完全正常的,它应该是正确的。如果你熟悉OOP,你得到的是一个对象,有几种方法。这是一个小型演示,显示预期用途:

      x < -  1:10 
    xx< - makeVector (x)
    xx $ get()
    #[1] 1 2 3 4 5 6 7 8 9 10
    xx $ getmean()
    #NULL
    xx $ setmean(mean(x))
    xx $ getmean()
    #[1] 5.5
    xx $ setmean(我是一个意思)
    xx $ getmean ()
    #[1]我是一个意思


    I am trying to stop R from displaying function code and environment information when I call a function. This function is part of an assignment for Coursera R Programming that was provided by the instructor. Here is the behavior:

    R script:

    makeVector <- function(x = numeric()) {
            m <- NULL
            set <- function(y) {
                    x <<- y
                    m <<- NULL
            }
            get <- function() x
            setmean <- function(mean) m <<- mean
            getmean <- function() m
            list(set = set, get = get,
                 setmean = setmean,
                 getmean = getmean)
    }
    

    I run the following in the console:

    > x <- 1:10
    > makeVector(x)
    

    And get:

    $set
    function (y) 
    {
        x <<- y
        m <<- NULL
    }
    <environment: 0x000000000967dd58>
    
    $get
    function () 
    x
    <environment: 0x000000000967dd58>
    
    $setmean
    function (mean) 
    m <<- mean
    <environment: 0x000000000967dd58>
    
    $getmean
    function () 
    m
    <environment: 0x000000000967dd58>
    

    It appears RStudio is returning function code and environment information rather than executing the function. Previously I ran debug(ls) and undebug(ls) as part of a quiz - it is my hunch that the debug() command has something to do with the behavior.

    To fix the problem, I have already tried:

    • deleting the RStudio-Desktop folder that contains RStudio settings. This reverted my appearance and global options to default, but the function calling behavior still happens.
    • uninstalling and reinstalling both R and RStudio. The behavior still happens as above.

    Does anyone know why RStudio is displaying function code and environment rather than executing the function?

    I really appreciate the help! Thanks!

    解决方案

    First of all, this has nothing to do with Rstudio: Rstudio is just an IDE, it would be very strange if it somehow managed to mess with your code, wouldn't it? The behaviour you see is completely fine and does exactly what it should. If you are familiar with OOP, what you get is an "object" with several methods. Here's a small demo that shows the intended usage:

    x <- 1:10
    xx <- makeVector(x)
    xx$get()
    # [1]  1  2  3  4  5  6  7  8  9 10
    xx$getmean()
    #NULL
    xx$setmean(mean(x))
    xx$getmean()
    #[1] 5.5
    xx$setmean("Hi, I am a mean")
    xx$getmean()
    #[1] "Hi, I am a mean"
    

    这篇关于R RStudio重置调试/功能环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆