r Shiny action button and data table output last N rows

Viewed 179

I need help with action button working only once. If you click on button it will shows you graph,table and some text. But if you want to change something on sidebar like a colour it will change and you dont have to press the button... (I need to have: If you want to change something colour,values..you need to always push the action button to confirm it it cant change without button) Also, I need to show only the last N rows from table airquality which depends on numericID input. Can anyone help me?

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) { 

  observeEvent(input$gobutton, {
    output$textik <- renderText({
      vypis=c("Zobrazili ste tabuľku s", input$numericID, "riadkami a boxplot pre atribút Ozone ste nastavili na farbu ", input$radioID)
      print(vypis)
    })

    output$table <- renderTable(airquality)

    output$distPlot <- renderPlot({
      x    <- airquality[,input$selectID]
      boxplot(x~airquality$Month,  col = input$radioID, border = 'white', main=input$textID)
    })
  })

})

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Dáta Airquality"),

  # Sidebar with a slider input for number of bins
  sidebarPanel(
    numericInput("numericID","PoÄet riadkov tabuľky",value=6, min=1, max=100, step=5),
    selectInput("selectID","Vyberte atribút",choices=c(colnames(airquality))),
    radioButtons("radioID","Vyberte farbu grafu", choices=c("yellow","green")),
    textInput("textID","Zadajte nadpis grafu", value ="Nadpis"),
    actionButton("gobutton","Start")
  ),

  # Show a plot of the generated distribution
  mainPanel(           
    plotOutput("distPlot"),
    tableOutput("table"),
    textOutput("textik")
  )

))
1 Answers

@Tomáš - I would recommend avoiding putting your output statements inside of observeEvent. Instead, I would create an eventReactive that will be triggered by your action button. And when this happens, it will store all your needed info in a list, and all of your outputs will be dependent on this list. Here is one way to do this (below is the server function only).

server <- function(input, output) {

  aq_data <- eventReactive(input$gobutton, {
    list(data = airquality, nID = input$numericID, rID = input$radioID, sID = input$selectID, tID = input$textID)
  }) 

  output$textik <- renderText({
    vypis=c("Zobrazili ste tabuľku s", aq_data()[["nID"]], "riadkami a boxplot pre atribút Ozone ste nastavili na farbu ", aq_data()[["rID"]])
    print(vypis)
  })

  output$table <- renderTable(tail(aq_data()[["data"]], aq_data()[["nID"]]))

  output$distPlot <- renderPlot({
    dat <- aq_data()
    boxplot(reformulate("Month", dat[["sID"]]), col = dat[["rID"]], border = 'white', main = dat[["tID"]], data = dat[["data"]])
  })

}
Related