我最近开始使用R shiny并且有一个关于在被动环境中使用if语句的问题。我读了其他帖子,但没找到我需要的东西。
假设有三个电话计划,A,B和C.每个都有固定成本和每分钟成本。根据使用情况和计划,我们想告诉用户总费用。因此,在UI中我们有:
selectInput("plan", "Choose phone plan", choices = c("A", "B", "C"))
我的问题是在说出A被选中后,我想定义变量固定成本为A = 10美元,每分钟成本= 1美元。类似于B,C。然后我可以显示总成本。
我无法弄清楚在选择计划后如何分配固定成本和可变成本。为简单起见,我不想提示用户每个计划的固定成本和可变成本。什么应该在服务器文件中的建议?
我试着这样做:
if(input$choices == "A"){fixcost = 10, mincost = 1}
output$cost = renderText(fixcost+mincost*input$use)
答案 0 :(得分:0)
您可以创建字典 喜欢
dict=data.frame(type=c("A","B","C"),fixcost =c(10,11,12),mincost=c(1,2,3))
ui=shinyUI(
fluidPage(
selectInput("plan", "Choose phone plan", choices = c("A", "B", "C"))
,
numericInput("use","use",0),
textOutput("txt"),
textOutput("cost")
)
)
server=shinyServer(function(input, output,session) {
output$txt=renderPrint({
paste("fixcost =",dict$fixcost[dict$type==input$plan] ," mincost =",dict$mincost[dict$type==input$plan])
})
output$cost = renderText(dict$fixcost[dict$type==input$plan]+dict$mincost[dict$type==input$plan]*input$use)
})
shinyApp(ui,server)