[英]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.