I found one issue with rtracklayer's export.bed function. It seems that it cannot export an empty GRanges object. For example,
> export(GRanges(), con = "test.bed") Error in `$<-.data.frame`(`*tmp*`, "score", value = 0) : replacement has 1 row, data has 0 > sessionInfo() R version 3.4.1 (2017-06-30) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Sierra 10.12.6 Matrix products: default BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib locale:  en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages:  parallel stats4 stats graphics grDevices utils datasets  methods base other attached packages:  rtracklayer_1.38.3 GenomicRanges_1.30.3 GenomeInfoDb_1.14.0  IRanges_2.12.0 S4Vectors_0.16.0 BiocGenerics_0.24.0 loaded via a namespace (and not attached):  lattice_0.20-35 matrixStats_0.54.0  XML_3.99-0 Rsamtools_1.30.0  Biostrings_2.46.0 GenomicAlignments_1.14.1  bitops_1.0-6 grid_3.4.1  zlibbioc_1.24.0 XVector_0.18.0  Matrix_1.2-14 BiocParallel_1.12.0  tools_3.4.1 Biobase_2.38.0  RCurl_1.96-0 DelayedArray_0.4.1  compiler_3.4.1 SummarizedExperiment_1.8.1  GenomeInfoDbData_0.99.1
Actually, this problem seems to be related to Why export of empty GRanges to GFF fails?, where it was reported that export.gff raised an error upon an empty object.
Can anybody give a look?