繁体   English   中英

如何将 server.r 引入全球环境?

[英]how to bring server.r to global environment?

在下面的这些代码中,我想将 output$click 带到全局环境中。

最终,我想直接使用选定的值。

即使 Shiny App 被终止,是否可以以 x,y 的形式使用鼠标选择的值?

ui <- fluidPage(

radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),

plotlyOutput("plot"

),

verbatimTextOutput("click"),

verbatimTextOutput("brush")

)

server <- function(input, output, session) {

output$plot <- renderPlotly({

# use the key aesthetic/argument to help uniquely identify selected observations

key <- row.names(mtcars)

if (identical(input$plotType, "ggplotly")) {

p <- ggplot(data, aes(x = c(1:nrow(data)) , y = y, colour = factor(WF_ID), key = LOT_CODE)) +

geom_line(color="black") + geom_point()

ggplotly(p) %>% layout(dragmode = "select")

} else {

plot_ly(data, x = ~c(1:nrow(data)), y = ~y, key = ~data$LOT_CODE, mode = 'lines+markers') %>%

layout(dragmode = "select")

}

})

output$click <- renderPrint({

d <- event_data("plotly_click")

if (is.null(d)) "Click events appear here (double-click to clear)" else d

})

}

正如@HubertL 所提到的,在为变量赋值时使用<<-是可能的。 如果您想将一些值保存到变量中以供以后使用,这意味着在您的应用程序终止后,您可以使用这个<<-符。

但是,如果您想在应用程序运行时将变量设为全局变量,则可以使用reactiveValues() global.R声明您的reactiveValues()将使它们对您链接到ShinyApp每个.R文件可见。 然后,您只需将您的output$click<-分配给observer()reactive()reactiveValues() 此外,使用reactiveValues()将保持output$click的“反应性”。 如果output$click更改, reactiveValues()将更改并触发依赖项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM