【发布时间】:2017-08-03 06:19:13
【问题描述】:
我无法在 observeEvent 调用中更新列表。
该列表称为Participants,是一个列表列表 - 即每个用户的配置文件。 Participants 应该由任何用户在某个 observeEvent 调用中更新。
我尝试让变量反应和非反应,但它不起作用。
下面是一个最小的工作示例
ui.R
用户代码
library(shiny)
shinyUI( fluidPage(
sidebarLayout(
#---------------------------------------------------
# accept user transid
sidebarPanel(
numericInput("transid", "Transid", 0),
actionButton("accept", "Accept" )
),
#---------------------------------------------------
# Return user spreadsheet
mainPanel( verbatimTextOutput("userSpreadSheet"))
))
)
服务器.R
服务器代码 #------------------------------------------------ -- ################## # 定义 图书馆(闪亮)
## Create Participant List
ParticipantsDF <- data.frame(
Name=stringi::stri_rand_strings(n=20, length=8, pattern="[a-z]"),
ID=paste0("JA",1:20) )
spreadsheet <- list(data.frame(foo=1:10, bar=runif(10), transid=NA) )
Participants <- apply( ParticipantsDF, 1, function(e){
c( as.list(e), spreadsheet=spreadsheet ) })
ParticipantIDs <- ParticipantsDF$ID
ParticipantNames <- ParticipantsDF$Name
## Update Participants by changing a person
## based on user input of transid value
## change persons sublist named ``transid''
update_person <- function(transid, pid, participants){
## Which User
userProfile_u <- participants[[pid]]
## Update User next transid to TRUE
rows_vacant <- is.na(userProfile_u$spreadsheet$transid)
row_update <- head( which(rows_vacant),1)
userProfile_u$spreadsheet[row_update, "transid"] <- TRUE
## Update Participants
participants[[pid]] <- userProfile_u
Participants <<- participants
}
#----------------------------------------------------------------------
##################
# Server
server <- function( input, output, session){
#---------------------------------------------------
# Get authorized user: requires shiny-server-pro
user <- session$user
#---------------------------------------------------
# Return user profile
userPID <- which(ParticipantIDs == session$user )
userProfile <- Participants[[userPID]]
#---------------------------------------------------
# When user clicks ``accept'', update Participants
observeEvent(input$accept, {
update_person(
input$transid,
userPID,
Participants
)
})
#---------------------------------------------------
# Display spreadsheet to user
## Does Not Update!
output$userSpreadSheet <- renderPrint({
Participants[[userPID]][["spreadsheet"]] })
## Updates (Based on Accepted Answer)
#ReactiveParticipants <- reactiveValues(Participants=Participants)
#output$userSpreadSheet <- renderPrint({
# ReactiveParticipants$Participants[[userPID]][["spreadsheet"]]
#})
#observe({ invalidateLater(100)
# ReactiveParticipants$Participants <<- Participants
#})
}
shinyServer(server)
如果我编辑上面的代码不使用 renderPrint reactiveValues,那么它仍然不起作用。
【问题讨论】:
标签: r shiny reactive-programming