我正在编写一个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
不幸的是,出于法律原因,我无法共享数据或功能,但是我已经测试了Shiny之外的功能,这些功能正常工作。该错误表明我没有在DiscountRatesAndFindNewPrems函数中使用参数,但是警告指向第54行,即载波%>%行。我是dplyr和Shiny的新手,因此无法理解“%>%”运算符是否只能与dplyr函数一起使用,因为我编写了另一个使用dplyr和“%>%”运算符的闪亮应用程序,但我只在该应用中使用了“%>%”的dplyr函数。还有其他我想念的东西,例如使Carrier具有反应性吗?
答案 0 :(得分:1)
看看这些问题。他们更详细地说明了%>%管道如何工作以及如何在链接函数Using `%>%` with `lm` and `rbind`和Using the %>% pipe, and dot (.) notation
时确保将数据放置在正确的位置使用%>%
管道时,管道左侧的输出将发送到右侧的第一个参数(或发送到.
指定的参数)运算符)。如果我不得不猜测的话,那是因为discountRatesAndFindNewPrems
接受了3个参数,而您正在传递4个参数。
carrier
进入第二名,discount/100
进入第三名。fee_name
无处可去,并且出现了unused argument
错误。我认为将管道与非dplyr函数一起使用的最佳策略是始终使用.
运算符指定管道的输出位置。在几乎所有情况下,只需将.
用作函数中的参数,管道就会将左侧函数的输出发送到该参数。
例如,此代码:
carrier %>%
createRatesComparedToRMin(carrier)
实际上是在做同样的事情:
createRatesComparedToRMin(carrier, carrier)
我想这不是您想要的。如果使用点.
,会更清楚发生什么情况:
carrier %>%
createRatesComparedToRMin(.)
我发现您的函数还有很多其他奇怪的问题,可能会引起问题:
1)您无法通过管道传递给赋值运算符来保存中间值(请参见此问题:R pipe (%>%) capabilities - storage and partial use?)
...
createRatesComparedToRMin(carrier) %>%
original_percent_higher <- carrier$Percent_Higher %>%
...
本节将给出错误,如我们在本示例中看到的:
mtcars %>%
dog <- sum() %>%
sum(volcano)
Error in mtcars %>% dog <- sum() %>% sum(volcano) :
could not find function "%>%<-"
如果要保存中间值,则需要停止管道并分配如下结果:
dog <- mtcars %>%
sum()
sum(dog, volcano)
[1] 704849.2
或者,您可以使用自定义函数来分配所链接问题中讨论的结果。