【问题标题】:file.show and user input in a loop循环中的 file.show 和用户输入
【发布时间】:2014-08-04 15:23:42
【问题描述】:

我有一个数据框 data,其中包含有关 tiff 的信息,包括一列 txt 描述 tiff 的内容。不幸的是,txt 并不总是正确的,我们需要手动更正它们。因此,我想遍历数据中的每一行,显示 tiff 并寻求反馈,然后将其放入 data$txt.cor

setwd(file.choose())

一些测试tiffs(里面有废话,但为了展示这个想法......):

txt <- sample(100:199, 5)

for (i in 1:length(txt)){
tiff(paste0(i, ".tif"))
plot(txt[i], ylim = c(100, 200))
dev.off()
}

和数据框:

pix.files <- list.files(getwd(), pattern = "*.tif", full.names = TRUE)
pix.file.info <- file.info(pix.files)
data <- cbind(txt, pix.file.info)
data$file <- row.names(pix.file.info)
data$txt.cor <- ""
data$txt[5] <- 200 # wrong one

我的反馈功能(错误处理被剥离):

read.number <- function(){
 n <- readline(prompt = "Enter the value: ")
 n <- as.character(n) #Yes, character. Sometimes we have alphanumerical data or leading zeros
 }

现在是循环,非常感谢您的帮助:

for (i in nrow(data)){
  file.show(data[i, "file"]) # show the image file
  data[i, "txt.cor"] <- read.number() # aks for the feedback and put it back into the dataframe
}

在我的第一次尝试中,我想到了 plot.lm 的想法,您可以在按回车后浏览诊断图。我怀疑情节和tiffs不是大朋友。 file.show 原来更容易。但是现在我很难处理那个循环......

【问题讨论】:

    标签: r loops plot tiff


    【解决方案1】:

    您的问题是您没有遍历数据,您只评估最后一行。只需写1:nrow(data)即可遍历所有行。

    要在 R 中显示您的 tiff 图像,您可以使用包 rtiff:

    library(rtiff)
    for (i in 1:nrow(data)){
      tif <- readTiff(data[i,"file"]) # read in the tiff data
      plot(tif) # plot the image
      data[i, "txt.cor"] <- read.number() # aks for the feedback and put it back into the dataframe
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多