Shiny: how to update the value in a table cell?

Using visual elements, a dataframe is created.
I want to update any cell of this table, using visual elements.
I don't understand where my mistake is.
Save button doesn't work.

makeTab <- function(x, y) {
  tb <- data.frame(mes = 1:y)
  for (i in 2:x) {
    tb <- data.frame(tb, mes = 1:y)
  }
  return(tb)
}

# ========= test makeTab() function
t <- makeTab(x = 3, y = 8)
t

# =================
library(shiny)

ui <- fluidPage(

  sliderInput(
    inputId = 'ncol',
    label = 'Columns',
    value = 7,
    min = 1,
    max = 10,
    step = 1,
    ticks = T,
  ),

  sliderInput(
    inputId = 'nrow',
    label = 'Rows',
    value = 8,
    min = 1,
    max = 20,
    step = 1,
    ticks = T,
  ),

  # ============ set column number for editing
  sliderInput(
    inputId = 'ncolEdit',
    label = 'Col for edit',
    value = 3,
    min = 1,
    max = 10,
    step = 1,
    ticks = T,
  ),

  # ============ set row number for editing
  sliderInput(
    inputId = 'nrowEdit',
    label = 'Row for edit',
    value = 4,
    min = 1,
    max = 20,
    step = 1,
    ticks = T,
  ),

  # ============ edit cell
  textInput(
    inputId = 'val',
    label = 'Volume',
    value = '100'
  ),

  actionButton(inputId = 'btSave',
               label = 'Save' ),

  tableOutput(outputId = 'tab'),
)


#=================================
server <- function(input, output) {

   #---------- create table
   tb <- reactive({
     r <- makeTab(x = input$ncol, y = input$nrow)
     return(r)
   })

  #----------- save new value to table - does not work
  # tb <- eventReactive(
  #   eventExpr = input$btSave,
  #   valueExpr = {
  #     tb[input$nrowEdit, input$ncolEdit] <- input$val
  #   })

  output$tab <- renderTable(expr = {tb()})
}

#=================================
shinyApp(ui = ui, server = server)

Hi @Korall,
this does what you want

makeTab <- function(x, y) {
  tb <- data.frame(mes = 1:y)
  for (i in 2:x) {
    tb <- data.frame(tb, mes = 1:y)
  }
  return(tb)
}

# ========= test makeTab() function
t <- makeTab(x = 3, y = 8)
t

# =================
library(shiny)

ui <- fluidPage(
  sliderInput(
    inputId = 'ncol',
    label = 'Columns',
    value = 7,
    min = 1,
    max = 10,
    step = 1,
    ticks = T,
  ),
  
  sliderInput(
    inputId = 'nrow',
    label = 'Rows',
    value = 8,
    min = 1,
    max = 20,
    step = 1,
    ticks = T,
  ),
  
  # ============ set column number for editing
  sliderInput(
    inputId = 'ncolEdit',
    label = 'Col for edit',
    value = 3,
    min = 1,
    max = 10,
    step = 1,
    ticks = T,
  ),
  
  # ============ set row number for editing
  sliderInput(
    inputId = 'nrowEdit',
    label = 'Row for edit',
    value = 4,
    min = 1,
    max = 20,
    step = 1,
    ticks = T,
  ),
  
  # ============ edit cell
  textInput(
    inputId = 'val',
    label = 'Volume',
    value = '100'
  ),
  
  actionButton(inputId = 'btSave',
               label = 'Save'),
  
  tableOutput(outputId = 'tab')
)


#=================================
server <- function(input, output) {
  
  tbl <- reactiveVal(NA)
  #---------- create table
  tb <- reactive({
    r <- makeTab(x = input$ncol, y = input$nrow)
    tbl(r)
    return(r)
  })
  
  #----------- save new value to table 
  observeEvent(eventExpr = input$btSave,
               handlerExpr =  {
                temp <- tbl()
                temp[input$nrowEdit, input$ncolEdit] <- input$val
                tbl(temp)
               })
  
  output$tab <- renderTable(expr = {
    # because of lazy evaluation call it doing nothing 
    tb()[1, ]
    # actually show something
    tbl()
  })
}

#=================================
shinyApp(ui = ui, server = server)

The reactive can't be called twice and i can't find any easy way to update a reactive.

Hope this brings you to some clever idea.

Great! Thanks a lot, Problem solved. I will use your solution. But it seems to me that there must be other ways)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.