gpt4 book ai didi

r - 从R Shiny App中的 react 性data()调用变量

转载 作者:行者123 更新时间:2023-12-04 10:58:18 26 4
gpt4 key购买 nike

我想在响应式(Reactive)表达式中调用某个变量。像这样的东西:

服务器。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
})
})

不幸的是,我无法找到如何在 asc_new1中调用 asc_new2data()。这是行不通的:
data()$asc_new1

最佳答案

响应式(Reactive)就像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

关于r - 从R Shiny App中的 react 性data()调用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17421779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com