a next button in shiny app

Hi,

I am now a PhD student and i want to read abstract using R shiny. I extract a bunch of abstract from the site web of science in an excel file and i just want to create a button to go to the next abstract. Here the code I use


# Importer les données

test <- c("abc","cde","efg")
i=1
# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Lire avec R shiny"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            actionButton("suivant",
                        "Article suivant")
        ),

        # Show a plot of the generated distribution
        mainPanel(
          textOutput("Abstract")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  observeEvent(input$suivant,{i<-i+1})
    output$Abstract <- renderText({test[i]})
  
}

# Run the application 
shinyApp(ui = ui, server = server)

I don't understand how reactivity quite work with this one and i can't get to change the value of i to parse the next abstract. I did understand that these button needs to be used with observevent or eventreactive but i can figure out how to change the value of i after i notice the change in input$suivant.

Regards

I don't thing an Action Button is the right tool. As you have seen, you have to store the value of the vector index i somewhere. I would use a numeric input.

library(shiny)
test <- c("abc","cde","efg")
i=0
# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Lire avec R shiny"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(

      numericInput("suivant", "Abstract Index", value = 1, 
                   min = 1, max = length(test))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      textOutput("Abstract")
    )
  )
)

server <- shinyServer(function(input,output){
  output$Abstract <- renderText(test[input$suivant])
}
)
shinyApp(ui,server)

thank you, it's right it is a little easier. Need to read now...

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.