使用 gWidgets 中的 gfilebrowse 读取 csv

Read csv using gfilebrowse from gWidgets

我正在尝试使用 gWidgets R 库构建一个 GUI 来下载卫星图像。目的是从逗号分隔值文件中读取 url。 GUI 看起来不错,但它没有按照我的预期进行。我做错了什么,非常感谢任何帮助。

示例数据如下:

Online.Access.URLs <- c("http://e4ftl01.cr.usgs.gov//MODIS_Composites/MOLT/MOD09A1.005/2000.02.18/MOD09A1.A2000049.h09v06.005.2006268183648.hdf",
                             "http://e4ftl01.cr.usgs.gov//MODIS_Composites/MOLT/MOD09A1.005/2000.02.26/MOD09A1.A2000057.h09v06.005.2006270065224.hdf",
                             "http://e4ftl01.cr.usgs.gov//MODIS_Composites/MOLT/MOD09A1.005/2000.03.05/MOD09A1.A2000065.h09v06.005.2006269234536.hdf")



Producer.Granule.ID  <- c("MOD09A1.A2000049.h09v06.005.2006268183648.hdf",
                              "MOD09A1.A2000057.h09v06.005.2006270065224.hdf",
                              "MOD09A1.A2000065.h09v06.005.2006269234536.hdf")

df <- data.frame(Producer.Granule.ID,Online.Access.URLs)

write.csv(df,"C:\GUI_test\h09v06v3.csv",row.names=FALSE)

这是我的尝试:

my.DownloadHDF <- function(){

  library(gWidgets)
  library(gWidgetstcltk)
  library(RCurl)

  options(guiToolkit = "tcltk")
  win <- gwindow("Download HDF with R!", visible = FALSE)

  csv.frame     <- gframe("csv file ", container = win)
  csv.label     <- glabel("csv with HDF's names ", container = csv.frame)  
  csv.file.name <- gfilebrowse("Select csv file", type="open",cont=csv.frame,action="read.csv")

  dir.frame <- gframe("Output Directory ", container = win)
  dir.label <- glabel("Where to save HDF's? ", container = dir.frame)
  dir.out   <- gfilebrowse("Select folder ",type = "selectdir", cont=dir.frame)

  dlw.frame <- gframe("Download ", container = win)
  dlw.label <- glabel(" ", container = dlw.frame) 

  btnDwn    <- gbutton("Start Download", container = dlw.frame,
                     handler = function(csv.file.name,dir.out){

                       df        <- read.csv(csv.file.name, header=TRUE,sep=",")
                       hdf.urls  <- df$Online.Access.URLs                    
                       hdf.urls  <- as.character(hdf.urls)
                       hdf.names <- df$Producer.Granule.ID                   
                       hdf.names <- as.character(hdf.names) 

                       for (i in 1:length(hdf.names)){ 
                         URL      <- hdf.urls [i]      
                         file     <- hdf.names[i]      
                         download.file(URL,paste(dir.out,file,sep=""),mode="wb") 
                         cat(paste("Composite number ",i,"successfully downloaded!"),sep="\n")
                         cat("\n\n\n\n\n\n\n\n") 
                       }})
  visible(win) <- TRUE
}
my.DownloadHDF()

我正在使用 R-3.2.2 和 RStudio 0.98.1103。

这是改进后的脚本。现在它完全符合我的预期。我希望有人觉得它有用:

# load functions ####
# download function
f.d <- function(hdf.urls,hdf.names,out.dir){
  for(i in 1:length(hdf.urls)){
    URL      <- hdf.urls  [i]     
    file     <- hdf.names [i]   
    download.file(URL,paste(out.dir,"/",file,sep=""),mode="wb")
  }}

# read csv function
f.csv <- function(x){
  df1 <<- read.csv(x,header=TRUE,sep=",")
  hdf.urls  <<- df1$Online.Access.URLs
  hdf.urls  <<- as.character(hdf.urls)
  hdf.names <<- df1$Producer.Granule.ID
  hdf.names <<- as.character(hdf.names)   
}
# load functions ####

# my.DownloadHDFv2 this one works fine ####
my.DownloadHDF <- function(){
  options(guiToolkit = "tcltk")

  win         <- gwindow("Download HDF with R!", visible = FALSE)
  csv.frame   <- gframe("csv with HDFs names ", container = win)

  a <- gfilebrowse("Upload csv file",cont=csv.frame, 
                   handler=function(h,...){
                     f.csv(svalue(a))                   
  })

  path.frame   <- gframe("Output Directory ", container = win)
  brow         <- gfilebrowse(text = "Select folder...", type = "selectdir",container=path.frame,
                              handler=function(h,...){
                                out.dir      <<- svalue(brow)
                              })

  b <- gbutton(text="Start Download",container = win,
                        handler   = function(h,...){
                          f.d(hdf.urls,hdf.names,out.dir=out.dir)                   
                        })
  visible(win)<-TRUE
}
my.DownloadHDF()
# my.DownloadHDFv2 this one works fine ####