【问题标题】:R and Shiny: Pass inputs from sliders to reactive function to compute outputR 和 Shiny:将输入从滑块传递到反应函数以计算输出
【发布时间】:2013-05-26 17:56:43
【问题描述】:

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

任何帮助将不胜感激!!

谢谢!

【问题讨论】:

  • ReactiveUI is a different Thing™
  • 为什么这是一个商标短语?

标签: r function output shiny


【解决方案1】:

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

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

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

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)}
  )
})

【讨论】:

  • 圣牛!!这完全有效!太感谢了!!!我整天都在工作!!!非常感谢你!!!!祝你有美好的一天!
猜你喜欢
  • 2017-08-31
  • 2021-05-26
  • 1970-01-01
  • 2016-11-06
  • 2020-05-15
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 2015-10-16
相关资源
最近更新 更多