我正在建设一个使用闪亮的Web应用程序,我不确定如何最好地构建应用程序,因为输入取决于数据和输出(图表)取决于基于输入的汇总数据。R闪亮的应用程序,输入取决于更新的数据
我试图想出一个简单的应用程序来重现问题。我的设置更先进,与示例无关。假设你有一个产品线,并想分析销售。假设每天都创建一个数据集(我不是说数据结构是最优的,但它对于说明我的问题很有用)。现在在应用程序中,从可用日期列表中选择一个日期,然后选择一个产品。日期限于数据可用期限,产品列表仅限于在选定日期实际销售的产品。然后,我们希望绘制一天中每个小时的总销售额。
我将在下面列出一些代码,其中也创建了一些示例数据。对不起,“长”的代码。这是有点工作,但我有一些担忧。
我的问题是:
1)我不知道以何种顺序的东西,每次执行,特别是在第一次加载应用程序时,然后输入的变化。同样,数据取决于第一个输入,第二个输入取决于数据。第三,计算图表友好的数据集,用于图表。您可能会注意到错误信息会打印到控制台(并在浏览器中短暂闪烁),但由于这些值可用,因此会进行更新并显示图。这似乎并不理想。
2)当输入依赖于数据/服务器.R时,当前的最佳实践是什么?我看到这个https://groups.google.com/forum/?fromgroups=#!topic/shiny-discuss/JGJx5A3Ge-A,但它似乎没有实现,甚至认为这个帖子是相当老。
下面是两个文件的代码:
# ui.R
######
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("New Application"),
sidebarPanel(
htmlOutput("dateInput"),
htmlOutput("prodInput")
),
mainPanel(
plotOutput("salesplot")
)
))
和:
#server.R
#########
library(shiny)
library(filehash)
set.seed(1)
dates <- format(seq(Sys.Date() - 10, Sys.Date(), "days"), "%Y-%m-%d")
products <- LETTERS
prices <- sample(10:100, size = length(products), replace = TRUE)
names(prices) <- LETTERS
if (file.exists("exampledb")) {
db <- dbInit("exampledb")
} else {
dbCreate("exampledb")
db <- dbInit("exampledb")
for (d in dates) {
no.sales <- sample(50:100, size = 1)
x <- data.frame(
product = sample(products, size = no.sales, replace = TRUE)
,hour = sample(8:20, size = no.sales, replace = TRUE)
,order.size = sample(1:10, size = no.sales, replace = TRUE)
)
x$price <- prices[x$product]
dbInsert(db, paste0("sales", gsub("-", "", d)), x)
}
}
current <- reactiveValues()
shinyServer(function(input, output) {
inputDates <- reactive({
sort(strptime(unique(substr(names(db), 6, 13)), "%Y%m%d"))
})
output$dateInput <- renderUI({ dateInput(
inputId = "date",
label = "Choose hour",
min = min(inputDates()),
max = max(inputDates()),
format = "yyyy-mm-dd",
startview = "month",
weekstart = 0,
language = "en")
})
inputProducts <- reactive({
current$data <<- dbFetch(db, paste0("sales", format(input$date, "%Y%m%d")))
sort(unique(current$data$product))
})
output$prodInput <- renderUI({ selectInput(
inputId = "product",
label = "Choose Product",
choices = inputProducts(),
selected = 1)
})
output$salesplot <- renderPlot({
pdata <- aggregate(I(order.size*price) ~ hour,
data = subset(current$data, product == input$product),
FUN = sum)
colnames(pdata)[2] <- "value"
plot(value ~ hour, data = pdata, xlim = c(8, 20))
})
})
关于“谷歌小组讨论”@WinstonChang最后一条消息中的一条说,提到的更改已经合并到版本'0.6.0'中。 IIRC,实际版本是'0.7.0',所以你应该可以使用它。至于第1点,我不能现在说,因为我需要到我的Shiny环境。无论如何,您可以尝试使用'cat'来跟踪'server.R'的执行情况。 –
你能解决这个问题吗?我有同样的问题。 –