我真的需要你的帮助。我试图建立一个闪亮的应用程序,可以根据以前完成的项目的大小发布项目的大致时间。(见下面的df)
Df看起来像这样(样本)。
Department Hours SIZE
1 HVAC 1281 38000
7 HVAC 202 38000
13 HVAC 52 33000
19 HVAC 118 33000
25 HVAC 2069 22000
31 HVAC 1546 22000
37 HVAC 282 450
43 HVAC 1 17000
49 HVAC 725 7000
55 HVAC 50 250000
61 HVAC 1573 11000
67 HVAC 100 11000
73 HVAC 562 500
79 HVAC 1441 7900
我使用geom_smooth制作ggplot图,以根据SIZE查看每个部门的平均小时数。
这就是它的样子。
这是我正在努力的Shiny APP骨架。我希望表格根据ggplot蓝线更改值。因此,例如,如果在此示例中将SIZE输入设置为50.000,则表格将显示大约2000的值。
我真的希望它有意义,因为我知道这是令人困惑的。
有关如何进行的任何想法?我真的很感激。
这是Shiny APP代码
library(shiny)
hello <- data.frame(125)
colnames(hello) <- "VALUE"
ui <- fluidPage(titlePanel("Pricing Calculator", windowTitle = "app"),
sidebarLayout(sidebarPanel(
sliderInput(inputId = "slideruno",label = "Size (m2)", min = 0, max = 250000,
value = 50000,step = 2500, ticks = T)
)
,mainPanel(
tableOutput(outputId = "tableuno")
)
))
server <- function(input, output) {
output$tableuno <- renderTable(hello)
}
shinyApp(ui, server)
用于制作ggplot的代码:
ggplot(HVAC, aes(SIZE, Hours)) + geom_point(aes(color = Department)) + stat_smooth()
数据首次亮相
dput(HVAC)
structure(list(Department = c("HVAC", "HVAC", "HVAC", "HVAC",
"HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC",
"HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC",
"HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC",
"HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC",
"HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC",
"HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC",
"HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC",
"HVAC", "HVAC", "HVAC", "HVAC", "HVAC", "HVAC"), Hours = c(1281,
202, 52, 118, 2069, 1546, 282, 1, 725, 50, 1573, 100, 562, 1441,
475, 1415, 191, 1498, 33, 763, 1, 81, 1433, 305, 1561, 1960,
6834, 274, 1465, 646, 3586, 77, 52, 395, 197, 10, 67, 33, 59,
431, 4, 154, 665, 461, 35, 331, 401, 605, 26, 99, 3981, 598,
47, 434, 39, 3578, 364, 124, 666, 58, 3086, 375, 7, 277, 29,
47), SIZE = c(38000, 38000, 33000, 33000, 22000, 22000, 450,
17000, 7000, 250000, 11000, 11000, 500, 7900, 7900, 7900, 77000,
1e+05, 1000, 2250, 2250, 15000, 35000, 7000, 7000, 1000, 51000,
51000, 15000, 29000, 23000, 23000, 23000, 3600, 27500, 2800,
2800, 3500, 3500, 6500, 20000, 192000, 2200, 8000, 30000, 36000,
6500, 6500, 11000, 70000, 55000, 3000, 1600, 36000, 36000, 17000,
1800, 5800, 15000, 46000, 26000, 34000, 7500, 130000, 36000,
15000)), .Names = c("Department", "Hours", "SIZE"), class = "data.frame", row.names = c(1L,
7L, 13L, 19L, 25L, 31L, 37L, 43L, 49L, 55L, 61L, 67L, 73L, 79L,
85L, 91L, 103L, 109L, 115L, 121L, 127L, 133L, 139L, 145L, 151L,
157L, 163L, 169L, 175L, 181L, 187L, 193L, 199L, 205L, 211L, 217L,
223L, 229L, 241L, 253L, 259L, 265L, 271L, 277L, 283L, 289L, 295L,
301L, 307L, 313L, 319L, 325L, 331L, 337L, 343L, 349L, 355L, 361L,
367L, 373L, 379L, 397L, 403L, 415L, 421L, 445L))
答案 0 :(得分:1)
或者你可以从ggplot获得stat_smooth,如
p=ggplot(HVAC, aes(SIZE, Hours)) + geom_point(aes(color = Department)) + stat_smooth(aes(outfit=fit<<-..y..))
ddd=ggplot_build(p)$data[[2]][,1:2]
并且完全闪亮将是
library(shiny)
library(ggplot2)
ui <- fluidPage(titlePanel("Pricing Calculator", windowTitle = "app"),
sidebarLayout(sidebarPanel(
sliderInput(inputId = "slideruno",label = "Size (m2)", min = 0, max = 250000,
value = 50000,step = 2500, ticks = T)
)
,mainPanel(
tableOutput(outputId = "tableuno")
)
))
server <- function(input, output) {
output$tableuno <- renderTable({
p=ggplot(HVAC, aes(SIZE, Hours)) + geom_point(aes(color = Department)) + stat_smooth(aes(outfit=fit<<-..y..))
ddd=ggplot_build(p)$data[[2]][,1:2]
ddd=ddd[ddd[[2]]<=input$slideruno,]
data.frame("Value"=ddd[nrow(ddd),1])
})
}
shinyApp(ui, server)
当然,如果我正确理解您希望如何从stat_smooth()
答案 1 :(得分:0)
不确定完全理解,但您可能需要在服务器端构建模型,然后使用predict
。 stat_smooth
默认使用loess
。有关iris
数据集的示例,因为我无法重现您的示例:
mod <- loess(Sepal.Length ~ Sepal.Width, iris)
predict(mod, newdata = data.frame(Sepal.Width=3))
服务器端:
在这里,您可能只需要使用您的值(Hours
,SIZE
,HVAC
)更改它,并更改3
的{{1}}。然后在input$slideruno
内将predict
的结果分配给output$predicted
。
UI方面:
只是renderText
。