在循环中渲染数据表
它们都应该表现一样
library(shiny)
getData = function(name){
if(name=='foo'){
return(data.frame(foo=c('foo')))
}else{
return(data.frame(bar=c('bar')))
}
}
ui <- fluidPage(
DT::dataTableOutput(outputId = "foo"),
DT::dataTableOutput(outputId = "bar")
)
server <- function(input, output) {
for(item in c('foo','bar')){
output[[item]] = DT::renderDT(getData(item))
}
}
shinyApp(ui = ui, server = server)
我想在闪亮的应用中展示多个表,并且由于 相同的数据集(上面的示例中的“ bar”),特别是循环中的最后一个数据集。 似乎结构输出[[item]] = ...
的行为不像我认为应该这样做,但我无法弄清楚发生了什么。
多谢!
I want to display multiple tables in a shiny-app, and as they should all behave the same, I thought I could render them in a for-loop:
library(shiny)
getData = function(name){
if(name=='foo'){
return(data.frame(foo=c('foo')))
}else{
return(data.frame(bar=c('bar')))
}
}
ui <- fluidPage(
DT::dataTableOutput(outputId = "foo"),
DT::dataTableOutput(outputId = "bar")
)
server <- function(input, output) {
for(item in c('foo','bar')){
output[[item]] = DT::renderDT(getData(item))
}
}
shinyApp(ui = ui, server = server)
Although the loop iterates correctly through the different 'datasets', all the tables finally show the same dataset ('bar' in the example above), notably the last one called in the loop.
It seems that the structure output[[item]] = ...
doens't behave the way I think it should, but I can not figure out what is going on.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
local
如下所示You can use
local
as shown below