在服务器中定义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)
})
})
)
如果您需要共享值“x”,您应该在'renderPlot()'之外定义它。那是你在说什么?或者你也有一个名为'x'的输入?一个更完整的[可重现的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)在这里会很有帮助。 – MrFlick