Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

r - Deactivate actionbuttons based on row length in a shiny app

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])
    })
  }
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this

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)
    rnumm <- 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)
        rnumm(nrow(dat))
        output$celltext <- renderText({
          if(length(input$tableId_rows_selected))  rv$text
          else ''
        })
      }
    })
    
    observeEvent(input[['prv']], {
      if (rnum()>1) rnum(rnum()-1)
      rv$text <- paste("flower",dt$data[rnum(),2],"has score",dt$data[rnum(),3])
    })
    observeEvent(input[['next']], {
      if (rnum()<rnumm()) rnum(rnum()+1)
      rv$text <- paste("flower",dt$data[rnum(),2],"has score",dt$data[rnum(),3])
    })
  }
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...