闪亮的单选按钮条件选择中无ggplot输出 [英] No ggplot output in shiny radioButtons condition choices

查看:40
本文介绍了闪亮的单选按钮条件选择中无ggplot输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这些代码中的输出仅在一种情况下显示.该图仅在radioButtons =="days"时显示,其他选择不再显示.

My output in these code only shows in one condition. The plot only shows when radioButtons == "days", another choice doesn't show any more.

这两个条件只是更改了X变量,而当我仅运行ggplot时,两个绘图代码都可以工作.我没有这个问题的线索.

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)

推荐答案

您实际上没有返回 output $ distPlot 中的图.顺便说一下,您可以简化下面的代码:

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)

这篇关于闪亮的单选按钮条件选择中无ggplot输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆