功能未返回结果

时间:2017-09-13 08:48:18

标签: r

我的代码存在问题,其中函数不会返回任何内容。以下是有问题的代码:

#Required libraries: rJava, rChoiceDialogs, tcltk


#Set the working directory
set_library = function(){
  library(rJava)
  library(rChoiceDialogs)
  wd = jchoose.dir()
  setwd(wd)
  return(wd)
}

#Load the csv files
load_files = function(){
  library(tcltk)
  stocks = tk_choose.files()
  print(length(stocks))
  return(stocks)
}

set_library()
load_files()
print(length(stocks))

该函数将在load_files函数中打印长度,但不会在最后打印。

1 个答案:

答案 0 :(得分:0)

您需要在父作用域中保存stocks

# Required libraries: rJava, rChoiceDialogs, tcltk

# Set the working directory
set_library = function() {
  library(rJava)
  library(rChoiceDialogs)
  wd = jchoose.dir()
  setwd(wd)
  return(wd)
}

# Load the csv files
load_files = function() {
  library(tcltk)
  stocks = tk_choose.files()
  # print(length(stocks))
  return(stocks)
}

set_library()
stocks <- load_files()
print(length(stocks))