【问题标题】:Call Variable from reactive data() in R Shiny App从 R Shiny App 中的反应数据()调用变量
【发布时间】:2013-06-29 14:18:01
【问题描述】:

我想在反应式表达式中调用某个变量。像这样的:

server.R

library(raster)

shinyServer(function(input, output) {

data <- reactive({
inFile <- input$test #Some uploaded ASCII file
asc <- raster(inFile$datapath) #Reads in the ASCII as raster layer

#Some calculations with 'asc':

asc_new1 <- 1/asc
asc_new2 <- asc * 100
})

output$Plot <- renderPlot({

inFile <- input$test
if (is.null(inFile)
 return (plot(data()$asc_new1)) #here I want to call asc_new1
plot(data()$asc_new2)) #here I want to call asc_new2
})
})

很遗憾,我不知道如何在data() 中调用asc_new1asc_new2。这个不行:

data()$asc_new1

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    反应式就像 R 中的其他函数一样。你不能这样做:

    f <- function() {
      x <- 1
      y <- 2
    }
    
    f()$x
    

    所以你在output$Plot() 中的内容也不起作用。你可以通过从data()返回一个列表来做你想做的事情。

    data <- reactive({
    
      inFile <- input$test 
      asc <- raster(inFile$datapath) 
      list(asc_new1 = 1/asc, asc_new2 = asc * 100)
    
    }) 
    

    现在你可以这样做了:

    data()$asc_new1
    

    【讨论】:

    • 由于某种原因,我仍然得到同样的错误。我已将变量放入list,但我仍然无法调用它:Error in data()$fitnew : $ operator not defined for this S4 class
    • 您确定将list 放在data() 函数的最后一行吗?这就是将要返回的内容。
    【解决方案2】:

    “使用data()$asc_new1,您将无法访问在reactive 上下文中创建的变量(至少在当前闪亮版本中)。 如果你把它放在像 MadScone 这样的列表中,你需要 data()[1] data()[2]。用$ 符号调用它会引发

    警告:观察者中未处理的错误:$ 运算符对原子向量无效

    但是,你得到的错误

    data()$fitnew 中的错误:未为此 S4 类定义 $ 运算符

    不仅是因为你访问的变量不对。您将reactive 函数的输出命名为data,这是R 中的保留名称。您想将其更改为 myData 或其他内容。

    【讨论】:

      猜你喜欢
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 2019-05-31
      • 2021-01-07
      • 1970-01-01
      相关资源
      最近更新 更多