에서 http://shiny.rstudio.com/articles/scoping.html 반짝에서 범위 지정에 대한 규칙이 잘 설명되어 있습니다. 함수 내, 세션 내, 모든 세션 내에서 사용 가능한 개체의 세 가지 환경 또는 수준이 서로 중첩되어 있습니다. <-를 사용하면 현재 환경 내의 개체가 변경되고 <<-는 모든 세션에 대해 전역 적으로 변경됩니다.
세션 내에서 변수를 정의했지만 함수 내에서 변경하려면 어떻게해야합니까?
<-그냥 함수 내에서 변경하므로 다른 함수에서 읽을 수 없으며 <<-모든 세션에 대해 변경합니다. 사이에 아무것도 없나요? "단지 한 레벨 업"처럼?
참조 스테판 감사합니다. 객체가 shinyServer () 이전에 정의 된 경우 shinyServer () 내에서 <<-를 사용하면 앱의 모든 인스턴스에 대한 값이 변경됩니다. 객체가 shinyServer () 내에 정의 된 경우 <<-(함수 내부 또는 외부)는 앱의 해당 인스턴스에 대한 값만 변경합니다.
나는 이것을 테스트하기 위해 카운터와 인스턴스 ID가있는 작은 앱을 모았습니다. 앱의 두 인스턴스를 실행하고 그 사이를 전환하여 개수를 늘리면 <<-
ui.r
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Testing Environments"),
sidebarPanel(
actionButton("increment_counter", "Increase Count")
),
mainPanel(
tabsetPanel(
tabPanel("Print", verbatimTextOutput("text1"))
))
))
server.r
instance_id<-1000
shinyServer(function(input, output, session) {
instance_id<<-instance_id+1
this_instance<-instance_id
counter<-0
edit_counter<-reactive({
if(input$increment_counter>counter){
counter<<-counter+1
}
list(counter=counter)
})
output$text1 <- renderPrint({
cat(paste("Session ID: ",Sys.getpid()," \n"))
cat(paste("Global Instance ID: ",instance_id," \n"))
cat(paste("This Instance ID: ",this_instance," \n"))
cat(paste("Button Value: ",input$increment_counter," \n"))
cat(paste("Counter Value: ",edit_counter()$counter," \n"))
})
}) # end server function
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다