R Shiny基本问题

时间:2018-08-29 01:10:59

标签: r shiny

我收到两个我不完全理解的警告:

Warning in if (file == "") { :
  the condition has length > 1 and only the first element will be used
Warning: Error in file: invalid 'description' argument
  166: file
  165: parse
  163: renderPlot [...app.R#51]
  161: func
  121: drawPlot
  107: <reactive:plotObj>
   91: drawReactive
   78: origRenderFunc
   77: output$Minimum_Rates
    1: runApp

作为R和Shiny的初学者,我不知道自己在做什么错。该程序可能有很多问题,但是如果有人可以帮助我解决这两个当前错误,那就太好了。这是我的代码:

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<-(input$inSelect)
    carrier<-eval(parse(text=carrier))
    category<-sym(input$Category)
    discount<-input$discount
    string_name=deparse(substitute(carrier))
    fee_name=eval(parse(paste0(substr(string_name,1,nchar(string_name)-6),"_fees")))
    carrier<-discountRatesAndFindNewPrems(carrier,discount/100,fee_name)
    tMinTable<-removeTheseRatesAndFindMinimumRates(t_list_rates)
    if(as_string(input$ProductType)=="t"){
      carrier %<%
        createRatesComparedToTMin(carrier) %<%
        original_percent_higher<-Percent_Higher %<%
        findRatesFromPrems(carrier,fee_name) %<%
        original_rates<-`Rates per Thousand` %<%
        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))
    }
    else{
      rMinTable<-removeTheseRatesAndFindMinimumRates(r_list_rates)
      carrier$Percent_Higher<-original_percent_higher
      carrier<-createNewRatesComparedToRMin(carrier)
      carrier%>%
        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)

我还有一个问题是选择的数据类型是什么?它们基本上是我在“ choices参数”中输入的任何类型吗?因此,如果我的选择全部是字符串,那么为什么需要将它们转换为符号?

编辑:我在string_name = deparse(substitute(carrier))行之后添加了print(string_name),看起来print(string_name)正在打印整个df,而我想得到的df的名称是字符串名称。我认为我对“载体”数据类型应该是什么有一些误解。有人知道吗?

0 个答案:

没有答案