我在这些代码中的输出仅在一种情况下显示。该图仅在radioButtons ==“ days”时显示,其他选择不再显示。
这两个条件仅更改X变量,并且当我仅运行ggplot时,两个绘图代码都可以工作。 我对这个问题没有任何线索。
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)
答案 0 :(得分:1)
问题在于ggplot对象必须是renderPlot
中的返回值。
如果input$radio == "days"
,一切顺利。但是如果input$radio == "dayFare"
,if(input$radio == "days"){ ggplot(....) }}
是NULL
,并且那个是renderPlot
的返回值。
简单修复:将if(input$radio == "days")
替换为else if(input$radio == "days")
。
或更明确地返回什么,例如:
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))
)
}}
)
或:
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
}
)
答案 1 :(得分:1)
您实际上并没有返回在output$distPlot
中的情节。顺便说一下,您可以简化下面的代码:
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)