gpt4 book ai didi

r - 使用多个 react 值

转载 作者:行者123 更新时间:2023-12-01 11:35:03 25 4
gpt4 key购买 nike

我正在尝试创建一个 Shiny 的 R 应用程序,用户可以在其中输入 2 个日期:开始日期和结束日期(假设用户将选择特定周的任一日期)。通过选择日期,用户将能够看到他将在那几天内从下周的商品 list 中卖出多少商品。我得到了一周内每天发生的总销售额百分比的数据。使用它并使用过去一周的每件商品的销售数据,我试图创建该应用程序。但是我认为我在使用响应式(Reactive)表达式时犯了一些错误。任何帮助将不胜感激。我提供了以下代码。

ui.R
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
dateInput('Start_Date',label = "starting on:",value = Sys.Date())
dateInput('End_Date',label = "Ending on:",value = Sys.Date())
),
mainPanel(
tableoutput("mytable")
)
)
))

server.R
library(shiny)
library(stats)
shinyServer(function(input, output) {
Days<-c("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
Percent_sales_by_day<-c(.10,.14,.14,.14,.14,.17,.17)
Data_days<-data.frame(Days,Percent)
items_sold<-c("A","B","C","D")
sales_last_week<-c("100","200","300","800")
Data_sales<-data.frame(items_sold,sales_last_week)
Day_vector<-reactive({
weekdays(seq(as.Date(input$Start_Date),as.Date(input$End_Date),by = "days"))
})
Daily_split_vector<-reactive({
library(dplyr)
Data_days%>%
filter(Days %in% Day_vector())
Data_days$Percent_sales_by_day
})
Daily_split_value<-reactive({
sum(Daily_split_vector())
})
Forecast<-reactive({
Data_sales%>%
mutate(sales_last_week=sales_last_week* Daily_split_value())
})
output$mytable<-renderTable({
Forecast()
})
})

最佳答案

我不是 100% 清楚您的基本目标,但不管下面的代码是否适合我。我试图评论我所做的所有更改 - 它们大多只是轻微的语法错误 - 但如果您希望我澄清任何内容,请告诉我。


ui.R:

library(shiny)
##
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(

dateInput(
'Start_Date',
label = "starting on:",
value = Sys.Date()
), ## added comma
dateInput(
'End_Date',
label = "Ending on:",
value = Sys.Date())
),

mainPanel(
tableOutput("mytable") ## 'tableOutput' not 'tableoutput'
)

)
))

server.R:

library(shiny)
library(dplyr)
options(stringsAsFactors=F) ## try to avoid factors unless you
## specifically need them
##
shinyServer(function(input, output) {

Days <- c(
"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday")

Percent_sales_by_day <- c(
.10,.14,.14,.14,.14,.17,.17)

Data_days <- data.frame(
Days,
Percent_sales_by_day) ## changed from 'Percent'

items_sold <- c("A","B","C","D")

sales_last_week <- c(
100,200,300,800) ## changed from character (???) to numeric type

Data_sales <- data.frame(
items_sold,
sales_last_week)

Day_vector <- reactive({
weekdays(
seq.Date(
as.Date(input$Start_Date),
as.Date(input$End_Date),
by = "day"))
})

Daily_split_vector <- reactive({
Data_days %>%
filter(Days %in% Day_vector()) %>% ## added pipe
## Data_days$Percent_sales_by_day ## changed this line
select(Percent_sales_by_day) ## to this line
})

Daily_split_value <- reactive({
sum(Daily_split_vector())
})

Forecast <- reactive({
Data_sales%>%
mutate(
sales_last_week=sales_last_week* Daily_split_value())
})

output$mytable <- renderTable({
Forecast()
})
})

关于r - 使用多个 react 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27874012/

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