我是 Shiny 的(以及任何网络应用程序的东西)新手,但相当精通 R。我正在尝试构建一个相当基本的页面,它在加载页面之前运行 API 调用,根据响应获取一些输入,然后运行另一个 API 调用并进行一些分析。我在输入时遇到问题。
这是我的用户界面:
shinyUI(fluidPage(
# Application title
titlePanel("IGP Risk Analysis"),
sidebarLayout(
sidebarPanel(
selectInput("portfolio", "Underlying Portfolio:",
choices = portfolioList),
selectInput("portDate", "Portfolio Date:",
choices = "Pick a portfolio..."),
width = 2),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
)
))
我的服务器代码如下:
shinyServer(function(input, output, session) {
portfolioInput <- reactive({
temp <- setnames(sendRequest(theURL, myUN, myPW, action = "GetPortfolios"), "Available Portfolios")
portfolioList <- temp[!grepl("AAA|ZZZ",unlist(temp)),]
return(portfolioList)
})
observe({
portfolioDates <- setnames(sendRequest(theURL, myUN, myPW, action = "GetPortfolioDates",
portfolioName = input$portfolio, portfolioCurrency = "USD"),
"Available Dates")
updateSelectInput(session, "portDate",
choices = c("Pick One", portfolioDates),
selected = "Pick One")
})
})
它正在工作,没有错误或警告,但第一个输入框正在显示 sendRequest() 的结果。它不是设置名称,也不是对响应进行子集化。 IE。 - 在第一个 selectInput 框中我得到:
theResponse.ArrayOfString.string
AAA - IGP\\Diver\\20151007
AAA - IGP\\Diver\\TEST
REAL
BD
Bvdh
Cap
Cas
Diver
IGP Aggregate
ZZZ - Archive
ZZZ - Archive\\AAA - IGP
我想要的地方:
Available Portfolios
REAL
BD
Bvdh
Cap
Cas
Diver
IGP Aggregate
这对我来说没有意义,因为它似乎忽略了代码。
由于portfolioList是静态的,因此只需要在第一次加载页面时加载一次,我尝试在服务器功能之外获取列表。我想这会设置一个全局变量,然后我可以在用户界面中引用。这不起作用。有什么想法为什么这种方法行不通?
这与服务器功能中的“ session ”有什么关系吗?我是否正在运行旧 session 或其他内容? “ session ”是 R session 吗?当我在 RStudio 中重新启动应用程序时,它会重新启动吗?
最佳答案
给你一些开始的东西,renderUI 的最小示例:
shinyApp(
ui = sidebarLayout(
sidebarPanel(
uiOutput("portfolio"),
selectInput("portDate", "Portfolio Date:",
choices = "Pick a portfolio..."),
width = 2),
mainPanel()),
server = function(input, output) {
ui1 <- reactive({
temp <- c("AAA","1","2","3","ZZZ")
temp[!grepl("AAA|ZZZ",temp)]
})
output$portfolio <- renderUI ({
selectInput("portfolio", "Underlying Portfolio:",
choices = ui1())
})
}
)
要添加我的评论,您不能简单地调用 ui.r 中的函数或对象,您可以在 server.r 中渲染对象并调用这些对象,在 ui.r 中标记为 output$name 。 我建议你做 Shiny 的教程http://shiny.rstudio.com/tutorial/ .
关于r - Shiny 的服务器返回错误的函数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015814/