简体   繁体   English

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

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

In these below code, I want to bring output$click to the global environment.在下面的这些代码中,我想将 output$click 带到全局环境中。

Ultimately, I want to make use of selected value directly.最终,我想直接使用选定的值。

Is it possible to use the values selected by a mouse in the form of x,y, even when Shiny App is terminated?即使 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

})

}

As @HubertL mentioned, using <<- when assigning a value to a variable is possible.正如@HubertL 所提到的,在为变量赋值时使用<<-是可能的。 If you want to save some value into a variable for later use, meaning, after your app terminated , you can use this <<- opreator.如果您想将一些值保存到变量中以供以后使用,这意味着在您的应用程序终止后,您可以使用这个<<-符。

However, if you want to make a variable global while your app is running you can use reactiveValues() .但是,如果您想在应用程序运行时将变量设为全局变量,则可以使用reactiveValues() Declaring your reactiveValues() in global.R will make them visible to every .R file you link to your ShinyApp .global.R声明您的reactiveValues()将使它们对您链接到ShinyApp每个.R文件可见。 You then simply assign in an observer() or reactive() your output$click with an <- into the reactiveValues() .然后,您只需将您的output$click<-分配给observer()reactive()reactiveValues() Further, using reactiveValues() will keep the "reactivity" of output$click .此外,使用reactiveValues()将保持output$click的“反应性”。 If output$click changes, reactiveValues() will change and trigger dependencies.如果output$click更改, reactiveValues()将更改并触发依赖项。

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

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