2017-08-02 78 views
0

所以我有两个文本输出,我有一个变量声明在其中一个输出中,我想在另一个输出中使用相同的变量,但是我在第一个输出中声明的变量不能用于第二个输出所以我必须在两个输出中重新声明相同的变量,导致多次声明相同事物的混乱代码。以下是我在谈论的一个例子,这是我server.R文件...在R Shiny中对多个输出使用相同的声明变量?

output$textoutput1 <- renderText({ 
x <- 1 + (1 * 0.25) 
paste("X is equal to ", x) 
}) 

output$textoutput2 <- renderText({ 
x <- 1 + (1 * 0.25) 
paste("X times 2 is ", x*2) 
}) 

看我怎么有两个输出的x声明?有没有办法可以在服务器文件中将所有变量声明一次,并在所有输出中使用它们而不必重新声明它们?

回答

1

就像在函数中分配一个变量一样,x只会在您的输出调用中存在。

也许这些方针的东西:

x <- reactive({1 + (1 * 0.25)}) 

output$textoutput1 <- renderText({ 
paste("X is equal to ", x()) 
}) 

output$textoutput2 <- renderText({ 
paste("X times 2 is ", x()*2) 
}) 

,或者如果x是真正的静态:

x <- 1 + (1 * 0.25) 

output$textoutput1 <- renderText({ 
paste("X is equal to ", x) 
}) 

output$textoutput2 <- renderText({ 
paste("X times 2 is ", x*2) 
})