R和 Shiny : Pass inputs from sliders to reactive function to compute output

标签 r function output shiny

我有 6 个参数,用户可以更改其值。这是我们的 6 个输入。 我想创建一个输出值,该输出值采用这 6 个输入,并根据函数中的多个相关方程计算我们感兴趣的值。这是我的用户界面中的内容...

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(类型“closure”)无法由“cat”处理 和许多其他错误。我只是不知道如何将 6 个参数的更新用户输入传输到我的函数,并将该函数吐回到 Shiny html 页面的输出区域中。

任何帮助将不胜感激!

谢谢!

最佳答案

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

按照您现在的方式,您正在 renderText() 中创建一个函数,然后尝试让 renderText 显示,而不给出它你的论点。这就是您收到错误消息的原因,因为 renderText 将其第一个参数传递给 cat,而后者不知道如何处理该函数。但是,它可以处理函数的输出

无论如何,以下内容对我有用。我只做了两个 slider ,但您大概可以自己扩展它。

ui.R:

#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"))
)
) 

服务器.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)}
  )
})

关于R和 Shiny : Pass inputs from sliders to reactive function to compute output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16848352/

相关文章:

通过 R 运行 .vbs 脚本,在任务计划程序中安排

r - 转换 R 中输入为 'X Weeks, Y Days, Z hours' 的年龄

c++ - 函数之间的数组指针丢失值(用 swig 编译以在 python3 中使用)

python - 将 bash 命令的输出保存到 if..else 语句中的变量中

apache - Gradle:获取Ant任务输出

python - 将数据从python程序打印到mac上的终端

r - 使用 `data.table` 和 `c()` 进行汇总时可以防止因子强制吗?

r - 两个样本(不等方差)welch t 检验的统计功效和样本量确定?

python - 在 Python 中查找字符串中最长运行的从零开始的索引的函数

c - 如何定义此代码并将其作为函数调用?