My output in these code only shows in one condition. The plot only shows when radioButtons == "days", another choice doesn't show any more.
The two condition just change the X variable, and both plot codes work when I only run ggplot.
I don't have any clue on the problem.
library(shiny)
library(ggplot2)
feeInMonth <- function(dayFare, days){
fee = dayFare * days
if(fee > 662.5){ #662.5 = 100 + 50/0.8 + 250/0.5
fee = (fee -262.5)} else if(fee > 162.5 & fee <= 662.5){ #162.5 = 100 + 50/0.8
fee = fee/2+68.75 } else if(fee > 100 & fee <= 162.5){#(fee-162.5)/2+150
fee = fee*0.8+20 } else { return(fee)} #(fee-100)*0.8+100
return(fee)
}
g <- Vectorize(feeInMonth)
ui <- fluidPage(
titlePanel(HTML("北京地铁月度支出计算器 <br/>Beijing Subway monthly Fare Calculator")),
fluidRow(
column(4,radioButtons("radio", label = h4(HTML("X轴选择 <br/> Select X Variable")),
choices = c("以天数看花费" = "dayFare", "以单日费用看花费" = "days"),
selected = "days")),
column(5,uiOutput("Input"))),
# Show a plot of the generated distribution
plotOutput("distPlot", width=890,height = 400)
)
server <- function(input, output) {
output$Input <- renderUI({
if(input$radio == "days"){
numericInput("Input", label = h4(HTML('每月使用日数<br/> monthly work days')),
value = 22, min = 1, max = 31)
}else{
numericInput("Input", label = h4(HTML('平均每日花费<br/> general each work day fare')),
value = 10, min = 3, max = 50)
}})
output$distPlot <- renderPlot({
req(!is.null( input$Input))
argList <- input$Input
names(argList) <- input$radio
ggplot(data.frame(days = c(0,31), dayFare = c(3,50)),
aes_string(x = ifelse(input$radio == "dayFare", "days", "dayFare"))) +
stat_function(fun = g, args = argList)
})
shinyApp(ui = ui, server = server)
The problem is that the ggplot object must be the return value in renderPlot
.
If input$radio == "days"
, all goes well. But if input$radio == "dayFare"
, if(input$radio == "days"){ ggplot(....) }}
is NULL
, and that is the return value of renderPlot
.
Simple fix: replace if(input$radio == "days")
with else if(input$radio == "days")
.
Or be more explicit about what to return, e.g.:
output$distPlot <- renderPlot(
{
if(input$radio == "dayFare"){
return(
ggplot(data.frame(dayFare = c(3,50),days = c(0,31)), aes(x = days)) +
stat_function(fun = g,args = list(dayFare = input$Input))
)
}
if(input$radio == "days"){
return(
ggplot(data.frame(dayFare = c(3,50),days = c(0,31)), aes(x = dayFare)) +
stat_function(fun = g,args = list(days = input$Input))
)
}}
)
or:
output$distPlot <- renderPlot(
{
if(input$radio == "dayFare"){
my_plot <- ggplot(data.frame(dayFare = c(3,50),days = c(0,31)), aes(x = days)) +
stat_function(fun = g,args = list(dayFare = input$Input))
}
if(input$radio == "days"){
my_plot <- ggplot(data.frame(dayFare = c(3,50),days = c(0,31)), aes(x = dayFare)) +
stat_function(fun = g,args = list(days = input$Input))
}
my_plot
}
)
You did not actually return a plot in output$distPlot
. By the way, you can simplify the code, which I did below:
library(shiny)
library(ggplot2)
feeInMonth <- function(dayFare, days){
fee = dayFare * days
if(fee > 662.5){ #662.5 = 100 + 50/0.8 + 250/0.5
fee = (fee -262.5)} else if(fee > 162.5 & fee <= 662.5){ #162.5 = 100 + 50/0.8
fee = fee/2+68.75 } else if(fee > 100 & fee <= 162.5){#(fee-162.5)/2+150
fee = fee*0.8+20 } else { return(fee)} #(fee-100)*0.8+100
return(fee)
}
g <- Vectorize(feeInMonth)
ui <- fluidPage(
titlePanel(HTML("北京地铁月度支出计算器 <br/>Beijing Subway monthly Fare Calculator")),
fluidRow(
column(4,radioButtons("radio", label = h4(HTML("X轴选择 <br/> Select X Variable")),
choices = c("以天数看花费" = "dayFare",
"以单日费用看花费" = "days"),
selected = "days")),
column(5,uiOutput("Input"))),
# Show a plot of the generated distribution
plotOutput("distPlot", width=890,height = 400)
)
server <- function(input, output) {
output$Input <- renderUI({
numericInput("Input", label = h4(HTML(ifelse(input$radio == "days", '每月使用日数<br/> monthly work days',
'平均每日花费<br/> general each work day fare'))),
value = 22, min = 1, max = 31)
})
output$distPlot <- renderPlot({
ggplot(data.frame(days = c(0,31), dayFare = c(3,50)),
aes_string(x = ifelse(input$radio == "dayFare", "days", "dayFare"))) +
stat_function(fun = g,args = input$Input)
})
}
shinyApp(ui = ui, server = server)