【问题标题】:Shiny server is returning wrong value for function闪亮的服务器返回错误的函数值
【发布时间】:2016-07-01 03:50:59
【问题描述】:

我是闪亮的新手(和任何网络应用程序的东西),但相当精通 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 是静态的,即在您第一次加载页面时只需要加载一次,我尝试在服务器函数之外获取列表。我在想这会设置一个全局变量,然后我可以在 UI 中引用。这没有用。任何想法为什么这种方法不起作用?

这与服务器功能中的“会话”有什么关系吗?我有旧的会话正在运行吗? “会话”是 R 会话吗?当我在 RStudio 中重新启动应用程序时它会重新启动吗?

【问题讨论】:

  • 您需要在反应函数中返回您的投资组合列表对象。此外,我建议将 renderUI 用于动态 ui 部分。
  • 感谢您的宝贵时间。我已经尝试过了,它并没有改变任何东西。另外,我尝试使用 renderUI,但无法正常工作。
  • 这是您运行的确切代码吗?以为这是一个草图,您的闪亮环境根本不应该知道portfolioList,请尝试在ui.r 中调用portfolioInput() - 但我不确定这是否适用于您的ui.r。我正在尝试一起取东西..
  • 哈!这正是我的观点!我不知道它来自哪里......这就是为什么我认为这是一个在后台运行的旧会话或其他东西。这是我正在运行的确切代码。
  • 我刚重启了RStudio,现在不行了。我得到:找不到对象'portfolioList'

标签: r shiny shiny-server


【解决方案1】:

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

这是工作界面:

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

  })
})

【讨论】:

    【解决方案2】:

    给你一些开始,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())
    
        })
    
      }
    )
    

    要添加到我的 cmets,您不能简单地调用 ui.r 中的函数或对象,您可以在 server.r 中渲染对象并调用这些对象,在 ui.r 中标记为 output$name。 我建议你做闪亮的教程http://shiny.rstudio.com/tutorial/

    【讨论】:

    • 这是我之前尝试使用 renderUI 时遇到的问题。运行您的代码,我不断收到 ERROR: [on_request_read] connection reset by peer。 (我使用 chrome 作为浏览器,所以它应该可以工作。)对不起,我实际上是在运行你的代码,在服务器中使用我的 observe() 函数。
    • 嗯,很奇怪,如果你把这段代码复制到你的 r 控制台,它会起作用吗?
    • 我如何将 output$portfolio selectInput 的选择返回到 portDate selectInput?
    • 编辑了一个反应函数,你可以在其他请求中使用它。我猜您想将“portfolio”的输入用于“portDate”?只需调用 input$portfolio 并在 server.r 中构建另一个 renderUI - 或者您可以选择使用您的 updateSelectInput,但只是渲染一个新的 ui 可能更容易开始。
    • 我需要使用投资组合的选择进行新的 API 调用,然后返回该投资组合的可用日期。所以,我需要从列表中进行选择。
    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 2015-10-22
    • 2014-11-26
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2021-03-10
    相关资源
    最近更新 更多