简体   繁体   中英

how to bring server.r to global environment?

In these below code, I want to bring output$click to the global environment.

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?

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. 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() . Declaring your reactiveValues() in global.R will make them visible to every .R file you link to your ShinyApp . You then simply assign in an observer() or reactive() your output$click with an <- into the reactiveValues() . Further, using reactiveValues() will keep the "reactivity" of output$click . If output$click changes, reactiveValues() will change and trigger dependencies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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