for 循环中 S4 对象的错误输出

Wrong output from S4 object in for loop

使用 for 循环从 S4 对象获取 class 特定输出。 S4对象称为数据,是一个大对象。

for循环由5个class组成,即:'fat' 'inflammation' 'muscle' 'glands' 'lumen'

tissues = c("fat","inflammation","muscle","glands","lumen")
tissues
for (i in tissues) {
    print(i)
    print(data$i)
}

输出:

'fat' 'inflammation' 'muscle' 'glands' 'lumen' 

[1] "fat"
$`m424_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 169 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m424_inflammation_tic-normalization
    raster dimensions: 507 x 164 x 1
    coord(3): x = 1..507, y = 1..164, z = 1..1
    mass range:  599.9313 to 3200.0906 
    centroided: FALSE 

$`m80_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 149 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m80_inflammation_tic-normalization
    raster dimensions: 400 x 212 x 1
    coord(3): x = 1..400, y = 1..212, z = 1..1
    mass range:  599.6025 to 3200.7207 
    centroided: FALSE 

[1] "inflammation"
$`m424_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 169 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m424_inflammation_tic-normalization
    raster dimensions: 507 x 164 x 1
    coord(3): x = 1..507, y = 1..164, z = 1..1
    mass range:  599.9313 to 3200.0906 
    centroided: FALSE 

$`m80_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 149 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m80_inflammation_tic-normalization
    raster dimensions: 400 x 212 x 1
    coord(3): x = 1..400, y = 1..212, z = 1..1
    mass range:  599.6025 to 3200.7207 
    centroided: FALSE 

[1] "muscle"
$`m424_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 169 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m424_inflammation_tic-normalization
    raster dimensions: 507 x 164 x 1
    coord(3): x = 1..507, y = 1..164, z = 1..1
    mass range:  599.9313 to 3200.0906 
    centroided: FALSE 

$`m80_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 149 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m80_inflammation_tic-normalization
    raster dimensions: 400 x 212 x 1
    coord(3): x = 1..400, y = 1..212, z = 1..1
    mass range:  599.6025 to 3200.7207 
    centroided: FALSE 

[1] "glands"
$`m424_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 169 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m424_inflammation_tic-normalization
    raster dimensions: 507 x 164 x 1
    coord(3): x = 1..507, y = 1..164, z = 1..1
    mass range:  599.9313 to 3200.0906 
    centroided: FALSE 

$`m80_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 149 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m80_inflammation_tic-normalization
    raster dimensions: 400 x 212 x 1
    coord(3): x = 1..400, y = 1..212, z = 1..1
    mass range:  599.6025 to 3200.7207 
    centroided: FALSE 

[1] "lumen"
$`m424_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 169 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m424_inflammation_tic-normalization
    raster dimensions: 507 x 164 x 1
    coord(3): x = 1..507, y = 1..164, z = 1..1
    mass range:  599.9313 to 3200.0906 
    centroided: FALSE 

$`m80_inflammation_tic-normalization`
An object of class 'MSContinuousImagingExperiment'
  <53400 feature, 149 pixel> imaging dataset
    imageData(1): intensity
    featureData(0):
    pixelData(0):
    metadata(11): ibd binary type universally unique identifier ...
        files name
    run(1): m80_inflammation_tic-normalization
    raster dimensions: 400 x 212 x 1
    coord(3): x = 1..400, y = 1..212, z = 1..1
    mass range:  599.6025 to 3200.7207 
    centroided: FALSE 


代码应输出 data$fat,然后是 data$inflammation、data$muscle、data$glands、data$lumen。

然而它只输出数据$inflammation

data$i 是问题所在。使用 data[[i]] 代替。 data$i 搜索名为 "i" 的列表元素,因为没有这样的元素,它会自动完成 data$i 到 data$inflammation.