我试图在shiny 的计算器中制作一个计算器,用户可以编辑一列,数据表将更新其他列中的计算,这些计算使用DT内的用户输入,侧面板中的用户输入,和/或其他列中的其他计算值.

以下是我目前所掌握的.我希望用户能够编辑"ppl"列,然后使用它来更新其他列.我只是用72填充了"ppl",以便有一些东西在那里.自从我刚到R Shiny公司,我就被困了一段时间.我见过有人使用过EscreveEvent,但我仍然不知道如何实现它.任何帮助或指导都是非常感谢的.

library(tidyverse)
library(shiny)
library(DT)

ui <- 
  sidebarLayout(
    sidebarPanel(
      numericInput("price", label = "Price ($)", value = 4.31, min = 0),
      numericInput("sqft", label = "Average Square Footage", value = 4400, min = 0),
      numericInput("delivery", label = "Delivery Cost", value = 2.60, min = 0),
      numericInput("avg_use", label = "Average Annual Use", value = 88000, min = 0),
      numericInput("turf", label = "Percentage (%)", value = 0, min = 0)
    ),
    mainPanel(
      DTOutput("table1")
    )
  )


server <- function(input, output, session) {
  
  df <- reactive({
    
    data.frame(
      behavior = c("A",
                   "B",
                   "C",
                   "D",
                   "E",
                   "F",
                   "G",
                   "H",
                   "I",
                   "J",
                   "K"),
      average = c(2541, 11913, 12707, 16995, 23668, 2859, 2224, 10483, 22555, 8259, 5718),
      ppl = c(72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72)
      
    ) |>
      # If there is an input for 'Percentage', modify E's total_land value by using the input
      mutate(total_land = ifelse(str_detect(behavior, "E") & input$turf > 0,
                                 ppl * input$sqft * input$turf,
                                 ppl * input$sqft),
             all_w = average * (total_land / 1000),
             all_ut = input$price * (all_w / 1000),
             annual_w = all_w / ppl,
             mon_w = annual_w / 12,
             annual_ut = all_ut / ppl,
             mon_ut = annual_ut / 12,
             deliv = (all_w / 1000) * input$delivery,
             supply = all_w / input$avg_use
      )
  })
  
    output$table1 <- renderDT(
      df(),
      selection = 'none',
      editable = list(target = 'column', disable = list(columns = c(0:1, 3:12))),
      server = T,
      rownames = F
    )
}

shinyApp(ui, server)

推荐答案

library(tidyverse)
library(shiny)
library(DT)

ui <- 
  sidebarLayout(
    sidebarPanel(
      numericInput("price", label = "Price ($)", value = 4.31, min = 0),
      numericInput("sqft", label = "Average Square Footage", value = 4400, min = 0),
      numericInput("delivery", label = "Delivery Cost", value = 2.60, min = 0),
      numericInput("avg_use", label = "Average Annual Use", value = 88000, min = 0),
      numericInput("turf", label = "Percentage (%)", value = 0, min = 0)
    ),
    mainPanel(
      DTOutput("table1")
    )
  )

dat00 <- data.frame(
  behavior = c("A",
               "B",
               "C",
               "D",
               "E",
               "F",
               "G",
               "H",
               "I",
               "J",
               "K"),
  average = c(2541, 11913, 12707, 16995, 23668, 2859, 2224, 10483, 22555, 8259, 5718),
  ppl = c(72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72)
)

makeData <- function(dat0, turf, sqft, price, delivery, avg_use) {
  dat0 |>
    # If there is an input for 'Percentage', modify E's total_land value by using the input
    mutate(total_land = ifelse(str_detect(behavior, "E") & turf > 0,
                               ppl * sqft * turf,
                               ppl * sqft),
           all_w = average * (total_land / 1000),
           all_ut = price * (all_w / 1000),
           annual_w = all_w / ppl,
           mon_w = annual_w / 12,
           annual_ut = all_ut / ppl,
           mon_ut = annual_ut / 12,
           deliv = (all_w / 1000) * delivery,
           supply = all_w / avg_use
    )
}

server <- function(input, output, session) {
  
  Dat0 <- reactiveVal(dat00)

  output$table1 <- renderDT({
    dat <- makeData(
      isolate(Dat0()), 
      input$turf, input$sqft, input$price, input$delivery, input$avg_use
    )
    datatable(
      dat,
      rownames = FALSE,
      selection = "none",
      editable = list(target = "cell", disable = list(columns = c(0:1, 3:12)))
    )
  }, server = TRUE)
  
  proxy <- dataTableProxy("table1")
  
  observeEvent(input[["table1_cell_edit"]], {
    info <- input[["table1_cell_edit"]] 
    # update Dat0
    dat0 <- editData(Dat0(), info, rownames = FALSE) 
    Dat0(dat0) 
    # update the data in the table
    dat <- makeData(
      dat0, 
      input$turf, input$sqft, input$price, input$delivery, input$avg_use
    )
    replaceData(proxy, dat, resetPaging = FALSE, rownames = FALSE)
  })
  
}

shinyApp(ui, server)

R相关问答推荐

使用scale_x_continuous复制ggplot 2中的离散x轴

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

编码变量a、b、c以匹配来自另一个数据点的变量x

使用ggplot 2根据R中的类别排列Likert比例gplot

为什么当我try 在收件箱中使用合并功能时会出现回收错误?

过滤器数据.基于两列的帧行和R中的外部向量

Highcharter多次钻取不起作用,使用不同方法

如何利用模型函数在格图中添加双曲/指数曲线

使用R闪光显示所有数据点作为默认设置

以更少间隔的较小表中的聚合离散频率表

如何将R中数据帧中的任何Nas替换为最后4个值

正在导出默认的RStudio主题,还是设置括号 colored颜色 ?

WRS2包中带有bwtrim的简单ANOVA抛出错误

有没有办法定制Plot(allEffects())面板标题?

R-使用stri_trans_General()将其音译为德语字母

如何更改包中函数中的参数?

roxygen2正在处理太多的文件

以R表示的NaN值的IS.NA状态

列间序列生成器的功能

通过不完全重叠的多个柱连接