I'm trying to create a shiny app where user is able to add text comment to a table.
I created a dataframe with 3 columns: num
, id
and val
. I want my shiny app to do the following:
- select an value from id column (selectInput).
- add text comment in a text box (textInput)
- click on an action button
- A new column called
comment
is created in the data table, text comments are added to the comment column in the row where id equals the value selected.
My shiny app code is below. When I select an value from selectinput, add some comment in the text box and click on `add comment' button, my shiny app window shut down by itself.
Does anyone know why that happens?
Thanks a lot in advance!
library(shiny)
library(DT)
df = data.frame(num=1:10, id=LETTERS[1:10], val=rnorm(10))
ui = fluidPage(
fluidRow(
column(2, selectInput(inputId = 'selectID',
label = 'Select ID2',
choices = LETTERS[1:10],
selected='',
multiple=TRUE)),
column(6, textInput(inputId = 'comment',
label ='Please add comment in the text box:',
value = "", width = NULL,
placeholder = NULL)),
column(2, actionButton(inputId = "button",
label = "Add Comment"))
),
fluidRow (
column(12, DT::dataTableOutput('data') )
)
)
server <- function(input, output, session) {
observeEvent(input$button, {
df[id==input$selectID, 'Comment']=input$comment
})
output$data <- DT::renderDataTable({
DT::datatable(df,
options = list(orderClasses = TRUE,
lengthMenu = c(5, 10, 20), pageLength = 5))
})
}
shinyApp(ui=ui, server=server)