将编辑的Datatable值插入闪亮的应用程序中

时间:2019-02-27 17:42:23

标签: r shiny dt datatables-1.10

我试图了解如何使用已编辑数据表中的值并进行一些计算。

我有一个默认加载的数据框。当您单击“运行”时,它将根据输入值更新表。

我希望用户在表中手动编辑值,然后单击“运行”。接下来,我希望该应用程序从数据表中获取编辑后的值,运行一些计算并更新该表。通过这种方式,用户可以动态地在表上看到他们输入的结果。

library(shiny)
library(DT)
library(dplyr)

#### Module 1 renders the first table
tableMod <- function(input, output, session, modelRun,modelData,budget){

  output$x1 <- DT::renderDataTable({
    modelRun()
    isolate(
      datatable(
        modelData %>% 
          mutate(New_Membership  = as.numeric(Modified * 0.01)*(budget())),
        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
  })
}
tableUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("x1"))
}

ui <- function(request) {
  fluidPage(
    tableUI("opfun"),
    numericInput("budget_input", "Total Forecast", value = 2),
    actionButton("opt_run", "Run")
  )
}
server <- function(input, output, session) {

  df <- data.frame(Channel = c("A", "B","C"),
                   Current = c(2000, 3000, 4000),
                   Modified = c(2500, 3500,3000),
                   New_Membership = c(450, 650,700),
                   stringsAsFactors = FALSE)

  callModule( tableMod,"opfun",
              modelRun = reactive(input$opt_run),
              modelData = df,
              budget = reactive(input$budget_input))

  observeEvent(input$opt_run, {
    cat('HJE')
  })
}

shinyApp(ui, server, enableBookmarking = "url")

2 个答案:

答案 0 :(得分:6)

一个干净的解决方案(而不是在其他环境中分配值)将是在与reactiveVal同步的模块中使用datatable。您可以从模块中返回此reactive,以便在主应用程序中也可以使用它:

library(shiny)
library(DT)
library(dplyr)

#### Module 1 renders the first table
tableMod <- function(input, output, session, modelRun, modelData, budget){

  df <- reactiveVal(modelData) ## this variable will be in sync with your datatable

  output$x1 <- DT::renderDataTable({
    modelRun()
    isolate(
      datatable(
        df() %>% ## ...you always use the synced version here
          mutate(New_Membership  = as.numeric(Modified * 0.01)*(budget())),
        selection = 'none', editable = TRUE
      )
    )
  })


  observeEvent(input$x1_cell_edit, { 
    new_df <- df()
    row <- input$x1_cell_edit$row
    col <- input$x1_cell_edit$col
    value <- as.numeric(input$x1_cell_edit$value)
    new_df[row, col] <- value
    df(new_df) ## ...and you make sure that 'df' stays in sync
  })

  list(updated_df = df) ## ...finally you return it to make use of it in the main app too
}
tableUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("x1"))
}

ui <- function(request) {
  fluidPage(
    tableUI("opfun"),
    numericInput("budget_input", "Total Forecast", value = 2),
    actionButton("opt_run", "Run")
  )
}
server <- function(input, output, session) {

  df <- data.frame(Channel = c("A", "B","C"),
                   Current = c(2000, 3000, 4000),
                   Modified = c(2500, 3500,3000),
                   New_Membership = c(450, 650,700),
                   stringsAsFactors = FALSE)

  result <- callModule( tableMod,"opfun",
              modelRun = reactive(input$opt_run),
              modelData = df,
              budget = reactive(input$budget_input))

  observeEvent(input$opt_run, {
    str(result$updated_df())
  })
}

shinyApp(ui, server, enableBookmarking = "url")

答案 1 :(得分:3)

这应该可以,但是不能实现“最干净”的实现:

为了使您的代码正常工作,我不得不从df中脱颖而出。编辑数据表后,使用assign替换全局环境中的df(不是最好的主意...)。但是,只有按下Run才会重新计算数据。一旦按下Run,就会覆盖modelData:(modelData <- df)。同样不是最好的主意,可能使modelData是反应性的会更好。

另外,请查看DT::replaceData。比重新生成全表更好的主意。

library(shiny)
library(DT)
library(dplyr)

df <- data.frame(Channel = c("A", "B","C"),
                 Current = c(2000, 3000, 4000),
                 Modified = c(2500, 3500,3000),
                 New_Membership = c(450, 650,700),
                 stringsAsFactors = FALSE)

#### Module 1 renders the first table
tableMod <- function(input, output, session, modelRun,modelData,budget){


  observeEvent( input$x1_cell_edit, {
    cat ('input$x1_cell_edit \n')
    info = input$x1_cell_edit
    str(info)
    i = info$row
    j = info$col
    v = info$value
    df[i, j] <- DT:::coerceValue(v, df[i, j])
    assign("df", df, envir = .GlobalEnv)

  })

  output$x1 <- DT::renderDataTable({
    modelRun()
    modelData <- df
    isolate(
      datatable(
        modelData %>% 
          mutate(New_Membership  = as.numeric(Modified * 0.01)*(budget())),
        selection = 'none', editable = TRUE
      )
    )
  })


}
tableUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("x1"))
}

ui <- function(request) {
  fluidPage(
    tableUI("opfun"),
    numericInput("budget_input", "Total Forecast", value = 2),
    actionButton("opt_run", "Run")
  )
}
server <- function(input, output, session) {

  callModule( tableMod,"opfun",
              modelRun = reactive(input$opt_run),
              modelData = df,
              budget = reactive(input$budget_input))

  observeEvent(input$opt_run, {
    cat('HJE')
  })
}

shinyApp(ui, server, enableBookmarking = "url")