2013-05-31 102 views
8

我有6个参数,用户可以更改其值。这是我们的6个输入。 我想创建一个输出值,它接受这6个输入,并根据函数中的许多相关方程计算我们的感兴趣值。以下是我在我的UI ......R和Shiny:将滑块输入传递到反应函数以计算输出

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

以下是我在我的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 = # 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页面的输出区域。

任何帮助将不胜感激!

谢谢!

+0

ReactiveUI是另一回事™ –

+1

为什么是一个注册商标的短语? – hedgedandlevered

回答

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

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

圣灵牛!这完全是作品!非常感谢!!!我一直在这一天工作!非常感谢你!!!!!祝你今天过得愉快! – Chloe

+3

没问题,欢迎来到SO! – alexwhan

相关问题