【发布时间】:2018-08-29 18:18:53
【问题描述】:
我正在编写一个 Shiny 应用程序,该应用程序根据 Carrier 中的输入选择数据帧并对其执行一些功能。这是我的代码:
library(shiny)
library(ggplot2)
library(dplyr)
library(rlang)
library(shinyWidgets)
library(rstudioapi)
ui <- fluidPage(
# Give the page a title
titlePanel("Rate Analysis"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("ProductType","Product Type: (Choose One)",choices=c("t","r")),
selectInput("inSelect","Carrier: (Choose One)",choices=unique(c(t_rates_names,r_rates_names))),
selectInput("Category","Category: (Choose One)",choices=c("Age","Term","Band","Class","Gender")),
sliderInput("discount","% Discount:",min=0,max=100,value=0),
width=3
),
# Create a spot for the barplot
mainPanel(
plotOutput("Minimum_Rates")
)
)
)
server <- function(input, output, session) {
observe({
req(input$ProductType)
x<-sym(input$ProductType)
updateSelectInput(session,"inSelect",label=paste("test"),choices=eval(parse(text=paste0(x,"_rates_names"))))
})
output$Minimum_Rates<-renderPlot({
req(input$inSelect)
req(input$Category)
req(input$discount)
req(input$ProductType)
carrier_name<-(input$inSelect)
carrier<-eval(parse(text=input$inSelect))
type<-input$ProductType
category<-sym(input$Category)
discount<-input$discount
fee_name=eval(parse(text=paste0(substr(carrier_name,1,nchar(carrier_name)-6),"_fees"))) #gets actual list of fees
if(type=="t"){
tMinTable<-removeCarrierRatesAndFindMinimumRates(t_list_rates)
carrier%>%
createRatesComparedToTMin(carrier)%>%
original_percent_higher<-carrier$Percent_Higher%>%
findRatesFromPrems(carrier,fee_name)%>%
original_rates<-carrier$`Rates per Thousand`%>%
discountRatesAndFindNewPrems(carrier,discount/100,fee_name)%>%
group_by(!!!category) %>%
summarise(Current_Comparison=sum(original_percent_higher)) %>%
ggplot(aes_string(input$category,"Current_Comparison"))+geom_bar(stat="identity",position="dodge") + geom_text(aes(label=scales::percent(Current_Comparison)),size=3.2,vjust=-0.5,position=position_dodge(width=1))
}
else{
rMinTable<-removeCarrierRatesAndFindMinimumRates(r_list_rates)
carrier%>%
createRatesComparedToRMin(carrier)%>%
original_percent_higher<-carrier$Percent_Higher%>%
findRatesFromPrems(carrier,fee_name)%>%
original_rates<-carrier$`Rates per Thousand`%>%
discountRatesAndFindNewPrems(carrier,discount/100,fee_name)%>%
group_by(!!!category) %>%
summarise(Current_Comparison=sum(original_percent_higher),Hypothetical_Comparison=sum(New_Percent_Higher)) %>%
ggplot(aes_string(input$category,"Current_Comparison"))+geom_bar(stat="identity",position="dodge") + geom_text(aes(label=scales::percent(Current_Comparison)),size=3.2,vjust=-0.5,position=position_dodge(width=1))
}
})
}
shinyApp(ui=ui,server=server)
错误是:
Warning: Error in discountRatesAndFindNewPrems: unused argument (fee_name)
170: function_list[[i]]
169: freduce
168: _fseq
167: eval
166: eval
164: %>%
163: renderPlot [.../app.R#54]
161: func
121: drawPlot
107: <reactive:plotObj>
91: drawReactive
78: origRenderFunc
77: output$Minimum_Rates
1: runApp
不幸的是,出于法律原因,我无法共享数据或功能,但我已经测试了闪亮之外的功能,效果很好。该错误表明我没有在 discountRatesAndFindNewPrems 函数中使用参数,但警告指向第 54 行,即运营商%>% 行。我是 dplyr 的新手并且一般来说是闪亮的,无法理解“%>%”运算符是否只能与 dplyr 函数一起使用,因为我编写了另一个使用 dplyr 和“%>%”运算符的闪亮应用程序,但我只在该应用程序中使用“%>%”的 dplyr 函数。我还缺少什么其他东西,例如使 Carrier 成为响应式或其他什么?
【问题讨论】:
-
看看这些问题。他们更详细地解释了
%>%管道的工作原理以及如何确保在链接函数时将数据放在正确的位置:stackoverflow.com/questions/49402641/…、stackoverflow.com/questions/42385010/… -
您的问题可能是因为
discountRatesAndFindNewPrems接受3 个参数:管道进入第一个,carrier进入第二个,discount/100进入第三个。fee_name,那么,无处可去。