【问题标题】:Environments in R ShinyR Shiny 中的环境
【发布时间】:2014-05-01 14:02:01
【问题描述】:

http://shiny.rstudio.com/articles/scoping.html,闪亮的范围规则得到了很好的解释。有 3 个相互嵌套的环境或级别:函数内、会话内和所有会话内可用的对象。使用

如果我在会话中定义了一个变量,但又想在函数中更改它怎么办?

【问题讨论】:

  • 我认为(不确定)这句话措辞不好,“
  • 你的意思是在函数中使用
  • <<- 不是“全局”而是“非本地”。在this discussion阅读谢一辉的cmets

标签: r environment shiny scoping


【解决方案1】:

感谢斯蒂芬妮的参考。如果在 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

【讨论】:

  • 感谢您的“小应用程序”。在不同的地方看到
猜你喜欢
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多