我最近开始构建闪亮的应用程序,但我卡住了。请帮助我。提前谢谢你
我正在尝试创建一个条形图来显示不同类型现金和不同期限的计数。这部分,代码进展顺利。
我还想创建箱形图以显示用户选择的不同变量的数字摘要。我创建了一个名为"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)
})
})
这是用户界面。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()
更改为metric1
。如:
p <- ggplot(dataset(),aes(factor(Incentive.Type), metric1))+geom_boxplot()
实际上,我认为您将不得不使用以下内容:
p <- ggplot(dataset(),aes_string(factor("Incentive.Type"), "metric1"))+geom_boxplot()
为了让它看到变量的值,metric1
并正确解释 ggplot 中字符串变量的使用。