I have the shiny
app below in which when the user clicks on a row of the datatable a subset happens in another dataframe df
and a text is displayed.
When I press the Next
actionbutton()
the text displayes the data of the next row of the subseted dataframe.
When I press the Previous
actionbutton()
the text displayes the data of the previous row of the subseted dataframe.
But when the row is the first the actionbutton() 'Previous'
should be deactivated because there is no previous row and when the row is the last the actionbutton() 'Next'
should be deactivated because there is no next row. So when they are clicked under these condition the message should remain the same.
library(shiny)
shinyApp(
ui <- fluidPage(DT::dataTableOutput('tableId'),
textOutput("celltext"),
actionButton("next","Next"),
actionButton("prv","Previous")),
server <- function(input, output) {
rv <- reactiveValues(text=NULL)
dt <- reactiveValues(data=NULL)
rnum <- reactiveVal(0)
output$tableId = DT::renderDataTable(
iris[,c(1,5)], selection = list(target = 'row',mode="single")
)
species<-c("setosa","setosa","virginica","virginica","setosa","setosa","virginica","virginica")
flower<-c("a","b","c","d","e","f","g","h")
score<-c(7,5,6,9,1,2,3,4)
df<-data.frame(species,flower,score)
observeEvent(input$tableId_rows_selected, {
if(is.null(input$tableId_rows_selected)){
return(NULL)
}
else{
row <- input$tableId_rows_selected
dat<-df[df$species %in% iris[row,5],]
dt$data <-dat[order(dat$score,decreasing = T),]
rv$text <- paste("flower",dt$data[1,2],"has score",dt$data[1,3])
rnum(1)
output$celltext <- renderText({
if(length(input$tableId_rows_selected)) rv$text
else ''
})
}
})
observeEvent(input[['prv']], {
rnum(rnum()-1)
rv$text <- paste("flower",dt$data[rnum(),2],"has score",dt$data[rnum(),3])
})
observeEvent(input[['next']], {
rnum(rnum()+1)
rv$text <- paste("flower",dt$data[rnum(),2],"has score",dt$data[rnum(),3])
})
}
)