簡體   English   中英

在R上制作玩具包裝,無法使用R文件中的其他功能

[英]Making a toy package on R, unable to use the other functions in the R file

我正在嘗試創建自己的R包作為練習 我關注了希拉里·帕克Hillaryary Parker)的在線教程,並設法有所發展。

我要制作的程序包使用一個csv文件,並打印數據集的head()和tail()。 然后,我編寫了另一個函數,該函數將在文本文件中打印head()和tail()值。

ExercisePkg <- function(.csv) {

  csv <- read.csv(.csv)

  headValue <- head(csv)
  print("The head of the dataset is:")
  print(headValue)

  tailValue <- tail(csv)
  print("The tail of the dataset is:")
  print(tailValue)

  return(list(headValue, tailValue))
}

我的下一個功能是將headValuetailValue打印到文本文件中。 為此,我使用sink()並按如下所示修改ExercisePkg

ExercisePkgTxt <- function(.csv) {

  sink('Report.txt')
  csv <- read.csv(.csv)

  headValue <- head(csv)
  print("The head of the dataset is:")
  print(headValue)

  tailValue <- tail(csv)
  print("The tail of the dataset is:")
  print(tailValue)

  return(list(headValue, tailValue))
  sink('Report.txt', append=TRUE)
}

我在code.R文件中具有以下兩個功能:

#' Function to see head and tail of a csv file
#'
#' Function is cool.
#' @param Do you love data? Defaults to TRUE
#' @keywords csv, data, head, tail,text.
#' @export ExercisePkg(),ExercisePkgTxt()
#' @examples no examples
#' ExercisePkg()
#' ExercisePKgTxt()

ExercisePkg <- function(.csv) {

      csv <- read.csv(.csv)

      headValue <- head(csv)
      print("The head of the dataset is:")
      print(headValue)

      tailValue <- tail(csv)
      print("The tail of the dataset is:")
      print(tailValue)

      return(list(headValue, tailValue))
    }

    ExercisePkgTxt <- function(.csv) {

      sink('Report.txt')
      csv <- read.csv(.csv)

      headValue <- head(csv)
      print("The head of the dataset is:")
      print(headValue)

      tailValue <- tail(csv)
      print("The tail of the dataset is:")
      print(tailValue)

      return(list(headValue, tailValue))
      sink('Report.txt', append=TRUE)
    }

它保存在/path/ToyPackage/R/code.R

安裝完軟件包后。 我嘗試測試。

ExercisePkg("/path/dataset.csv")就像一個魅力。

但是ExercisePkgTxt("/path/dataset.csv")給出了類似Error: could not find function "ExercisePkgTxt"

我嘗試將這兩個函數放在單獨的R文件中( ExercisePkg() code.R和ExercisePkgTxt() code1.R)並重建包。 但是問題並沒有消失。

當我嘗試運行document() ,得到以下信息:

>document()
Updating ToyPackage documentation
Loading ToyPackage
Writing NAMESPACE
Writing ExercisePkg.Rd
> 

NAMESPACE文件如下所示:

# Generated by roxygen2: do not edit by hand

export("ExercisePkg(),ExercisePkgTxt()")

當我嘗試通過install(“ ToyPackage”)安裝軟件包時。 我收到以下錯誤:

* installing *source* package 'ToyPackage' ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
Error in namespaceExport(ns, exports) : 
  undefined exports: ExercisePkg(),ExercisePkgTxt()
Error: loading failed
Execution halted
*** arch - x64
Error in namespaceExport(ns, exports) : 
  undefined exports:  ExercisePkg(),ExercisePkgTxt()
Error: loading failed
Execution halted
ERROR: loading failed for 'i386', 'x64'
* removing 'C:/Users/user/Documents/R/win-library/3.3/ToyPackage'
Error: Command failed (1)

我究竟做錯了什么?

請不要完全給我一個全新的代碼,只是提出一些更改(如有)。

謝謝。

由於您正在使用Roxygen和devtools,因此需要執行以下操作:

  • 對於要從​​包中導出的每個函數,請包含#' @export指令
  • 在構建/安裝軟件包之前,請運行document() ,以確保NAMESPACE文件已更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM