Reputation: 1265
I have created the following App in R shiny
library(shiny)
library(data.table)
After we import the libraries, we now create the UI as follows
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
This will create a ui with a numericinput and two buttons, one a go button and another a reset button
Next we create the server and run the App
server <- function(input, output, session) {
table_output<-eventReactive(input$goButton,{ ##### WE CREATE THE TABLE OBJECT HERE
table_output<-data.frame("SLNO"= seq(1:input$n))
table_output$Col2<-table_output$SLNO^2
return(table_output)})
output$all<-renderDataTable({
table_output()})
observeEvent(input$Reset, { #### WE EMPTY THE PLOT AREA HERE
output$all <- renderDataTable({ })})}
shinyApp(ui = ui, server = server)
The table is generated when I press the Go button. However after pressing the rest button, the table refuses to render despite changing the inputs. I request some help here.
Upvotes: 2
Views: 706
Reputation: 2613
I prefer to use observeEvent
and reactiveValues
in situations like this.
library(shiny)
library(data.table)
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
server <- function(input, output, session) {
table_output <- reactiveValues(df = data.frame(SLNO = as.numeric(), Col2 = as.numeric()))
observeEvent(input$goButton, {
temp <- table_output$df
temp <- data.frame("SLNO"= seq(1:input$n))
temp$Col2 <- temp$SLNO^2
table_output$df <- temp
})
observeEvent(input$goButton, {
if(nrow(table_output$df) > 1) {
output$all<-renderDataTable({
table_output$df})
}
}
)
observeEvent(input$Reset, { #### WE EMPTY THE PLOT AREA HERE
table_output <- reactiveValues(df = data.frame(SLNO = as.numeric(), Col2 = as.numeric()))
output$all<-renderDataTable({})
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1
Reputation: 66
Check if this works out for you:
library(shiny)
library(data.table)
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
server <- function(input, output, session) {
table_output <- eventReactive({input$goButton}, data.frame("SLNO" = seq(1:input$n), "Col2" = seq(1:input$n)^2))
observeEvent(input$goButton, {
output$all <- renderDataTable({
table_output()})
})
observeEvent(input$Reset, {
output$all <- renderDataTable({ })})}
shinyApp(ui = ui, server = server)
Upvotes: 2