我将森林库存数据存储在多个关系表中。我想在markdown中创建一个报告,该报告显示符合数据关系层次结构的信息。即:
Plot 1
Subplot 1
Tree 1
Tree 2
Tree 3
Plot 1
Subplot 2
Tree 1
Tree 2
Tree 3 (and so on…)
下面显示的代码将所有子图数据重新打印在一起。但是,我想在各个表中使用每个子图(我使用knitr),然后我可以在各自的子图下添加加法树属性。我的问题可以通过以下代码重现:
# Define Plot Number
```{r}
call_plot <- 1
```
```{r echo=FALSE, include=FALSE}
# Make the df plot_info
plot_id <- c(1:3)
x <- c(89.74, 89.67, 89.62)
y <- c(22.71, 22.66, 22.71)
plot_info <- as.data.frame(cbind(plot_id, x, y))
# Make the df subplot_info
plot_id <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3)
subplot_id <- c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
subplot_info <- as.data.frame(cbind(plot_id, subplot_id))
# Make the df for tree
plot_id <- c(1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)
subplot_id <- c(4, 4, 4, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
5, 5, 1, 1, 2, 2, 2, 2, 2, 4, 4, 5, 5, 5, 5)
species <- c("Phoenix sylvestris", "Albizia procera", "Tamarindus indica",
"Lannea coromandelica", "Lannea coromandelica", "Cocos nucifera",
"Cocos nucifera", "Samanea saman", "Samanea saman", "Cocos nucifera",
"Swietenia mahagoni", "Gmelina arborea", "Mangifera indica", "Mangifera
indica", "Cocos nucifera", "Cocos nucifera", "Samanea saman", "Mangifera
indica", "Artocarpus heterophyllus", "Artocarpus heterophyllus", "Artocarpus
heterophyllus", "Cocos nucifera", "Cocos nucifera", "Swietenia mahagoni",
"Cocos nucifera", "Samanea saman", "Polyalthia longifolia", "Samanea saman",
"Dillenia indica")
tree <- as.data.frame(cbind(plot_id, subplot_id, species))
```
```{r latex_table, results='markup', echo=FALSE, results='asis'}
# Asign call_plot
plot_info <- plot_info[which(plot_info$plot_id == call_plot),]
# Print Plot table
knitr::kable(plot_info, table.attr = "id=\"mytable\"", caption = "Plot Location")
#Asign subplot data to plot table
subplot_info <- subplot_info[which(subplot_info$plot_id == plot_info$plot_id),]
# Loop through subplots
for (i in 1:length(unique(subplot_info$subplot_id))){
p = (knitr::kable(subplot_info, table.attr = "id=\"mytable\"", caption = "Subplot"));
print(p)
}
```
答案 0 :(得分:0)
问题出现在你的循环中&#39; for&#39;。如果我理解正确,您想显示树信息,但调用表subplot_info。检查是否有效:
在树数据框中添加tree_id列:
# loop through subplot
vec_plot_id <- unique(tree$plot_id)
vec_subplot_id <- unique(tree$subplot_id)
for (i in 1:length(vec_plot_id)){
for (j in 1:length(vec_subplot_id)){
table_tree <- tree[which(tree$plot_id == vec_plot_id[i] & tree$subplot_id == vec_subplot_id[j]),]
table_tree[,c("plot_id", "subplot_id")] <- NULL
caption = paste("plot id: ", vec_plot_id[i], ", subplot id: ", vec_subplot_id[j])
p = (knitr::kable(table_tree, table.attr = "id=\"mytable\"", caption = caption))
print(p)
}
}
然后用:
改变你的循环(#cop through subplot)Path
希望这个帮助!