R如有需要,刷新闪亮全局变量

时间:2018-08-20 23:01:25

标签: r shiny refresh

我有一个用户输入了他们想要的R Shiny页面的侧边栏菜单选项的数据库。当用户在浏览器中单击刷新,F5或关闭并重新启动浏览器时,用户仍然看不到他们刚刚添加到数据库中的内容。我不希望每次都运行此操作,因此尝试添加按钮或仅在数据库更改的更新时间戳记时才运行。我只是不知道如何从代码的服务器部分获取全局变量以进行更新。以下代码的示例片段:

query = paste0("select * from database.animals")
rs = dbSendQuery(conn, query)
result = fetch(rs,-1)
dbDisconnect(conn)

type<-distinct(result,type)
breed<-distinct(result,breed)
gender<-distinct(result,gender)
age<-distinct(result,age)

ui <- dashboardPage(

   dashboardSidebar(
     sidebarMenu(
          selectInput("type","Type",choices=c(type),multiple=FALSE),
          selectInput("breed","Breed:", choices=c(breed), multiple=TRUE),
          selectInput("gender","Gender:", choices=c(gender), multiple=TRUE),
          selectInput("age","Age:", choices=c(age), multiple=TRUE)
     )
   )
 )
server <- fuction(input, output, session) {

   ...What do I add in here to rerun that SQL and update the global 
      variables only when needed?.....

}

1 个答案:

答案 0 :(得分:0)

当用户点击刷新时,Shiny会自动运行原始代码。

我现在看到2个选项:

1。)您可以将所需的类别保存在单独的文件中,并且每次用户A更新时,您也可以更新文件,然后使用renderUI更改这些类别。

2。)或者您可以创建一个包含类别的反应型变量,并通过会话进行更新,然后在会话结束时将更改再次保存在单独的文件中。

Breed <- reactiveValues(br=c("original breeds"))

observeEvent("ur name of a input variable that observe changes",{
  Breed$br[length(Breed$br)+1] <- "new breed"
  updateSelectizeInput(session,"breed",choices=Breed$br, selected =Breed$br[1] )
}) 

session$onSessionEnded(function() {
    ### save Breed$br somewhere from where you will read next time the same user logs in
    ### You would probably want to create a new df where you can save user id and his changes
  })

## how to set up cookie to remember which user changed what, you can learn from this link;
## or you can create like a log in button to help you recognize who changed what
## https://gist.github.com/calligross/e779281b500eb93ee9e42e4d72448189