Hello
I have a list of data tables which all have the same structure and from which I need to dynamically choose a subset and aggregate them to a single data.table by using e.g. rbind.
In a non reactive environment this is no problem, but when I use dynamic input it does not seem to create the data table.
I managed to rbind two data tables, but with more the do.call(rbind,.. ) does not seem to work.
I tried to create a reproducible example:
### Create an exemplary list of data tables as original data set
somelist<-list()
somelist[[1]] <- as.data.table(list("A"=c(1,1),"B"=c(2,2),"C"=c(3,3)))
somelist[[2]] <- as.data.table(list("A"=c(4,4),"B"=c(5,5),"C"=c(6,6)))
somelist[[3]] <- as.data.table(list("A"=c(7,7),"B"=c(8,8),"C"=c(9,9)))
somelist[[4]] <- as.data.table(list("A"=c(10,10),"B"=c(11,11),"C"=c(12,12)))
somelist[[5]] <- as.data.table(list("A"=c(13,13),"B"=c(14,14),"C"=c(15,15)))
biglist <- do.call("rbind",somelist) #rbinding in this way works in a non reactive environment
server <- function(input,output,session) {
output$somelist<- renderTable({biglist})
output$testinput1<-renderText({input$testinput1})
y<-reactive({input$testinput1}) #Input the amount of data tables that should be appended
testlist<-reactive({list(y())}) #create empty list
# testlist<-list() #create empty list
# Create a dynamic subset(testlist) of data.tables from the original data
reactive({
for(i in 1:y())
{
testlist()[[i]]<-reactive({somelist[[i]]})
}
})
# Rbind the chosen data tables into one
reactivelist<-reactive({do.call("rbind",testlist())}) #create appended data.table from list of data.tables
output$testtable<-renderTable({
reactivelist()
})
Sum_A <- reactive({reactivelist()[,sum(A)]})
output$sum_a<-renderText({
a<-paste0(Sum_A())
a})
}
header<-dashboardHeader(title = "Rbind_test",titleWidth = 280)
sidebar<-dashboardSidebar(width = 280,sidebarMenu(id="sidebar_tabs",
menuItem("AAA", tabName = "AAA")
))
body<-dashboardBody(title="Main",
inlineCSS(list(.blue = "color: blue",
.red = "color: red")),
tabItem(tabName = "Overview",h1("Overview"),
fluidPage(
box(sliderInput(inputId = "testinput1",label="testinput1",min=1,max=5,value=5)),
box(title="Output1",textOutput(outputId="testinput1")),
box(title="Output2: rbind of first x lists",tableOutput(outputId="testtable")),
box(title="rbinded_table",tableOutput(outputId="somelist")),
box(title="Value from table (sum A)",textOutput(outputId="sum_a"))
)
)
)
ui <- dashboardPage(skin = "black",
header,
sidebar,
body
)
shinyApp(ui = ui, server = server)
Where am I going wrong?