【问题标题】:Passing reactive values to functions and accessing reactive attributes将反应值传递给函数并访问反应属性
【发布时间】:2015-06-21 23:38:28
【问题描述】:

我想在将反应源发送到反应端点之前通过多个反应导体获取反应源。

通用示例:

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

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    一个问题可能是您在定义header_call 时缺少url 之后的括号。 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'
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      • 2021-09-27
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      相关资源
      最近更新 更多