【发布时间】:2015-09-21 17:48:57
【问题描述】:
在我下面的示例中,一旦在 RStudio 中运行,通过单击滑块上的“播放”按钮,移位的行数逐渐增加。但是通过暂停,然后将数据集名称更改为iris,然后单击“显示”按钮并重新单击“播放”,不会出现相同的动画行数增加...为什么?以及如何调整我的代码来做到这一点......即让动画出现在不同的数据集上?
下面的例子部分改编自eventReactive()函数
require(shiny)
if (interactive()) {
ui <- fluidPage(
column(4,
sliderInput('x',label='Num Rows',min=2,max=30,step=1,value=3,animate = TRUE),
textInput('tbl_nm',label='Data Set',value='cars'),
br(),
actionButton("button", "Show")
),
column(8, tableOutput("table"))
)
server <- function(input, output) {
# reactively adjust the number of rows
ll <- eventReactive(input$x,{
input$x
})
# change the data sets after clicking the button
dat <- eventReactive(input$button,{
if(input$tbl_nm=='cars'){
dat <- cars
} else {
dat <- get(input$tbl_nm)
}
return(dat)
})
# Take a reactive dependency on input$button, but
# not on any of the stuff inside the function
df <- eventReactive(input$button, {
yy <- ll()
# choose only the relevant data...
head(dat(),yy)
})
# show the final table
output$table <- renderTable({
if(input$button==0){
# show the first few lines of cars at the begining
head(cars, ll())
} else {
# show the selected data
df()
}
})
}
shinyApp(ui=ui, server=server)
}
【问题讨论】: