[英]Using rbind to append the results to a data frame in a for-loop
我是 Rshiny 的新手,在 for 循环中显示结果时遇到问题,所有迭代都附加到同一个数据框中。 下面的最小示例在 R 桌面上运行良好:
df_total = data.frame()
for (i in 1:7){
# vector output
model <- i + 10
# add vector to a dataframe
df <- data.frame(model)
df_total <- rbind(df_total,df)
}
但是当我尝试在 Rshiny 中复制它并在表格中显示结果时,我只会得到空白结果。 这是我在 Rshiny 中使用的代码的最小示例:
rm(list=ls())
library(tidyverse)
library(shiny)
ui <- fluidPage(
mainPanel(
#Add tabs to the Dashboard
tabsetPanel(type = "tab",
#for DT::dataTableOutput
tabPanel("table1", DT::dataTableOutput("TableSample")),
)
)
)
server <- function(input, output, session) {
FinalTable <- reactive({
df_total = data.frame()
for (i in 1:7){
# vector output
model <- i + 10
# add vector to a dataframe
df <- data.frame(model)
df_total <- rbind(df_total,df)
}
})
output$TableSample <- DT::renderDataTable(FinalTable(), extensions = 'Buttons',
options = list(scrollX=TRUE, lengthMenu = c(5,10,15),
paging = TRUE, rownames = FALSE, searching = TRUE,
fixedColumns = FALSE, autoWidth = FALSE,
ordering = TRUE, dom = 'tB',
buttons = c('copy', 'csv', 'excel','pdf')))
}
shinyApp(ui, server)
我尝试使用观察者在 for 循环中绑定反应式 output。 尝试了几种不同的方法,但似乎没有任何效果。
您需要将 df_total 添加为反应式 FinalTable 的返回值,如下所示:
FinalTable <- reactive({
df_total = data.frame()
for (i in 1:7){
# vector output
model <- i + 10
# add vector to a dataframe
df <- data.frame(model)
df_total <- rbind(df_total,df)
}
df_total
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.