- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我每天通过 cron 作业为我的 Shiny 应用程序更新我的 RData 文件。但是, Shiny 的应用程序大多数时候不会选择更新,并继续显示旧 RData 文件中的旧数据。
这是最小的可重现示例。当从我的桌面执行 data_processing.R 时,它工作正常。然而,当它在 Rshiny 服务器上完成时,shiny 应用程序不会读取更新的日期和时间戳。
data_processing.R
rm(list=ls())
df <- iris
data_update_date_time <- Sys.time()
save.image("working_dataset.RData", compress = TRUE)
服务器.R
load("working_dataset.RData")
function(input, output, session) {
# Combine the selected variables into a new data frame
selectedData <- reactive({
df[, c(input$xcol, input$ycol)]
})
clusters <- reactive({
kmeans(selectedData(), input$clusters)
})
output$plot1 <- renderPlot({
palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
par(mar = c(5.1, 4.1, 0, 1))
plot(selectedData(),
col = clusters()$cluster,
pch = 20, cex = 3)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})
## Data update date and time stamp
output$update_date_time <- renderPrint(data_update_date_time)
}
ui.R
pageWithSidebar(
headerPanel('Iris k-means clustering'),
sidebarPanel(
selectInput('xcol', 'X Variable', names(iris)),
selectInput('ycol', 'Y Variable', names(iris),
selected=names(iris)[[2]]),
numericInput('clusters', 'Cluster count', 3,
min = 1, max = 9),
br(),
h4("Date update date time"),
textOutput("update_date_time")
),
mainPanel(
plotOutput('plot1')
)
)
感谢您抽出时间。
最佳答案
实际上,shiny
包中有一个名为 reactiveFileReader
的函数,它完全可以满足您的需求:定期检查文件“上次修改”时间或大小是否发生变化并相应地重读。但是,此函数只能在服务器
上下文中使用,因此对于连接到您的应用的每个用户,该文件至少会被读取一次。我的答案中的选项 3 和 4 没有这些低效问题。
首先,Shiny 没有办法跟踪文件更改 AFAIK。您的实现每当
时都会重新加载.RData
文件
shiny-server
通过 bash 重新启动无法判断何时满足第二个条件。因此,我建议使用以下四个选项之一。从简单到你最好了解你的 Shiny !排序。
在这里,每当新用户连接应用程序时,都会重新加载图像。但是,如果您的 .RData
文件很大,这可能会减慢您的应用程序的速度。如果速度不是问题,我会选择这个解决方案,因为它简单且干净。
# server.R
function(input, output, session) {
load("working_dataset.RData")
...
}
每当用户刷新页面(F5)时,数据也会被重新读取
(另请参阅@shosacos 答案)。这会强制重新加载 .Rdata
文件。
$ sudo systemctl restart shiny-server
同样,这可能会减慢您的生产过程,具体取决于应用程序的复杂性。这种方法的一个优点是,如果您在 global.R
中加载数据,您还可以使用导入的数据来构建 ui。 (我假设您没有给出您所提供的代码)。
这里的想法是在用户连接到应用时检查 .RData
是否已更改。为此,您必须使用包含上次导入版本的时间戳的“全局”变量。以下代码未经测试,但应该可以让您了解如何实现此功能。
# server.R
last_importet_timestamp <- reactiveVal("")
function(input,output,session){
current_timestamp <- file.info(rdata_path)$mtime
if(last_importet_timestamp() != current_timestamp){
# use parent.frame(2) to make data available in other sessions
load(rdata_path, envir = parent.fame(2))
# update last_importet_timestamp
last_importet_timestamp(current_timestamp)
}
...
}
就速度而言,这应该比前两个版本更高效。每个时间戳导入数据的次数不会超过一次(除非 Shiny 的服务器重新启动或变得空闲)。
基本上与选项 3 相同,但每 50 毫秒检查一次文件是否有更改。这是此方法的完整工作示例。请注意,除非检测到“上次修改”的更改,否则不会加载数据,因此产生的开销并不算太糟糕。
library(shiny)
globalVars <- reactiveValues()
rdata_path = "working_dataset.RData"
server <- function(input, output, session){
observe({
text = input$text_in
save(text = text, file = rdata_path, compress = TRUE)
})
observe({
invalidateLater(50, session)
req(file.exists(rdata_path))
modified <- file.info(rdata_path)$mtime
imported <- isolate(globalVars$last_imported)
if(!identical(imported, modified)){
tmpenv <- new.env()
load(rdata_path, envir = tmpenv)
globalVars$workspace <- tmpenv
globalVars$last_imported <- modified
}
})
output$text_out <- renderText({
globalVars$workspace$text
})
}
ui <- fluidPage(
textInput("text_in", "enter some text to save in Rdata", "default text"),
textOutput("text_out")
)
shinyApp(ui, server)
如果您觉得使用globalVars$workspace$text
不方便,可以使用with
直接访问globalVars$workspace
的内容.
output$text_out <- renderText({
with(globalVars$workspace, {
paste(text, "suffix")
})
})
关于r - Shiny 应用程序不反射(reflect)更新 RData 文件中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719268/
我有: func NewMethodDescriptor(typ interface{}) *MethodDescriptor { reflectedMethod := reflect.Val
我需要确定地检查 reflect.Type 是否是一个错误。 错误没有反射(reflect)类型。在 go reflect 中检查类型错误的正式/惯用方式是什么? Go Playground Full
根据 reflect 文档 reflect.Value.MapIndex() 应返回一个 reflect.Value,它表示存储在 map 特定键处的数据的值.所以我的理解是以下两个表达式应该是相同的
与 reflect pkg 有点混淆 所有示例都使用 reflect.NewValue() 来获取 var 的 reflect.Value,但是 func NewValue 未记录在 http://g
在计算机语言的上下文中,我从未找到关于反射的词源的明确解释,所以我想在这里澄清一下。 “Reflection”源于拉丁语,有以下definitions : bend back turn back tu
我写了一个漂亮的函数,它可以接受 system.object ,反射(reflect)其属性并将对象序列化为 JSON 字符串。它看起来像这样: public class JSONSerializer
我正在尝试创建一个函数 import Language.Reflection foo : Type -> TT 我尝试使用reflect 策略: foo = proof { intro t
最近我和一位同事谈论 C++,感叹没有办法获取带有类字段名称的字符串并提取具有该名称的字段;换句话说,它缺乏反射(reflection)。他困惑地看着我,并问什么时候有人需要做这样的事情。 除了“嘿,
我正在考虑允许模块与属性文件中的类一起使用的想法;像 availableModules.properties Contact=org.addressbook.ContactMain Business=
这个问题特别与为具有大量字段的对象覆盖 equals() 方法有关。首先,让我说这个大对象不能在不违反 OO 原则的情况下分解成多个组件,所以告诉我“没有类应该有超过 x 个字段”无济于事。 继续前进
例子 router.Get(path, handler) // works fine methodStr = "Get" router.methodStr(path, handler) // e
我一直坚持使用反射库的问题。由于很多推荐,我决定使用它,但我只是在学习,有些部分并不是很容易.. 我有这部分代码: func countDataByName(sourceName string, s
我有一个包含一些 url 参数的特定结构,我想使用 reflect 构建一个 url 参数字符串以遍历结构字段,这样我就不会关心结构真正包含什么。 假设我有一个这样的结构: type Student
我正在尝试从 reflect.Value 中检索字符串值, 我希望 value.String()成为okok但我得到了相反。 我错过了什么吗? package main import ( "f
为了避免创建 org.reflections.Reflections 类的多个实例,我只想创建一个并根据需要重用。有谁知道这个类是否是线程安全的? 如果它不是线程安全的,我知道我可以使用 Java 的
我最近对引用、具体化和反射(reflection)感到困惑。有人可以很好地解释他们的关系和差异(如果有的话)吗? 最佳答案 引用 这可能是最简单的一个。考虑一下当您在 REPL 中键入以下内容时会发生
less main.go输出: ``` package main import ( "reflect" "net/url" "fmt" ) type User struct {
我在 golang 中使用 gorm 包 ( https://github.com/jinzhu/gorm ) 作为我的数据库库。我有很多类(数据库表),如“酒店”或“套餐”。复制代码不是好的编程习惯
我有代码 var t reflect.Type = LaunchController(route.controller) // create controller ptr . var
是否有可能以及如何在不从类型创建对象并调用它的情况下获取类型的 reflect.Type reflect.TypeOf(obj) Java 中的内容是:MyType.class 最佳答案 您可以使用以
我是一名优秀的程序员,十分优秀!