如何将输入从app.R传递到其他脚本并返回对象?

时间:2019-03-24 07:33:24

标签: r shiny shiny-reactivity

我有两个文件app.RLoanHealthv2.R

app.R具有以下代码。我将noc作为输入名称,我需要将其作为输入传递给LoanHealthv2.R文件并计算结果,以便可以在此应用程序中使用LoanHealthv2.R文件的结果。

又如何使它具有反应性,以便每次选择不同的noc时都会产生新结果?

########################## Ui function ################################

ui=fluidPage(

  titlePanel("Loan Health"),

  fluidRow(
    selectInput("noc","Name of Customer", customers[,1], selected = NULL, multiple = FALSE,
                selectize = TRUE, width = NULL, size = NULL)
  ),


  mainPanel(
    plotOutput("Plot", width = "100%", height = "400px", click = NULL,
               dblclick = NULL, hover = NULL, hoverDelay = NULL,
               hoverDelayType = NULL, brush = NULL, clickId = NULL,
               hoverId = NULL, inline = FALSE)
  )
)

########################## server function ################################

server <- function(input, output) {

  output$Plot<-renderPlot({

    customer<-input$noc #Name of customer

    plot(SalesClientData$Date,SalesClientData$DPD,type="l")

  })
}

shinyApp(ui, server)

LoanHealthv2.R看起来像这样

customer = "A1"   #<-I want this to be equal to input `noc` from app.R and compute the result below and make it available so that I can plot output.

account<-customers[which(customers[,1]==customer), 2] 
start<- customers[which(customers[,1]==customer), 3]

SalesClientData = subset(POSData,POSData$Loan_Account_Number==account)

1 个答案:

答案 0 :(得分:1)

我不确定我是否完全了解您要做什么,但是您可以在应用程序中结合使用reactiveValuessource

########################## Ui function ################################

ui=fluidPage(

  titlePanel("Loan Health"),

  fluidRow(
    selectInput("noc","Name of Customer", customers[,1], selected = NULL, multiple = FALSE,
                selectize = TRUE, width = NULL, size = NULL)
  ),


  mainPanel(
    plotOutput("Plot", width = "100%", height = "400px", click = NULL,
               dblclick = NULL, hover = NULL, hoverDelay = NULL,
               hoverDelayType = NULL, brush = NULL, clickId = NULL,
               hoverId = NULL, inline = FALSE)
  )
)

########################## server function ################################

server <- function(input, output) {
  rv <- reactiveValues()
  output$Plot<-renderPlot({

    customer<-input$noc #Name of customer
    rv$customer <- customer
    plot(SalesClientData$Date,SalesClientData$DPD,type="l")

  })
  source("LoanHealthv2.R", local = TRUE)
}

shinyApp(ui, server)

然后在您的LoanHealthv2.R文件中,进行适当的更改:

#Assuming you want to output this as a table using the `DT` package

output$CustomerDF <- renderDT ({
req(rv$customer)
customer = rv$customer   

account<-customers[which(customers[,1]==customer), 2] 
start<- customers[which(customers[,1]==customer), 3]

SalesClientData = subset(POSData,POSData$Loan_Account_Number==account)

datatable(SalesClientData)

})