R 应用一个函数,该函数接受值并使用

R 应用一个函数,该函数接受值并使用,r,shiny,R,Shiny,我正在尝试开发一个应用程序,它要求用户输入一些值,将这些值传递给一个函数,并将结果输出到表中 我的R代码如下所示: someFunction <- function(S, K, type){ # call option if(type=="C"){ d1 <- S/K value <- S*pnorm(d1) - K*pnorm(d1) return(value)} # put option if(type

我正在尝试开发一个应用程序,它要求用户输入一些值,将这些值传递给一个函数,并将结果输出到表中

我的R代码如下所示:

someFunction <- function(S, K, type){
  
  # call option
  if(type=="C"){
    d1 <- S/K
    value <- S*pnorm(d1) - K*pnorm(d1)
    return(value)}
  
  # put option
  if(type=="P"){
    d1 <- S*K
    value <-  (K*pnorm(d1) - S*pnorm(d1))
    return(value)}
}


SInput <- 20
KInput <- 25
Seq <- seq(from = KInput - 1, to = KInput + 1, by = 0.25)

C <- someFunction(
  S = SInput,
  K = Seq,                                                  
  type = "C"
)

P <- someFunction(
  S = SInput,
  K = Seq,
  type = "P"
)

cbind(C, P)
我想使用Shining将其输出为一个表。我目前拥有的是:

library(shiny)
library(shinydashboard)

#######################################################################
############################### Functions #############################

someFunction <- function(S, K, type){
    
    # call option
    if(type=="C"){
        d1 <- S/K
        value <- S*pnorm(d1) - K*pnorm(d1)
        return(value)}
    
    # put option
    if(type=="P"){
        d1 <- S*K
        value <-  (K*pnorm(d1) - S*pnorm(d1))
        return(value)}
}


############################### Header ###############################
header <- dashboardHeader()

#######################################################################
############################### Sidebar ###############################
sidebar <- dashboardSidebar()

#######################################################################
############################### Body ##################################

body <- dashboardBody(
    fluidPage(
        numericInput("SInput", "Input S:", 10, min = 1, max = 100),
        numericInput("KInput", "Input K:", 10, min = 1, max = 100),
        verbatimTextOutput("S_K_Output")
    )
)

#######################################################################

ui <- dashboardPage(header, sidebar, body)

#######################################################################

server <- function(input, output) {
    
    output$S_K_Output <- observeEvent(
        
        input$Seq <- seq(from = input$KInput - 1, to = input$KInput + 1, by = 0.25),    # create a sequence going from K-1 to K+1

        input$C <- someFunction(
            S = input$SInput,
            K = input$Seq,                                                    # Apply this sequence to the function
            type = "C"
        ),
        
        input$P <- someFunction(
            S = input$SInput,
            K = input$Seq,
            type = "P"
        ),
        
        cbind(input$C, input$P)                                               # Extract the results and put side-by-side 
    )
}
我得到以下错误:

.getReactiveEnvironment$currentContext中出错:操作未成功 在没有主动-被动上下文的情况下允许。你想做点什么 这只能从反应式表达式或观察者内部完成

我相信这是因为我试图通过observeEvent传递数据

我的问题是,如何允许用户输入值、应用函数并在表格中显示结果?

几个问题:

observeEvent是通过副作用工作的,即不要试图做任何有返回值的事情;你需要的是renderText。 您调用observeEvent的方式是每个参数一个表达式,而不是它的工作方式:第一个参数应该是要监视/响应的反应组件的表达式,第二个是发生某些事情时要执行的单个表达式。当它是复合词时,则必须在其中一个或两者中使用{…}。您称之为的方式有以下几个问题:

observeEvent是通过副作用工作的,即不要试图做任何有返回值的事情;你需要的是renderText。
您调用observeEvent的方式是每个参数一个表达式,而不是它的工作方式:第一个参数应该是要监视/响应的反应组件的表达式,第二个是发生某些事情时要执行的单个表达式。当它是复合词时,则必须在其中一个或两者中使用{…}。您称之为Seq observeEvent的方式并不意味着返回一个值,它主要是在副作用下运行的。也许您需要使用renderText?当我应用renderTable和renderText时,我会收到相同的错误消息。observeEvent并不是要返回一个值,它的操作主要是产生副作用。也许您需要renderText?当我应用renderTable和renderText时,我会收到相同的错误消息。太好了,谢谢!我现在对Shiny有了更多的了解。再次感谢,谢谢!我现在对Shiny有了更多的了解。再次感谢
library(shiny)
library(shinydashboard)

#######################################################################
############################### Functions #############################

someFunction <- function(S, K, type){
    
    # call option
    if(type=="C"){
        d1 <- S/K
        value <- S*pnorm(d1) - K*pnorm(d1)
        return(value)}
    
    # put option
    if(type=="P"){
        d1 <- S*K
        value <-  (K*pnorm(d1) - S*pnorm(d1))
        return(value)}
}


############################### Header ###############################
header <- dashboardHeader()

#######################################################################
############################### Sidebar ###############################
sidebar <- dashboardSidebar()

#######################################################################
############################### Body ##################################

body <- dashboardBody(
    fluidPage(
        numericInput("SInput", "Input S:", 10, min = 1, max = 100),
        numericInput("KInput", "Input K:", 10, min = 1, max = 100),
        verbatimTextOutput("S_K_Output")
    )
)

#######################################################################

ui <- dashboardPage(header, sidebar, body)

#######################################################################

server <- function(input, output) {
    
    output$S_K_Output <- observeEvent(
        
        input$Seq <- seq(from = input$KInput - 1, to = input$KInput + 1, by = 0.25),    # create a sequence going from K-1 to K+1

        input$C <- someFunction(
            S = input$SInput,
            K = input$Seq,                                                    # Apply this sequence to the function
            type = "C"
        ),
        
        input$P <- someFunction(
            S = input$SInput,
            K = input$Seq,
            type = "P"
        ),
        
        cbind(input$C, input$P)                                               # Extract the results and put side-by-side 
    )
}