cpl*_*lus 3 r reactive-programming shiny
我的目标是从googlesheet中检索数据并将其映射到传单地图上.
一切正常,只有从googlesheet检索数据的代码放在global.R中,并且它仅对正在运行的闪亮应用程序的那个会话有效.
但是,如果同时更新工作表,则这些更新不会反映在正在运行的会话中.因此,每次触发按钮时,我需要连接一个ui.R按钮来获取新数据,并将数据传递到server.R中的相关代码.(我希望这很清楚).
在我当前的设置中,数据从googlesheet(通过global.R)下载并传递到环境并用于正在运行的应用程序会话.
这是我工作的闪亮应用程序设置:
...
leafletOutput("map"),
actionButton("button", "Get New Data")
...
#added the below lines to update the question:
selectInput("Country",
"Country:",
c("All",
unique(as.character(draw$Country))))
Run Code Online (Sandbox Code Playgroud)
shinyServer(function(input, output, session) {
#...
output$map <- renderLeaflet({
#... some options here
})
draw <- mydata
drawvalue <- reactive({
if (input$year == year1){return(mydata)} else {
filtered <- filter(mydata, Type == input$type1)
return(filtered)
}
})
observe({
#... some other variable definitions
colorBy <- input$color ##added to update the question
sizeBy <- input$size ##added to update the question
draw <- drawvalue()
colorData <- draw[[colorBy]] ##added to update the question
#... code related to the leaflet
})
#...
}
Run Code Online (Sandbox Code Playgroud)
mydata <- gs_read(gs_key("0123456abcdabcd123123123"))
Run Code Online (Sandbox Code Playgroud)
经过一些阅读和探索,我被告知我必须使用反应和observeEvent.但我错误的设置导致错误,说"对象"mydata"未找到".
我在服务器上试过.R :(我知道下面的代码都有问题)
observeEvent(input$button,{
mydata <- gs_read(gs_key("0123456abcdabcd123123123"))
})
mydata <- eventReactive(input$button,{
mydata()
})
Run Code Online (Sandbox Code Playgroud)
在我的ui.R中,我也提到"画",这也是错误.我应该怎么改变这个呢?我在问题中更新了ui.R中的行.这一行是ui.R行的一部分,它调用DT包来显示一些表.
顺便说一下,这个应用程序是基于superzip闪亮的应用程序.
注意:我会为接受的答案给予100分奖励.
一般而言observe,observeEvent不返回任何值,它们用于副作用.所以下面代码的这一部分不返回任何值,即使你曾经<<-覆盖变量,mydata闪亮也不会知道它的值已经改变了.
observeEvent(input$button,{
mydata <- gs_read(gs_key("0123456abcdabcd123123123"))
})
Run Code Online (Sandbox Code Playgroud)
因此,如果您想要了解数据何时更新,您应该在响应环境中阅读它.因此,不是通过global.RI读取数据,而是建议在以下内容中执行以下操作server.R:
mydata <- eventReactive(input$button, {
gs_read(gs_key("0123456abcdabcd123123123"))
})
Run Code Online (Sandbox Code Playgroud)
单击按钮后,将读取(新)数据,然后可以访问这些数据并将其mydata()传递给代码的render*函数和其他反应部分.例如:
drawvalue <- reactive({
if (input$year == year1){return(mydata() )} else { # added ()
filtered <- filter(mydata(), Type == input$type1) # added () to mydata
return(filtered)
}
})
Run Code Online (Sandbox Code Playgroud)
你必须改变这部分代码
draw <- drawvalue()
Run Code Online (Sandbox Code Playgroud)
至
draw <- reactive({ drawvalue() })
Run Code Online (Sandbox Code Playgroud)
然后用它访问它 draw()
更新:
如果您想selectInput从UI.R中选择窗口小部件,draw您可以执行以下操作:
1)将参数添加session到服务器功能中updateSelectInput
shinyServer(function(input, output, session) {...} # added session
Run Code Online (Sandbox Code Playgroud)
2)在UI.R中设置小部件的选择为""
selectInput("Country", "Country:", choices = "")
Run Code Online (Sandbox Code Playgroud)
3)使用以下命令更新服务器端的选项:
observe({
req(draw()) # require that draw is available
updateSelectInput(session, "Country", "Country:",
c("All", unique(as.character(draw()$Country)))) # added ()
})
Run Code Online (Sandbox Code Playgroud)