【问题标题】:R shiny, cannot set column names with Shiny reactive variable for ggplotR闪亮,无法为ggplot设置带有闪亮反应变量的列名
【发布时间】:2015-04-29 21:05:10
【问题描述】:

我最近开始构建闪亮的应用程序,但我陷入了困境。请帮助我。提前谢谢你

我正在尝试创建一个条形图来显示不同类型现金和不同期限的计数。这部分,代码进行得很顺利。

我还想创建箱线图来显示用户选择的不同变量的数字摘要。我创建了一个名为“metric”的 selectInput,然后在 server.R 中创建了一个名为“metric1”的响应式。然后使用“metric1”作为我选择在 server.R 中创建箱线图的变量。

但是老是说“找不到函数”metric1”,不知道为什么它把“metric1”当成函数?应该是用户选择的变量的向量名。 如果我在 ggplot 中使用 input$metric 直接创建箱线图,它仍然会显示错误:“找不到对象'输入'”。为什么找不到输入?我已经粘贴了下面的代码。这不是一个长代码。请帮助我!

library(shiny)
library(ggplot2)

cash <- read.csv("cash 042014-032015.csv")
cash$TERM <- as.numeric(cash$TERM)

shinyServer(function(input, output) {

  dataset <- reactive({cash[cash$mapped_name %in% (input$model),]})
  metric1 <- reactive({input$metric})

  output$caption <- renderText({
    input$model
  })

  output$countPlot <- renderPlot({    
    p <- ggplot(dataset(), aes(Incentive.Type, fill=factor(Incentive.Type))) + geom_bar()+ facet_grid(~TERM, margins=TRUE)+theme(axis.text.x =     element_blank(),axis.ticks=element_blank(),legend.text =     element_text(size=20))+guides(fill=guide_legend(title="Incentive     Type"),title.theme=element_text(size=30))+scale_x_discrete(limits=c("Standard","Standard+Captive","Standard+Customer","Standard+Captive+Customer","Special","Special+Captive","Special+Customer","Special+Captive+Customer"))
    print(p)    
  })

  output$summaryPlot <- renderPlot({
    p <- ggplot(dataset(),aes(factor(Incentive.Type), metric1()))+geom_boxplot()
    print(p)
  })
})

这里是 ui.R

library(shiny)
library(ggplot2)

dataset <- cash

shinyUI(

  fluidPage(    

# Give the page a title
titlePanel("Incentives by Model"),

# Generate a row with a sidebar
  sidebarPanel(
    checkboxGroupInput("model", "Select Models:", 
                choices=c("370Z","Altima","Armada","Crew","Cube","Frontier","GTR","Juke","Leaf",
                          "Maxima","Murano","NV","Other","Pathfinder","Quest","Rogue","Sentra","Titan","Versa","Xterra"),selected="Altima"),
    selectInput("metric","Please select an option below:", choices=c("Dealer Commission Amount"="DLR_COMM_AMT", "Total Monthly Payment"="TOT_MO_PMT","Original Loan Amount"="ORIG_LN_AMT", "Rate"="RATE"), 
                selected="DLR_COMM_AMT"),
    width=2  
  ),


mainPanel(
  h3(textOutput("caption", container=span)),
  plotOutput("countPlot"),  
  plotOutput("summaryPlot")

) ))

【问题讨论】:

  • 可能是因为您在第二次 ggplot 调用中无意中输入了 metric1() :)
  • 如果对你有帮助请记得采纳答案。

标签: r plot shiny boxplot


【解决方案1】:

尝试在第二个ggplot 调用中将metric1() 更改为metric1。如:

  p <- ggplot(dataset(),aes(factor(Incentive.Type), metric1))+geom_boxplot()

实际上我认为你将不得不使用类似的东西:

  p <- ggplot(dataset(),aes_string(factor("Incentive.Type"), "metric1"))+geom_boxplot()

为了让它查看变量metric1 的值并正确解释ggplot 内部字符串变量的使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2019-02-15
    • 2016-12-20
    • 1970-01-01
    相关资源
    最近更新 更多