1

I'm trying to append a value taken from an input (in the present case input$n) to a list (in the present case the variable "keyword_list"), when the user presses the an action button (in the present case the button input$goButton).

ui.R
library(shiny)

pageWithSidebar(
  headerPanel("actionButton test"),
  sidebarPanel(
    #numericInput("n", "N:", min = 0, max = 100, value = 50),
    textInput("n", "Caption", "Enter next keyword"),
    br(),
    actionButton("goButton", "Go!"),
    p("Click the button to update the value displayed in the main panel.")
  ),
  mainPanel(
    verbatimTextOutput("nText"),
    dataTableOutput('mytable')
  )
)
})

server.R
library(shiny)

# Define server logic required to summarize and view the selected
# dataset
function(input, output,session) {

#prepare data
keyword_list <- matrix()
makeReactiveBinding('keyword_list')

observe({
  if (input$goButton == 0)
    return()

  isolate({
    keyword_list <- append(keyword_list,input$n)  })
})

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


  output$nText <- renderPrint({
    #input$n
    ntext()
  })

  output$mytable = renderDataTable({
    as.data.frame(keyword_list)
  })


}

1 Answer 1

1

How about this:

library(shiny)

ui <- pageWithSidebar(
  headerPanel("actionButton test"),
  sidebarPanel(
    #numericInput("n", "N:", min = 0, max = 100, value = 50),
    textInput("n", "Caption", "Enter next keyword"),
    br(),
    actionButton("goButton", "Go!"),
    p("Click the button to update the value displayed in the main panel.")
  ),
  mainPanel(
    verbatimTextOutput("nText"),
    dataTableOutput('mytable')
  )
)
})

library(shiny)

# Define server logic required to summarize and view the selected
# dataset
server <- function(input, output,session) {

  global <- reactiveValues(keyword_list = "")

  observe({
    if (input$goButton == 0)
      return()

    isolate({
      global$keyword_list <- append(global$keyword_list, input$n)
    })
  })

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


  output$nText <- renderPrint({
    #input$n
    ntext()
  })

  output$mytable = renderDataTable({
    as.data.frame(global$keyword_list)
  })
}

shinyApp(ui, server)
Sign up to request clarification or add additional context in comments.

3 Comments

I would go with reactiveValues as well but instead of if (input$goButton == 0) return() I would use req(input$goButton != 0) :)
i was also not a fan of this part to be honest, but prefer to change the original code as least as possible to only answer the actual question.
Hi, Both solutions work great ! Thank you very much. Now I can go skiing with my mind in peace ;-). Have a good day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.