0
votes

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:

  1. select an value from id column (selectInput).
  2. add text comment in a text box (textInput)
  3. click on an action button
  4. 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)
1

1 Answers

1
votes
  1. The column id is not recognized as a column of the data.frame df in df[id == input$selectId, "Comment], replacing id by df$id fixes the error.

  2. In order to rerender the datatable after updating df, df should be a reactive object.

  3. To handle multiple selected id's in the selectInput selectId, you might want to replace df$id == input$selectId by df$id %in% input$selectId

This updated server function should help you with these issues:

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

  ## make df reactive
  df_current <- reactiveVal(df)

  observeEvent(input$button, {

      req(df_current())

      ## update df by adding comments
      df_new <- df_current()
      df_new[df_current()$id %in% input$selectID, "Comment"] <- input$comment

      df_current(df_new)

  })

  output$data <- DT::renderDataTable({

      req(df_current())

      DT::datatable(df_current(), 
          options = list(orderClasses = TRUE,
              lengthMenu = c(5, 10, 20), pageLength = 5))
  })

}