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

Chl*_*loe 8 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"))
 )
)  
Run Code Online (Sandbox Code Playgroud)

这是我在服务器中的内容.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
})
})
Run Code Online (Sandbox Code Playgroud)

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

任何帮助将不胜感激!!

谢谢!

ale*_*han 25

我认为这里有一些混乱.首先,在您定义f的位置server.R,我认为您只想以通常的方式定义函数.然后,当你这样做时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"))
)
) 
Run Code Online (Sandbox Code Playgroud)

server.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)}
  )
})
Run Code Online (Sandbox Code Playgroud)

  • 天啊!!这完全工作!! 非常感谢!!!我这一天都工作了!非常感谢你!!!!! 祝你今天过得愉快! (2认同)