使用特定数据框列中的唯一值将selectInput设置为闪亮

时间:2018-12-04 08:51:25

标签: r shiny

我有两个单独的r文件,一个用于服务器,一个用于ui(并且需要将它们分开进行分配)。我当前正在创建selectInput()选项,如果我的一个变量具有一个基于列中唯一值的下拉列表(而不是在UI本身中键入每个单独的值),则希望使用它。有问题的列是“等级”,其值为A:G。

关于获得此功能的任何建议?

此外,我还是刚开始在stackoverflow上发帖,所以不能完全确定需要什么代码,所以请告诉我是否需要我没有放的东西(或者我放了太多的东西)。

ui.r

library(shiny)
shinyUI(fluidPage(  
  # Application title
  titlePanel("KNN Model"),


  sidebarLayout(
   sidebarPanel(
    selectInput('loan_amnt', 'Loan Amount', names(iris)),
    br(),
    selectInput('grade', 'Loan Grade', choices=("gradelist")),
    br(),
    selectInput('sub_grade', 'Loan Sub-Grade', names(iris)),
    br()
   ),

   # run the model
   mainPanel(
    plotOutput("KnnModel")
   )
  )
))

我意识到我当前的selectInputs是不正确的(我才刚刚开始学习如何使用Shiny)。

server.r

library(shiny)

shinyServer(function(input, output) {

 output$KnnModel <- renderPlot({

  getdata <- function()
  {
   library(ROracle)
   #CODE REMOVED FOR PRIVACY CONCERNS
 }

 df <- getdata()

library(dplyr)
 bad_indicators <- c("Charged Off",
                    "Default",
                    "Does not meet the credit policy. Status:Charged Off",
                    "Default Receiver", 
                    "Late (16-30 days)",
                    "Late (31-120 days)")

 df$default <- ifelse(df$loan_status %in% bad_indicators, 1,
                           ifelse(df$loan_status=="", NA, 0))

 df$default = as.factor(df$default)
 df$loan_amnt = as.numeric(df$loan_amnt)
 df$int_rate = as.numeric(df$int_rate)
 df$grade = as.factor(df$grade)
 df$sub_grade = as.factor(df$sub_grade)
 df$home_ownership = as.factor(df$home_ownership)
 df$pymnt_plan = as.factor(df$pymnt_plan)
 df$application_type = as.factor(df$application_type)

 df$loan_income <- (df$loan_amnt/df$annual_inc)
 df$loan_income <- ifelse(is.infinite(df$loan_income),yes=0, no = df$loan_income)

 df<- subset(df, select=c("loan_amnt", "int_rate", "grade", "annual_inc", "delinq_2yrs",
                                    "sub_grade", "home_ownership", "default", "loan_income", "pymnt_plan"))
 set.seed(321)
 df2<- sample_n(tbl = df, size = 1000, replace = TRUE)
 df2<- df2[complete.cases(df2), ]



 df3 <- df2[1,]
 df3[1,]$loan_amnt <- input$loan_amnt
 df3[1,]$grade <- input$grade
 df3[1,]$int_rate <- input$int_rate
 df3[1,]$sub_grade <- input$sub_grade
 df3[1,]$home_ownership <- input$home_ownership
 df3[1,]$delinq_2yrs <- input$delinq_2yrs
 df3[1,]$annual_inc <- input$annual_inc
 df3[1,]$loan_income <- df3$loan_amnt/df3$annual_inc
 df3[1,]$pymnt_plan <- input$pymnt_plan

如果有可能在1 Q中执行,还想学习如何为我的'sub_grade'selectInput选项编码,以随为'grade'选择的选项而变化。由于sub_grade具有A:G的选项1:5,例如C3,并与“等级”的值链接。我希望sub_grade的选项显示(仅)D1:D5,以选择“等级”“ D”,依此类推。

0 个答案:

没有答案