Reputation: 1433
I have a shiny app where one of the functionality is allowing the user to edit the values in the table and when clicked on run it will use the user inputs as the value to a function and then update the results in the same table. Below is the example of the current table and the expected table. So in the first table if the user changes the values for current for Channel A and C and click run it should update itself to the values reflected in table expected output.
So my question is, is it possible to take editable DT values as an input to a function.
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
DT::dataTableOutput("x1"),
actionButton("opt_run", "Run"),
tags$h1("Expected Output"),
DT::dataTableOutput("x2")
),
server = function(input, output, session) {
df <- data.table(Channel = c("A", "B","C"),
Current = c("2000", "3000","4000"),
Modified = c("2500", "3500","3000"),
New_Membership = c("450", "650","700"))
output$x1 = renderDT(df, selection = 'none', editable = TRUE)
expdf <- data.table(Channel = c("A", "B","C"),
Current = c("3000", "3000","5000"),
Modified = c("3500", "3500","6000"),
New_Membership = c("650", "650","1100"))
output$x2 = renderDT(expdf, selection = 'none', editable = TRUE)
})
}
)
Upvotes: 1
Views: 2753
Reputation: 29417
I wasnt sure if you want them to be stored globally or not. I will give you a global version so you can save it somewhere, to a DB or on Disk:
You can access the cell values using input$x1_cell_edit
, note that I am pressing F5
to refresh the page to check if the values have been saved or not
library(shiny)
library(DT)
options(stringsAsFactors = F)
df <- data.frame(Channel = c("A", "B","C"),
Current = c("2000", "3000","4000"),
Modified = c("2500", "3500","3000"),
New_Membership = c("450", "650","700"))
expdf <- data.frame(Channel = c("A", "B","C"),
Current = c("3000", "3000","5000"),
Modified = c("3500", "3500","6000"),
New_Membership = c("650", "650","1100"))
shinyApp(
ui = fluidPage(
DT::dataTableOutput("x1"),
tags$h1("Expected Output"),
DT::dataTableOutput("x2")
),
server = function(input, output, session) {
output$x1 = renderDT(df, selection = 'none', editable = TRUE)
observeEvent(input$x1_cell_edit, {
df[input$x1_cell_edit$row,input$x1_cell_edit$col] <<- input$x1_cell_edit$value
})
output$x2 = renderDT(expdf, selection = 'none', editable = TRUE)
observeEvent(input$x2_cell_edit, {
expdf[input$x2_cell_edit$row,input$x2_cell_edit$col] <<- input$x2_cell_edit$value
})
})
}
)
Upvotes: 6