闪亮的服务器返回错误的函数值

Shiny server is returning wrong value for function

我不熟悉 shiny(以及任何网络应用程序的东西),但相当精通 R。我正在尝试构建一个相当基本的页面,它在加载页面之前运行 API 调用,需要基于响应的一些输入,然后运行另一个 API 调用并进行一些分析。我在输入时遇到问题。

这是我的 UI:

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 是静态的,首次加载页面时只需要加载一次,因此我尝试在服务器函数之外获取列表。我在想这会设置一个全局变量,然后我可以在 UI 中引用它。这没有用。任何想法为什么这种方法行不通?

这跟服务器函数中的'session'有关系吗?我有旧会话 运行 还是什么? 'session' 是 R 会话吗?当我在 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 中渲染你的对象并调用这些对象,在 [= 中标记为 output$name 16=]。 我会建议你做闪亮的教程 http://shiny.rstudio.com/tutorial/

谢谢大家!!!我想通了,或者我想出了解决问题的方法。非常感谢 Sebastion 引导我朝着正确的方向前进。 (还要感谢 this post。)我只是发布了一个答案来表达对这个问题的敬意。 Sebastion 和其他人的所有道具。

这是工作 ui:

shinyUI(fluidPage(

  # Application title
  titlePanel("IGP Risk Analysis"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("portfolio"),  
      uiOutput("portDate"),
      width = 2),

    mainPanel(
      tabsetPanel(type = "tabs", 
                  tabPanel("Plot", plotOutput("plot")), 
                  tabPanel("Summary", verbatimTextOutput("summary")), 
                  tabPanel("Table", tableOutput("table"))
      )
    )
  )
))

这是服务器:

shinyServer(function(input, output, session) {

  output$portfolio <- renderUI ({
    temp <- setNames(sendRequest(theURL, myUN, myPW, action = "GetPortfolios"), "Available Portfolios")
    temp <- temp[sapply(temp, function (x) !grepl("AAA|ZZZ",x)),]
    selectInput("portfolio", "Underlying Portfolio:", choices = c("Pick One",temp))
  })

  output$portDate <- renderUI ({
    if (is.null(input$portfolio) || input$portfolio == "Pick One") return() else { 
           portfolioDates <- setNames(sendRequest(theURL, myUN, myPW, action = "GetPortfolioDates", 
                                                  portfolioName = input$portfolio, portfolioCurrency = "USD"),
                                      "Available Dates")
           selectInput("portDate", "Portfolio Date",
                             choices = c("Pick One", portfolioDates),
                             selected = "Pick One")
    }

  })
})