如何获得对象的反应值并将其传递给闪亮R中的另一个函数

时间:2015-10-16 22:10:47

标签: r shiny shiny-server

如何在plot1中使用反应值并在plot2中使用X的对象。换句话说,我想得到x的值并将其传递给plot1之外的另一个函数。 Server.R中的代码如下:

output$plot1<-renderPlot({
x<-reactiveValue(o="hello")


)}
outpu$plot2<-renderplot({
print(input$x$o)

)}

当我运行它时,它在RStudio控制台中没有显示任何内容。

1 个答案:

答案 0 :(得分:2)

在服务器中定义renderPlot之外的被动值。此外,它不属于input,因此请将其简称为x$o

library(shiny)

shinyApp(
    shinyUI(
        fluidPage(
            wellPanel(
                checkboxInput('p1', 'Trigger plot1'),
                checkboxInput('p2', 'Trigger plot2')
            ),
            plotOutput('plot2'),
            plotOutput('plot1')
        )
    ),
    shinyServer(function(input, output){
        x <- reactiveValues(o = 'havent done anything yet', color=1)

        output$plot1 <- renderPlot({
            ## Add dependency on 'p1'
            if (input$p1)
                x$o <- 'did something'
        })

        output$plot2<-renderPlot({
            input$p2 
            print(x$o)  # it is not in 'input'

            ## Change text color when plot2 is triggered
            x$color <- isolate((x$color + 1)%%length(palette()) + 1)

            plot(1, type="n")
            text(1, label=x$o, col=x$color)
        })
    })
)