Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Next" button in a R Shiny app

Tags:

r

shiny

I'm trying to build an step by step app using Shiny. My aim is creating an examen consisting in a bunch of questions written in a database. What I need is a "next" button which when you click another question shows up.

I've been triying with an "action button" but it just works the first time, that is, the first time that it is clicked a question shows up, but it becomes unclickable once clicked for first time (it doesn't work as a "next button" as I wish).

Here is the code:

Server.R:

library(xlsx)
data<-read.xlsx("data/base.xlsx",sheetName="Full1")

shinyServer(function(input, output) {

  data[,2]<-as.character(data[,2])

  question<-data[2,2]

  ntext <- eventReactive(input$goButton, {
    question
  })

  output$nText <- renderText({
     ntext()
  })  
})

ui.R:

shinyUI(pageWithSidebar(
  headerPanel("Exam"),
  sidebarPanel(
    actionButton("goButton", "Next"),
    p("Next Question")
  ),
  mainPanel(
    verbatimTextOutput("nText")
  )
))

Thank you so much.

like image 234
Leibnitz Crew Avatar asked Jul 11 '16 08:07

Leibnitz Crew


People also ask

How do you use Shiny apps in R?

Open the app. R script in your RStudio editor. RStudio will recognize the Shiny script and provide a Run App button (at the top of the editor). Either click this button to launch your app or use the keyboard shortcut: Command+Shift+Enter (Control+Shift+Enter on Windows).

How do you use the Shiny action button?

Create an action button with actionButton() and an action link with actionLink() . Each of these functions takes two arguments: inputId - the ID of the button or link. label - the label to display in the button or link.

Why is R Shiny good?

On the other hand, R Shiny is an open-source package for building web applications with R. It provides a robust web framework for developing any sort of app, not only dashboards. Shiny is easy and intuitive to use, as you'll see in the examples below.

What is R shiny?

R shiny allows you to present your data interactively – that means your app users can: App users can do all of this without any R knowledge. You do the coding, your users get the info they are looking for! In this course I will show you step by step how to master R Shiny.

What are the R buttons for?

Some buttons are specialized for particular tasks, such as navigation, repeated actions, or presenting menus. For more details and examples visit the official docs . The R package can not handle each and every case, so for advanced use cases you need to work using the original docs to achieve the desired result.

Is shiny free to use?

All the software downloads, add on packages as well as entry level hosting for shiny are totally free. I will show you what you need and where to get it. That includes hosting as well. Take a look at shiny – your boss, colleagues, students, customers will be astonished what modern day data visualization can do.

How much does shiny cost to install?

All the software downloads, add on packages as well as entry level hosting for shiny are totally free. I will show you what you need and where to get it. That includes hosting as well.


1 Answers

You can do something like this. Please note the comments in the code

rm(list = ls())
library(shiny)
questions <- c("What is your name?","Can you code in R?","Do you find coding fun?","Last Question:How old are you?")

ui <- pageWithSidebar(
  headerPanel("Exam"),
  sidebarPanel(actionButton("goButton", "Next"),p("Next Question")),
  mainPanel(verbatimTextOutput("nText")))

server <- function(input, output,session) {

  # Inititating reactive values, these will `reset` for each session
  # These are just for counting purposes so we can step through the questions
  values <- reactiveValues()
  values$count <- 1

  # Reactive expression will only be executed when the button is clicked
  ntext <- eventReactive(input$goButton,{

    # Check if the counter `values$count` are not equal to the length of your questions
    # if not then increment quesions by 1 and return that question
    # Note that initially the button hasn't been pressed yet so the `ntext()` will not be executed
    if(values$count != length(questions)){
      values$count <- values$count + 1
      return(questions[values$count])
    }
    else{
      # otherwise just return the last quesion
      return(questions[length(questions)])
    }
  })

  output$nText <- renderText({
    # The `if` statement below is to test if the botton has been clicked or not for the first time,
    # recall that the button works as a counter, everytime it is clicked it gets incremented by 1
    # The initial value is set to 0 so we just going to return the first question if it hasnt been clicked
    if(input$goButton == 0){
      return(questions[1])
    }
    ntext()
  })  
}
shinyApp(ui = ui, server = server)
like image 65
Pork Chop Avatar answered Sep 24 '22 09:09

Pork Chop