我试图用actionButton
触发的selectInput
更新一个React式表达式,但是没有成功。下面是(几乎)最简单的示例代码:
library(tidyverse)
library(shiny)
library(DT)
data <- tibble(ID = 1:9,
x = c(5, 4, 3, 4, 5, 7, 4, 2, 5),
min = c(NA, NA, -1, NA, NA, NA, NA, -1, NA),
rating = NA_integer_)
ui <- fluidPage(
DTOutput("tbl", width = "100%"),
hr(),
fluidRow(
column(4, selectInput(inputId = "min", label = "Choose min", choices = 1)),
column(4, selectInput(inputId = "rating", label = "Please rate",
choices = c("Choose one", "1: Yes" = "1", "2: No" = "2"))),
column(4, fluidRow(
column(12, tags$div(HTML("<p style = \"margin-bottom: 5px;\"><strong>Submit</strong></p>"))),
column(12, actionButton("submit", "Submit rating and save to data"))
)))
)
server <- function(input, output, session) {
mins <- reactive({
data %>% filter(min == -1) %>% pull(ID)
})
observeEvent(mins(), {
updateSelectInput(session, inputId = "min", choices = mins())
})
mins_table <- reactive({
data %>% filter(ID %in% mins())
})
tbl <- reactive({
DT::datatable(mins_table(),
caption = "Min to rate",
rownames = FALSE,
options = list(paging = FALSE,
scrollX = FALSE,
searching = FALSE,
ordering = FALSE,
lengthChange = FALSE)) %>%
formatStyle("ID", target = "row", fontWeight = styleEqual(as.integer(input$min), "bold"))
})
output$tbl <- renderDT({
tbl()
})
observeEvent(input$submit, {
tmp <- which(mins() == input$min)
# write rating to mins_table (to show rating in app) --> doesn't work:
################ Error occurs in the following line
mins_table()$rating[tmp] <<- as.integer(input$rating) # Error in <<-: invalid (NULL) left side of assignment
# write rating to data and save file locally (overwrite) --> works fine
data$rating[data$ID == input$min] <<- as.integer(input$rating)
saveRDS(data, file = "output/data2.rds")
# go to next min
updateSelectInput(session, inputId = "min", selected = mins()[tmp + 1])
})
}
shinyApp(ui, server)
错误发生在observeEvent
中。如有任何帮助,将不胜感激。
1条答案
按热度按时间myss37ts1#
我认为这是因为
mins_table
是一个React式,如果你想赋值,就把它改成data.frame,然后把data.frame赋值给React式。