讀寫Texual format

  • dput()dget()

    直接演示:

    > x <- data.frame(foo = 1:4, bar = c("a", "b", "c", "d"))
    > dput(x, file = "D:\\test")
    > dput(x)# 如果file = "",則輸出到console
    structure(list(foo = 1:4, bar = structure(1:4, .Label = c("a", "b", "c", "d"), class = "factor")), .Names = c("foo", "bar"), row.names = c(NA, -4L), class = "data.frame")
    > dget("D:\\test")
      foo bar
    1   1   a
    2   2   b
    3   3   c
    4   4   d
    
  • dump()source()

    有什麼比直接演示還明白的呢?

    > x <- data.frame(foo = 1:4, bar = c("a", "b", "c", "d"))
    > y <- "abc"
    #把x、y存到test裡面,dump第一個參數是vector,元素是由Objest名稱(string),第二個參數是存檔path。
    > dump(c("x", "y"), file = "D:\\test")
    #把x、y物件從執行環境中刪除
    > rm(x, y)
    > x
    Error: object 'x' not found
    > y
    Error: object 'y' not found
    #再載入存有x、y物件的檔案到目前執行環境
    > source("D:\\test")
    #解構x看看,這時候x就存在了
    > unclass(x)
    $foo
    [1] 1 2 3 4
    
    $bar
    [1] a b c d
    Levels: a b c d
    
    attr(,"row.names")
    [1] 1 2 3 4
    

results matching ""

    No results matching ""