我正在尝试创建一个闪亮的应用程序,该应用程序将接受各种输入,并创建一个输出表,该表具有记录用户输入并根据用户输入计算成本字段的列。我已经成功地用用户输入创建了数据表,但是我无法获得根据输入数据来计算新值的字段。
data <- structure(list(product = c("Cars", "Trucks", "Buses", "Cars",
"Trucks", "Buses"), offshore = c(0, 0, 0, 1, 1, 1)), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
library(shiny)
library(DT)
library(data.table)
library(lubridate)
library(tidyverse)
mydata = data.frame(Product=NA,
Start_date = NA,
End_date = NA,
On_shore_cost = NA,
Off_shore_cost = NA,
Cost = NA)
ui <- fluidPage(
wellPanel(
flowLayout(
selectInput("select", "Product",
choices = list("Cars" = 1,
"Trucks" = 2,
"Buses" = 3), selected = 1),
dateInput("sdate", "Start date"),
dateInput("edate", "End date"),
numericInput("onshore","On-Shore Cost",1),
numericInput("offshore","Off-Shore Cost",1)
actionButton("action","Blast Off")),
downloadButton("downloadData", "Download")),
dataTableOutput("table")
)
我要创建的字段是“成本=”字段。我使用dplyr表达式来操纵数据。我似乎无法使它正常工作。作为奖励,一旦用户建立了所需的表,我希望他们能够下载该表。我还没有花很多时间在下载上。您能帮我弄清楚如何填充数据表的“费用”字段吗?
# Define server logic
server <- function(input, output) {
output$table <- renderDataTable( df())
df <- eventReactive(input$action,{
if(input$select!="" && !is.null(input$onshore) && input$action>0){
newrow = data.table(Product = input$select,
Start_date = as.character(input$sdate),
End_date = as.character(input$edate),
On_shore_cost =input$onshore,
Off_shore_cost = input$offshore,
#########This is the trouble spot, I am trying to generate this field
Cost = renderText({data %>% filter(product == input$select) %>%
mutate(pc_cost= ifelse(offshore == 0 , input$onshore,input$offshore)) %>%
summarise(total = sum(pc_cost)) %>% pull()}) )
mydata <<- rbind(mydata, newrow)
}
mydata
}, ignoreNULL = FALSE)
}
# Run the application
shinyApp(ui = ui, server = server)
答案 0 :(得分:1)
我认为您的代码中存在一些错误。但是,您是否正在寻找类似的东西?我认为的问题之一是您分配给产品的编号。无法正确过滤。
ui <- fluidPage(
wellPanel(
flowLayout(
selectInput("select", "Product",
choices = list("Cars",
"Trucks",
"Buses"), selected = "Cars"),
dateInput("sdate", "Start date"),
dateInput("edate", "End date"),
numericInput("onshore","On-Shore Cost",1),
numericInput("offshore","Off-Shore Cost",1),
actionButton("action","Blast Off")),
downloadButton("downloadData", "Download")),
dataTableOutput("table")
)
server <- function(input, output) {
#observeEvent(input$action,{
output$table <- DT::renderDataTable( df())
# })
df <- eventReactive(input$action,{
# if(input$select!="" && !is.null(input$onshore) && input$action>0){
newrow = data.frame(Product = input$select,
Start_date = as.character(input$sdate),
End_date = as.character(input$edate),
On_shore_cost =input$onshore,
Off_shore_cost = input$offshore)
#########This is the trouble spot, I am trying to generate this field
Cost = data %>% dplyr::filter(as.factor(product) %in% c(paste(as.factor(input$select)))) %>%
mutate(pc_cost= ifelse(as.numeric(offshore) == 0, as.numeric(input$onshore), as.numeric(input$offshore))) %>%
summarise(total = sum(pc_cost)) %>% pull()
mydata <<- cbind(Cost, newrow)
mydata
})
}
# Run the application
shinyApp(ui = ui, server = server)