将图保存到R中for循环中的不同jpeg文件中

时间:2017-01-19 17:49:29

标签: r

我想在R中完成以下操作:我同样用于循环,我想将不同的图保存到不同的jpeg文件中。我的问题是我不确切地知道如何告诉R"将此情节保存在这个开放的jpeg文件中以及此其他 jpeg中的其他情节文件"

我目前可以使用两个for循环来执行此操作:

   const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})

constructor(props) {
    super(props)
    this.state = {
      favoriteDB: props.favorites,
      renderPlaceholder: true,
      dataSource: ds.cloneWithRows([])
    }
  }

 componentDidMount() {
    InteractionManager.runAfterInteractions(() => {
      this.setState({
        dataSource: ds.cloneWithRows(this.state.favoriteDB),
        renderPlaceholder: false
      })
    })
  }

  componentWillReceiveProps(prevProps) {
    const {favorites} = this.props

    if (JSON.stringify(favorites) != JSON.stringify(prevProps.favorites)) {
      this._updateFavorites()
    }
  }

  _updateFavorites() {
    const {favorites} = this.props

    this.setState({
      favoriteDB: favorites,
      dataSource: ds.cloneWithRows(favorites)
    })
  }

 render() {
    const {dataSource} = this.state
    const {visibleHeight, visibleWidth} = this.props

    if (this.state.renderPlaceholder)
      return <Placeholder/>

    return (
      <Container theme={theme}>
        <View style={{ flex:1, height: visibleHeight - 100, width: visibleWidth }}>
          <Row>
            <Col>
              <List>
                <ListView
                  style={{ height: visibleHeight - 100, width: visibleWidth }}
                  enableEmptySections={TRUE}
                  automaticallyAdjustContentInsets={FALSE}
                  initialListSize={100}
                  pageSize={100}
                  dataSource={dataSource}
                  renderRow={rowData => this._renderRow(rowData)}/>
              </List>
            </Col>
          </Row>
        </View>
      </Container>
    )
  }

但是,我想将这两个循环组合在一起(后面的内容不是实际的代码,而是我想要完成的想法):

library(data.table)

set.seed(10)

data1 <- data.table(A = letters[3:5], B = letters[6:8], 
                    C = rnorm(20), D = rnorm (20) )

for( i in unique(data1$A )){

  data2 <- data1[A == i]

  jpeg(paste(i,'plot1.jpg',sep = ''))

  hist(data2$C)

  dev.off()

}

for( i in unique(data1$A )){

  data2 <- data1[A == i]

  jpeg(paste(i,'plot2.jpg',sep = ''))

  hist(data2$D)

  dev.off()

}

1 个答案:

答案 0 :(得分:2)

library(data.table)

set.seed(10)

data1 <- data.table(A = letters[3:5], B = letters[6:8], 
                    C = rnorm(20), D = rnorm (20) )

for( i in unique(data1$A )){

  data2 <- data1[A == i]

  jpeg(paste(i,'plot1.jpg',sep = ''))

  hist(data2$C)

  dev.off()

  jpeg(paste(i,'plot2.jpg',sep = ''))

  hist(data2$D)

  dev.off()

}