簡體   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