连续更新 rhandsontable 中的条件值
Continuous updating of conditional values in a rhandsontable
我有一个迷你 Shiny 应用程序,可以正常运行。
首先,在应用程序的文件夹中,我为两个商店生成了一个包含数据框的列表:
stores <- list(store1 = tibble(Date = as.Date(c("2019-08-31", "2019-09-01", NA)), Item = c("A", "B", NA),
Price = c(100, 120, NA), Comment = as.character(rep(NA, 3))),
store2 = tibble(Date = as.Date(c("2019-08-31", NA, NA)), Item = c("C", NA, NA),
Price = c(95, NA, NA), Comment = as.character(rep(NA, 3))))
saveRDS(stores, file = "stores.rds")
print(stores)
这是我闪亮的代码。我希望用户能够根据需要更新每个商店 table 中的信息,并通过单击 "Update store info" 操作按钮保存更改。
但是,请注意:在服务器代码的末尾,我有此 'conditional' 行:mutate(Comment = ifelse(Price > 100, "Nice!", Comment)): If Price is > 100,评论 "Nice!" 应该会出现 - 我无需手动输入。
问题:我不知道如何在点击 input$update_store 时让这个条件注释立即出现在屏幕上的 table 中。我可以在下拉菜单中切换到另一家商店,然后回到第一家商店——评论就会在那里!但是有没有办法让它在点击 input$update_store 后立即更新?
非常感谢您的帮助!
library(shiny)
library(dplyr)
library(rhandsontable)
# Read in the existing list of stores:
stores <- readRDS("stores.rds")
print("Reading in stores the first time:")
print(stores)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### ui code ####
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ui <- fluidPage(
titlePanel("My UI"), # Application title
sidebarLayout( # Sidebar with a pull-down to select a store:
sidebarPanel(
selectizeInput("store_select", label = "Select store",
choices = names(stores), multiple = FALSE,
selected = names(stores)[1]),
actionButton("update_store", "Update store Info")
),
mainPanel( # Main panel with an editable table:
rHandsontableOutput("store_table")
)
)
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### server code ####
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
server <- function(input, output, session) {
stores_reactive <- reactiveValues( # Creating reactive values for stores:
stores = stores
)
# What happens when one store is selected:
mystore <- eventReactive(input$store_select, {
store_name <- input$store_select
store_table <- stores_reactive$stores[[store_name]]
return(store_table)
})
# rhandsontable to be shown:
output$store_table <- renderRHandsontable({
rhandsontable(mystore())
})
# What happens upon pressing button "Update store Info":
observeEvent(input$update_store, {
stores[[input$store_select]] <- hot_to_r(input$store_table) %>%
mutate(Comment = ifelse(Price > 100, "Nice!", Comment))
stores_reactive$stores <- stores # Update stores_reactive
saveRDS(stores, file = "stores.rds") # save stores to the file
stores <<- stores # Update 'stores' list
})
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Run the app ####
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
shinyApp(ui = ui, server = server)
您不需要 mystore
成为 input$store_select
的 eventReactive
方法。如果单击操作按钮 (input$update_store
),您的 mystore
方法将不会被调用,因为 store_select
没有改变。
如果您想保留 mystore
功能,您可以执行以下操作,它应该可以工作。
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### server code ####
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
server <- function(input, output, session) {
stores_reactive <- reactiveValues( # Creating reactive values for stores:
stores = stores
)
# What happens when one store is selected:
mystore <- function(store_name) {
store_table <- stores_reactive$stores[[store_name]]
return(store_table)
}
output$store_table <- renderRHandsontable({
rhandsontable(mystore(input$store_select))
})
# What happens upon pressing button "Update store Info":
observeEvent(input$update_store, {
stores[[input$store_select]] <- hot_to_r(input$store_table) %>%
mutate(Comment = ifelse(Price > 100, "Nice!", Comment))
stores_reactive$stores <- stores # Update stores_reactive
saveRDS(stores, file = "stores.rds") # save stores to the file
stores <<- stores # Update 'stores' list
})
}
或者您可以完全不使用 mystore
函数,使用以下代码:
# rhandsontable to be shown:
output$store_table <- renderRHandsontable({
rhandsontable(stores_reactive$stores[[input$store_select]])
})
我有一个迷你 Shiny 应用程序,可以正常运行。 首先,在应用程序的文件夹中,我为两个商店生成了一个包含数据框的列表:
stores <- list(store1 = tibble(Date = as.Date(c("2019-08-31", "2019-09-01", NA)), Item = c("A", "B", NA),
Price = c(100, 120, NA), Comment = as.character(rep(NA, 3))),
store2 = tibble(Date = as.Date(c("2019-08-31", NA, NA)), Item = c("C", NA, NA),
Price = c(95, NA, NA), Comment = as.character(rep(NA, 3))))
saveRDS(stores, file = "stores.rds")
print(stores)
这是我闪亮的代码。我希望用户能够根据需要更新每个商店 table 中的信息,并通过单击 "Update store info" 操作按钮保存更改。
但是,请注意:在服务器代码的末尾,我有此 'conditional' 行:mutate(Comment = ifelse(Price > 100, "Nice!", Comment)): If Price is > 100,评论 "Nice!" 应该会出现 - 我无需手动输入。
问题:我不知道如何在点击 input$update_store 时让这个条件注释立即出现在屏幕上的 table 中。我可以在下拉菜单中切换到另一家商店,然后回到第一家商店——评论就会在那里!但是有没有办法让它在点击 input$update_store 后立即更新?
非常感谢您的帮助!
library(shiny)
library(dplyr)
library(rhandsontable)
# Read in the existing list of stores:
stores <- readRDS("stores.rds")
print("Reading in stores the first time:")
print(stores)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### ui code ####
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ui <- fluidPage(
titlePanel("My UI"), # Application title
sidebarLayout( # Sidebar with a pull-down to select a store:
sidebarPanel(
selectizeInput("store_select", label = "Select store",
choices = names(stores), multiple = FALSE,
selected = names(stores)[1]),
actionButton("update_store", "Update store Info")
),
mainPanel( # Main panel with an editable table:
rHandsontableOutput("store_table")
)
)
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### server code ####
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
server <- function(input, output, session) {
stores_reactive <- reactiveValues( # Creating reactive values for stores:
stores = stores
)
# What happens when one store is selected:
mystore <- eventReactive(input$store_select, {
store_name <- input$store_select
store_table <- stores_reactive$stores[[store_name]]
return(store_table)
})
# rhandsontable to be shown:
output$store_table <- renderRHandsontable({
rhandsontable(mystore())
})
# What happens upon pressing button "Update store Info":
observeEvent(input$update_store, {
stores[[input$store_select]] <- hot_to_r(input$store_table) %>%
mutate(Comment = ifelse(Price > 100, "Nice!", Comment))
stores_reactive$stores <- stores # Update stores_reactive
saveRDS(stores, file = "stores.rds") # save stores to the file
stores <<- stores # Update 'stores' list
})
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Run the app ####
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
shinyApp(ui = ui, server = server)
您不需要 mystore
成为 input$store_select
的 eventReactive
方法。如果单击操作按钮 (input$update_store
),您的 mystore
方法将不会被调用,因为 store_select
没有改变。
如果您想保留 mystore
功能,您可以执行以下操作,它应该可以工作。
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### server code ####
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
server <- function(input, output, session) {
stores_reactive <- reactiveValues( # Creating reactive values for stores:
stores = stores
)
# What happens when one store is selected:
mystore <- function(store_name) {
store_table <- stores_reactive$stores[[store_name]]
return(store_table)
}
output$store_table <- renderRHandsontable({
rhandsontable(mystore(input$store_select))
})
# What happens upon pressing button "Update store Info":
observeEvent(input$update_store, {
stores[[input$store_select]] <- hot_to_r(input$store_table) %>%
mutate(Comment = ifelse(Price > 100, "Nice!", Comment))
stores_reactive$stores <- stores # Update stores_reactive
saveRDS(stores, file = "stores.rds") # save stores to the file
stores <<- stores # Update 'stores' list
})
}
或者您可以完全不使用 mystore
函数,使用以下代码:
# rhandsontable to be shown:
output$store_table <- renderRHandsontable({
rhandsontable(stores_reactive$stores[[input$store_select]])
})