[英]R and Shiny: Pass inputs from sliders to reactive function to compute output
我有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頁面的輸出區域中將該函數吐出。
任何幫助將不勝感激!!
謝謝!
我認為這里有一些混亂。 首先,在server.R
定義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)}
)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.