gpt4 book ai didi

r - RStudio中的代码折叠:在代码中创建层次结构

转载 作者:行者123 更新时间:2023-12-03 14:22:54 28 4
gpt4 key购买 nike

我在RStudio中编写R脚本,并且经常使用code folding。我发现您可以通过按cmd + shift + O来查看折叠的层次结构。这非常有用。

# to my dear love ---------------------------------------------------------
2+2
# yo man ====
x.2 = function (x) {x+2}

### I do love potatoes ####

通过按cmd + shift + O查看结果。

enter image description here

我不明白这是如何工作的,因为当我编写下面的代码时,我可以创建一个没有文本的子节,但是当其中有文本时就不能创建子节(使用 # ====而不是 # yo man ====)。
# to my dear love ---------------------------------------------------------
2+2
# ====

# yo man ====

### I do love potatoes ####
x.2 = function (x) {x+2}
data = "here is some data"

通过按cmd + shift + O查看结果。
enter image description here

您会看到 # to my dear love ---------------------------------------------------------下的所有内容都向右移动!这很酷!
  • 因此,问题是,如何创建一个包含文本的节的层次结构?
  • 是执行此操作的特殊软件包还是Emac?如何创建带有文本的小节,并在cmd + shift + O框中查看层次结构?
  • 如何通过减小右侧框中的可视层次结构,将某个部分(转到较高的部分(例如第2部分))下移到较低的部分(例如第1部分)?

  • 最佳答案

    根据克里斯的回答subheaders within functions

    RStudio代码折叠层次结构仅在函数定义和if-else结构内起作用。例如:

    # Section 1 ----
    a <- 1

    testfunct1 <- function () {
    # sect in function=====
    b <- 2
    c <- 3
    }

    # Section 2 #####
    d <- 4

    # Section 3 =======
    e <- 5

    testfunct2 <- function () {
    # sect in function 2 =====
    f <- 6
    testsubfunct2_1 <- function () {
    # sect in subfunction 2_1 -----
    if (a == 1) {
    # section in if ----
    g < 7
    } else {
    # section in else ----
    h = 8
    }
    }
    }

    # Section 4 ####
    j <- 9

    产生此轮廓:

    Code Outline screenshot

    我不知道为什么if-else部分标签不对齐。

    关于r - RStudio中的代码折叠:在代码中创建层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37837095/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com