将反应值传递给函数并访问反应属性

时间:2015-04-15 20:08:25

标签: r shiny

我希望在将其发送到反应端点之前,通过一个以上的无功导体获取无功源。

一般例子:

x <- reactive({ function(input$var) })

y <-reactive({ function(x) })

output$display<-renderText({ y()$attr })

以下是具体的例子。

提前致谢

# Install packages if needed
packageNeeds <- c('shiny', 'httr', 'dataRetrieval')
packageNeeds <- packageNeeds[!packageNeeds %in% rownames(installed.packages())]
if(length(packageNeeds)>0){
  install.packages(packageNeeds, repos='http://cran.cnr.Berkeley.edu')
}

library(shiny)
library(httr)
library(dataRetrieval)

shinyApp(
  ui = fluidPage(
    fluidRow(
            wellPanel(
              selectInput("site", label = p("Pick a site"), choices = list('01594440', 'sysys')),
              selectInput("start", label = p("pick a date"), choices = list('1985-01-01', "xyz")))),
    fluidRow(wellPanel(verbatimTextOutput("URL"))),
    fluidRow(wellPanel(verbatimTextOutput("REC")))),
  server = function(input, output, session){
    url<-reactive({ 
      # Here the user inputs are used to constuct a url - 
      # but really this could be any function that I'd like to take the output of
      # and pass it on to other functions and/or subeset
      # before sending to a reactive output      
      constructWQPURL(paste("USGS",input$site,sep="-"),
                            c('01075','00029','00453'),
                            input$start, endDate = "")
      })   
    # This is the function I've passed the reactive value to
    # it returns an object with a headers attributes that has the 
    # 'total-result-count' attribute I want 
    header_call = reactive({HEAD(url)})

    output$URL<-renderText({
      # The url displays fine
       url()
    })
    output$REC<-renderText({
      # The record count from the header pull does not display
      # the value is the number of records stored as a string
      header_call()$headers$'total-result-count'
    })
  }
)

1 个答案:

答案 0 :(得分:1)

一个问题可能是您在定义urlheader_call后缺少括号。 url是一个闭包,url()返回URL字符串。

同样renderText是被动的,因此您只需从那里拨打HEAD(url())

这对我有用(我删除了header_call = reactive({HEAD(url)})):

output$REC<-renderText({
  # The record count from the header pull does not display
  # the value is the number of records stored as a string
  #header_call()
  HEAD(url())$headers$'total-result-count'
})