用于计算的 R 中的 if else 语句
Posted
技术标签:
【中文标题】用于计算的 R 中的 if else 语句【英文标题】:If else statement in R shiny for calculation 【发布时间】:2020-05-17 03:48:27 【问题描述】:我已经开始练习 R Shiny,请在下面我尝试进行计算的部分代码中找到。由于我无法生成,请协助获取输出。
用户界面
图书馆(闪亮) 库(闪亮的仪表板)
shinyUI(fluidPage(
titlePanel("基于零的成本计算模型"),
侧边栏布局(
sidebarPanel(("输入客户详细信息"),
conditionalPanel(condition = "input.tabselect == 1",
textInput("client_name", "Enter the Client Name", value = " "),
selectInput("route_IO", "Enter the route whether it is Inbound or Outound",
choices = c("Inbound","Outbound"), selected = NULL, multiple = FALSE),
textInput("route_start", "Enter the route start point", value = ""),
textInput("route_end", "Enter the route end point ", value = ""),
radioButtons("GST_Mechanism", "Enter the mechanism of GST",
choices = c("RCM","FCM"), selected = NULL ),style = "color:green;font-size: 20px"),
conditionalPanel(condition = "input.tabselect==2",
textInput("model","Enter the Model of the Vehicle",value = " "),
numericInput("n","Enter the distance between start location and end location in KMs", value = 1, min = 1),
selectInput("Road type", "Enter the Road or Terrain Type",
choices = c("Highway","Non-Highway","Hilly","Non Tar","Type B","Type C"),
selected = NULL, multiple = FALSE),
radioButtons("vehicle_type", "Enter the vehicle type",
choices = c("Market","Dedicated"), selected = NULL ),
numericInput("fuel price","Enter the Fuel Price, Inr/Litre", value = 1, min = 1),
numericInput("fuel eff with load","Enter the Fuel Price with load, Inr/Litre", value = 1, min = 1),
numericInput("fuel eff without load","Enter the Fuel Price with load, Inr/Litre", value = 1, min = 1),
numericInput("trip efficiency","Enter the trip efficiency in percentage",
value = 1, min = 1, max = 100, step = 0.5),
numericInput("run", "Enter run", 1),
sliderInput("profir_margin", "Enter the profit margin for transporter",
min = 0, max = 30, step = 0.5, value = " ")),
conditionalPanel(condition = "input.tabselect == 3",
numericInput("driver","Enter the number of Drivers Involved for the trip",1),
numericInput("Driver_sal","Enter the salary of all the Drivers Involved for the trip",1),
numericInput("cleaner","Enter the number of Cleaners Involved for the trip",0),
numericInput("Cleaner_sal","Enter the salary of all the Cleaners Involved for the trip",0),
numericInput("Bhatta","Enter the daily bhatta for driver + cleaner as per 25 day working ",1),
numericInput("Admin","Number of Admin Staff ",1),
numericInput("Admin_route","Number of Routes Handled by Admin Staff ",1),
numericInput("Admin_sal","Enter the salary of the Admin Staff",1),
numericInput("Admin_sal","Enter the salary of the Admin Staff",1),
numericInput("Tar","Enter the cost for Tarpaulin",1),
numericInput("Bank","Enter the bank Guarantee Amount (INR/Month): ",1),
numericInput("Gps","Enter the GPS installation Cost (One Time): ",0),
numericInput("Gps_trip","GPS Trip Charge ",0)),
conditionalPanel(condition = "input.tabselect == 4",
numericInput("Amc","Enter the AMC cost (Inclusive of GST) ",1),
numericInput("Ann_run","Enter annual running of the vehicle",1),
numericInput("warai","Enter the Loading/Unloading/Warai charges",1),
numericInput("Cleaner_sal","Enter the salary of all the Cleaners Involved for the trip",0),
numericInput("Toll","Enter the toll charge of the trip (One Side) ",1),
numericInput("LR","Enter the LR charges ",1),
numericInput("RTO","Enter the RTO Expense ",1),
numericInput("Gunda","Enter the Gunda Tax",1),
numericInput("Over_height","Enter the over height challan cost",1)),
conditionalPanel(condition = "input.tabselect == 5",
numericInput("front tyres","Enter the number of front tyres",2),
numericInput("rear tyres","Enter the number of rear tyres",4),
numericInput("tyre life","Enter the Life of tyre in Km",60000),
numericInput("front tyre cost","Enter the front tyre cost per unit",16000),
numericInput("rear tyre cost","Enter the rear tyre cost per unit",16000),
numericInput("LR","Enter the LR charges ",1),
numericInput("RTO","Enter the RTO Expense ",1),
numericInput("Gunda","Enter the Gunda Tax",1),
numericInput("Over_height","Enter the over height challan cost",1)),
conditionalPanel(condition = "input.tabselect == 6",
numericInput("License","Enter the one time License Cost",1000),
numericInput("Reg_char","Enter the one time registration charger",25000),
numericInput("Nat_permit","Enter the charges for national permit (INR/Yr)",20000),
numericInput("Road_tax","Enter the Road Tax amount (INR/Yr)",16000),
numericInput("Fitness_cert","Enter the charges for fitness certificate",2500),
numericInput("Puc","Enter the charges for PUC ",600),
numericInput("RTO","Enter the RTO Expense ",1),
helpText("Insurance Cost"),
numericInput("IDV","Enter the insured declaration value in percentage",90),
numericInput("Insurance_cost","insurance cost",3)),
conditionalPanel(condition = "input.tabselect == 7",
numericInput("Chasis","Enter the original cost of chasis and cabin",100000),
numericInput("Reg_char","Enter the number of years after chasis and cabin making",0))),
# submitButton("提交前检查详细信息")), #
mainPanel(
tabsetPanel(type = "pills",
tabPanel("Client & Route Details", value =1,
textOutput("Myclient_name"), textOutput("Myroute_IO"),
textOutput("Myroute_start"), textOutput("Myroute_end"), textOutput("MyGST"),
style = "color:black;font-size: 20px;line-height: 2.92857143"),
tabPanel("Trip Detail",value =2, textOutput("dist"),textOutput("Mymodel")),
tabPanel("Fixed Operating Cost",value =3),
tabPanel("Maintenance Cost",value =4),
tabPanel("Variable Cost",value =5),
tabPanel("Regulatory and Insurance Cost",value =6),
tabPanel("Body Chasis", value = 7, textOutput("MyChasis"), textOutput("MyReg_char1")),
id = "tabselect"
))
)))
**Server**
图书馆(闪亮) 库(闪亮的仪表板)
shinyServer(函数(输入、输出)
output$Myclient_name <- renderText(
paste("Please Enter Client's Name:", input$client_name)
)
output$Myroute_IO <- renderText(
paste("Route Selected Is As : ", input$route_IO)
)
output$Myroute_start <- renderText(
paste("Start point of the client entered: ", input$route_start)
)
output$Myroute_end <- renderText(
paste("End point of the client entered: ", input$route_end)
)
output$Myvehicle_type <- renderText(
paste(" Vehicle used is Market or Dedicated: ",input$vehicle_type)
)
output$Myprofir_margin <- renderText(
paste("Profit Margin for trip to be given to transporter: ",input$profir_margin)
)
output$MyGST <- renderText(
paste("GST mechanism selected by the client is: ",input$GST_Mechanism)
)
output$dist <- renderText(
paste("The Distance Entered is : ", input$n)
)
output$Mymodel <- renderText(
paste("The vehicle model Entered is : ", input$model)
)
output$MyChasis <- renderText(
paste("Original Cost of the Chasis is :", input$Chasis)
)
output$MyReg_char1 <- renderText(
if (as.numeric(input$Reg_char == 1) )
paste("The current cost of the vehicle with respect to number of years: ",
as.numeric(input$Chasis * 0.9500))
else
paste("Not in Condition")
)
【问题讨论】:
Reg_char
在ui
中有两个输入 - 也许您想使用不同的名称。此外,如果您可以尝试在您的问题中再次替换您的代码以清楚起见,这将有所帮助。
谢谢本。是这个问题,感谢您的帮助。
【参考方案1】:
请参阅下面的工作模型。我建议你看一些关于制作闪亮仪表板的教程 (https://shiny.rstudio.com/reference/shiny/latest/fluidPage.html),因为你上面的代码在错误的地方有参数,你错过了实际调用仪表板的东西...shinyApp()
library(shiny)
library(shinydashboard)
ui <- fluidPage(
titlePanel("Zero Based Costing Model"),
sidebarLayout(
sidebarPanel(
"Enter client details",
numericInput("Chasis","Enter the original cost of chasis and cabin",100000),
numericInput("Reg_char","Enter the number of years after chasis and cabin making",0)
),
mainPanel(
tabsetPanel(type = "pills",
tabPanel("Body Chasis", value = 7, textOutput("MyChasis"), textOutput("MyReg_char")),
id = "tabselect"
)
)
)
)
server <- function(input, output)
output$MyChasis <- renderText(
paste("Original Cost of the Chasis is :", input$Chasis)
)
output$MyReg_char <- renderText(
if (as.numeric(input$Reg_char == 1) )
paste("The current cost of the vehicle with respect to number of years: ", as.numeric(input$Chasis * 0.9500))
else
paste("Not in Condition")
)
shinyApp(ui, server)
【讨论】:
嗨@Kevin 我已经更新了代码,我只分享了有问题的部分以供快速参考,但我仍然无法在“body chasis”选项卡中获得所需的输出,请您查看完整的代码并帮助我。 请重新格式化您的问题,以便所有代码都包含在代码块中以上是关于用于计算的 R 中的 if else 语句的主要内容,如果未能解决你的问题,请参考以下文章
If_else 语句基于多个列,创建一个基于 R 中的真实语句数量平均的分数
R语言嵌套的ifelse语距:将一条If语句放在另一条If语句中,该语句作为嵌套的If else调用。If else语句允许我们根据表达式结果(TRUE或FALSE)打印不同的语句,执行不同的语句块