gpt4 book ai didi

r - 在 Shiny 的应用程序中插入已编辑的数据表值

转载 作者:行者123 更新时间:2023-12-02 06:27:14 25 4
gpt4 key购买 nike

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

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

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

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")

最佳答案

一个干净的解决方案(而不是在其他环境中分配值)是在您的模块中使用一个与数据表同步的reactiveVal。您可以从您的模块返回此 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")

关于r - 在 Shiny 的应用程序中插入已编辑的数据表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54911464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com