R和Shiny:将来自滑块的输入传递给无功功能以计算输出

时间:2013-05-31 01:15:45

标签: r function output shiny

我有6个参数,用户可以更改值。这是我们的6个输入。 我想创建一个输出值,它接受这6个输入,并在函数中给出许多相关方程式来计算我们感兴趣的值。以下是我在UI中的内容......

library(shiny)

# Define UI for slider demo application
shinyUI(pageWithSidebar(

#  Application title
headerPanel("# Header Title Goes Here"),

# Sidebar with sliders that demonstrate various available options
sidebarPanel(
# Simple integer interval
sliderInput("u1", "Name:", 
            min=0, max=10000, value=10000),
#There are 6 slider inputs...

    ),

# Show a table summarizing the values entered
mainPanel(
 tableOutput("values"),

 uiOutput("focal"))
 )
)  

以下是我在服务器中的内容.R ...

library(shiny)

shinyServer(function(input, output) {

# Reactive expression to compose a data frame containing all of the values
sliderValues <- reactive({

# Compose data frame
data.frame(
  Name = # Names of my 6 parameters,

  Value = # inputs based on my 6 values by `input$value`,

  stringsAsFactors=FALSE)
})

f <- renderText({function(r1, r2, r3, d1, d2, u1) #these are my 6 values
{ #List of equations that compute f based on the 6 values
}
})


# Show the values using an HTML table
output$values <- renderTable({
sliderValues()
})

# Show the final calculated value 
output$focal <- renderText({
f
})
})

我一直在......错误:参数1(类型'封闭')无法由'cat'处理 和许多其他错误。我只是不知道如何将6个参数的更新用户输入传递给我的函数,并在Shiny html页面的输出区域中将该函数吐出。

任何帮助将不胜感激!!

谢谢!

1 个答案:

答案 0 :(得分:25)

我认为这里存在一些混淆。首先,在f中定义server.R的地方,我想你只想按照通常的方式定义一个函数。然后,当您执行renderText()时,您可以调用该函数来获取您的值。

你现在拥有它的方式,你在renderText()内创建一个函数,然后你试图让renderText显示,而不给它你的论点。这就是您收到错误消息的原因,因为renderText将其第一个参数传递给cat#ui.R library(shiny) # Define UI for slider demo application shinyUI(pageWithSidebar( # Application title headerPanel("# Header Title Goes Here"), # Sidebar with sliders that demonstrate various available options sidebarPanel( # Simple integer interval sliderInput("u1", "Name:", min=0, max=10000, value=10000), sliderInput("r1", "r1:", min=0, max=10000, value=10000) ), # Show a table summarizing the values entered mainPanel( tableOutput("values"), uiOutput("focal")) ) ) 不知道如何处理该函数。但是,它可以处理函数的输出

无论如何,以下适用于我。我只做了两个滑块,但你可以自己扩展它。

ui.R:

#server.R
library(shiny)

shinyServer(function(input, output) {

  # Reactive expression to compose a data frame containing all of the values
  sliderValues <- reactive({

    # Compose data frame
    data.frame(
      Name = c("u1", "r1"),

        Value = c(input$u1,
                  input$r1),

        stringsAsFactors=FALSE)
  })

  f <- function(u1, r1) {
    u1 + r1
  }


  # Show the values using an HTML table
  output$values <- renderTable({
    sliderValues()
  })

  # Show the final calculated value 
  output$focal <- renderText(
    {f(input$u1, input$r1)}
  )
})

server.R

{{1}}