简体   繁体   中英

Assigning values after input in R Shiny

I recently started using R shiny and have a question about using if statements in reactive environment. I read other posts, but did not exactly find what I needed.

Suppose there are three phone plans, A, B, and C. Each have fixed cost and per minute cost. Given the usage and plan, we want to tell user total cost. So in UI we have:

selectInput("plan", "Choose phone plan", choices = c("A", "B", "C"))

my problem is after say A is choosen, I want to define variables fixed cost for A = 10 dollars, per min cost = 1 dollar. Similar for B, C. Then I can display total cost.

I can't figure out how to assign the fixed and variable cost after plan is choosen. For simplity, I don't want to prompt user for the fixed and variable cost for each plan. Any suggestions on what should go in server file?

I tried to do:

if(input$choices == "A"){fixcost = 10, mincost = 1}
output$cost = renderText(fixcost+mincost*input$use)

You can create dictionary like

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM