我试图在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)