r - 将 react 值传递给函数并访问 react 属性

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

最佳答案

一个问题可能是您在定义 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'
})

关于r - 将 react 值传递给函数并访问 react 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659902/

相关文章:

r - 您可以使用 R 的原生数据编辑器在 Shiny 中编辑 csv 文件吗?

r - 在 renderPlot 之外的 Shiny 中为 renderTable 提供一个对象

r - 以 Shiny 而不是错误显示空白图

r - 将一个 id 列包含到 split.default 结果列表的所有元素中的规范方法

r - Shiny 的变化 slider 动画速度

drop-down-menu - 允许在 Rhandsontable 列下拉列表中进行多选

r - 在用户使用 Shinyauth 看到应用程序的任何部分之前,如何要求在 R Shiny 中进行用户身份验证?

r - 如何读取 csv 但仅在前两个逗号分隔符处分开?

performance - R嵌套循环慢

r - 使用 ROCR 和 pROC (R) 计算平均 AUC 的差异