R 用计算机绘制图形;使用重复循环绘制数据以进行自动分析
编辑 我正在构建一个闪亮的应用程序,其中一个组件将包括自动数据分析。我的主要用途是访问API以收集数据并对其进行后续分析。此操作发生在重复循环内,延迟5分钟,但这可以由应用程序的用户指定。延迟结束后,将再次访问API,整个过程将重新开始。当我在主控制台中以列表的形式返回绘图/表格时,这对我在shiny之外的工作很好 但是,我无法在一个闪亮的应用程序中执行它。我无法提供API信息,但出于所有目的,这里有一个使用mpg数据集的可复制示例 下面是一个使用动作按钮而不使用重复循环生成的绘图示例。每次单击“操作”按钮时,图形将使用当前系统时间更新: 以及生成这个的代码:-R 用计算机绘制图形;使用重复循环绘制数据以进行自动分析,r,plot,shiny,repeat,shinydashboard,R,Plot,Shiny,Repeat,Shinydashboard,编辑 我正在构建一个闪亮的应用程序,其中一个组件将包括自动数据分析。我的主要用途是访问API以收集数据并对其进行后续分析。此操作发生在重复循环内,延迟5分钟,但这可以由应用程序的用户指定。延迟结束后,将再次访问API,整个过程将重新开始。当我在主控制台中以列表的形式返回绘图/表格时,这对我在shiny之外的工作很好 但是,我无法在一个闪亮的应用程序中执行它。我无法提供API信息,但出于所有目的,这里有一个使用mpg数据集的可复制示例 下面是一个使用动作按钮而不使用重复循环生成的绘图示例。每次单击
library(shiny)
library(ggplot2)
ui<-fluidPage(
titlePanel('Minimal example'),
tabsetPanel(
tabPanel("Example",
#summary
sidebarPanel(width = 4,
h5("The default interval for the analysis refresh is 5 minutes. If you wish to change this, please do so in the box below:"),
numericInput("intervaltime","Input refresh interval:",5),
br(),
h5("Press 'Run Analysis' button below to start automated analysis"),
actionButton("automatedanalysis", "Run Analysis")),
mainPanel(
h4("An example plot"),
plotOutput("example_plot", width = "100%"),
h4("Some text with updated system time"),
textOutput("example_text")
)
)))
server<-function(input,output,session){
observeEvent(input$automatedanalysis,{
#interval=input$intervaltime*60
#repeat{
currenttime<-Sys.time()
p<-ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()+ggtitle(paste0("graph made at: ",currenttime))# adds on the current time
output$example_plot<-renderPlot({
return(p)
})
output$example_text<-renderText({
print(paste0("The current system time is: ", Sys.time())) #a check to know that it is working
})
#Sys.sleep(interval)
#}
})
}
shinyApp(ui, server)
但是,当我使用间隔计时器(可以使用UI中的数字输入进行切换)执行重复循环时,它就不再工作了。以下是非工作代码:-
ui<-fluidPage(
titlePanel('Minimal example'),
tabsetPanel(
tabPanel("Example",
#summary
sidebarPanel(width = 4,
h5("The default interval for the analysis refresh is 5 minutes. If you wish to change this, please do so in the box below:"),
numericInput("intervaltime","Input refresh interval:",5),
br(),
h5("Press 'Run Analysis' button below to start automated analysis"),
actionButton("automatedanalysis", "Run Analysis")),
mainPanel(
h4("An example plot"),
plotOutput("example_plot", width = "100%"),
h4("Some text with updated system time"),
textOutput("example_text")
)
)))
server<-function(input,output,session){
observeEvent(input$automatedanalysis,{
interval=input$intervaltime*60
repeat{
currenttime<-Sys.time()
p<-ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point()+ggtitle(paste0("graph made at: ",currenttime))# adds on the current time
output$example_plot<-renderPlot({
return(p)
})
output$example_text<-renderText({
print(paste0("The current system time is: ", Sys.time())) #a check to know that it is working
})
Sys.sleep(interval)
}
})
}
shinyApp(ui, server)
没有图形或文本输出出现
再一次,在一个闪亮的应用程序之外,它工作得很好,但我需要它作为我正在开发的闪亮应用程序的一个功能
总之,我如何才能使其工作,以便在单击操作按钮时,分析在间隔期结束后刷新?您应该查看invalidateLater或reactiveTimer函数 我在下面的示例中使用invalidateLater注意:函数以毫秒作为第一个参数 您也不应该在观察者中放置任何输出,创建一个reactiveVal/reactiveValues对象,并在每一个新间隔填充该对象。你可以在应用程序的任何地方使用该对象 我还将observeEvent更改为普通的observeEvent,因为否则它只会在单击按钮时触发。现在,当单击按钮时,观察者将触发,间隔滑块将更改,并且间隔已过
library(shiny)
library(ggplot2)
ui<-fluidPage(
titlePanel('Minimal example'),
tabsetPanel(
tabPanel("Example",
sidebarPanel(width = 4,
h5("The default interval for the analysis refresh is 5 minutes. If you wish to change this, please do so in the box below:"),
numericInput("intervaltime","Input refresh interval:",5),
br(),
h5("Press 'Run Analysis' button below to start automated analysis"),
actionButton("automatedanalysis", "Run Analysis")),
mainPanel(
h4("An example plot"),
plotOutput("example_plot", width = "100%"),
h4("Some text with updated system time"),
textOutput("example_text")
)
)))
server<-function(input,output,session){
rv <- reactiveVal(NULL)
observe({
interval = input$intervaltime*1000
invalidateLater(interval, session)
req(input$automatedanalysis)
print("Fill ReactiveVal")
mpg$hwy <- mpg$hwy * runif(nrow(mpg))
rv(mpg)
})
output$example_plot<-renderPlot({
req(rv())
currenttime<-Sys.time()
print("Plot Code")
ggplot(rv(), aes(displ, hwy, colour = class)) +
geom_point()+ggtitle(paste0("graph made at: ",currenttime))
})
output$example_text<-renderText({
print(paste0("The current system time is: ", Sys.time())) #a check to know that it is working
})
}
shinyApp(ui, server)
您应该看看invalidateLater或reactiveTimer函数 我在下面的示例中使用invalidateLater注意:函数以毫秒作为第一个参数 您也不应该在观察者中放置任何输出,创建一个reactiveVal/reactiveValues对象,并在每一个新间隔填充该对象。你可以在应用程序的任何地方使用该对象 我还将observeEvent更改为普通的observeEvent,因为否则它只会在单击按钮时触发。现在,当单击按钮时,观察者将触发,间隔滑块将更改,并且间隔已过
library(shiny)
library(ggplot2)
ui<-fluidPage(
titlePanel('Minimal example'),
tabsetPanel(
tabPanel("Example",
sidebarPanel(width = 4,
h5("The default interval for the analysis refresh is 5 minutes. If you wish to change this, please do so in the box below:"),
numericInput("intervaltime","Input refresh interval:",5),
br(),
h5("Press 'Run Analysis' button below to start automated analysis"),
actionButton("automatedanalysis", "Run Analysis")),
mainPanel(
h4("An example plot"),
plotOutput("example_plot", width = "100%"),
h4("Some text with updated system time"),
textOutput("example_text")
)
)))
server<-function(input,output,session){
rv <- reactiveVal(NULL)
observe({
interval = input$intervaltime*1000
invalidateLater(interval, session)
req(input$automatedanalysis)
print("Fill ReactiveVal")
mpg$hwy <- mpg$hwy * runif(nrow(mpg))
rv(mpg)
})
output$example_plot<-renderPlot({
req(rv())
currenttime<-Sys.time()
print("Plot Code")
ggplot(rv(), aes(displ, hwy, colour = class)) +
geom_point()+ggtitle(paste0("graph made at: ",currenttime))
})
output$example_text<-renderText({
print(paste0("The current system time is: ", Sys.time())) #a check to know that it is working
})
}
shinyApp(ui, server)
嗨,罗宾。如果没有一个完整的可重复脚本,就很难提供帮助。虽然您声明Shining应用程序使用了一些敏感内容,但您可能会生成一些虚拟数据来处理?。我不确定这一点,但因为您在observeEvent调用中使用repeat,并且在该调用中创建的对象是短暂的,仅在退出时显示。我认为你的重复呼叫永远不会结束,然后“观察事件”永远不会结束,因为无法打印你的情节。尝试在ObserveeEvent之外使用反应值。嗨@BaltazarGonzálezChávez,我同意很难提供帮助,通常我会尝试提供一个reprex来提供帮助,但在这种情况下并不可行。其思想是,操作按钮automatedanalysis启动自动分析,然后启动重复循环过程。您所说的对于observeEvent是有意义的,但是,当我用reactiveValues包装它时,reactiveValues中会出现错误{:传递给reactiveValues的所有参数都必须命名。你知道为什么吗?一个更简单的问题是:有没有合适的方法可以在shiny app中使用重复循环在UI中重现新的绘图?到目前为止,从我的搜索中有没有这样的例子,我想不出多少?如果我能做到这一点,那么我很可能可以从那里回答我的问题。嗨@BaltazarGonzálezChávez,我已经更新了这个问题,使它更友好和可复制。我还添加了一个赏金。所以请随意看看,看看你是否能解决它。谢谢!嗨@Robin。没有完整的可重复脚本,很难帮助。尽管你说闪亮的应用程序使用了一些敏感的内容nt,也许您可以生成一些虚拟数据来处理?。我不确定这一点,但因为您在observeEvent调用中使用repeat,并且在该调用中创建的对象是短暂的,仅在退出时显示。我认为您的repeat调用永远不会结束,然后“observeEvent”永远不会完成无法打印您的绘图。请尝试在外部使用ReactiveValueobserveEvent.Hi@BaltazarGonzálezChávez,我同意很难提供帮助,通常我会尝试提供一个reprex来帮助,但在这种情况下并不可行。我的想法是动作按钮自动分析开始自动分析
然后开始重复循环过程。您所说的对于observeEvent是有意义的,但是,当我用reactiveValues包装它时,reactiveValues中会出现错误{:传递给reactiveValues的所有参数都必须命名。你知道为什么吗?一个更简单的问题是:有没有合适的方法可以在shiny app中使用重复循环在UI中重现新的绘图?到目前为止,从我的搜索中有没有这样的例子,我想不出多少?如果我能做到这一点,那么我很可能可以从那里回答我的问题。嗨@BaltazarGonzálezChávez,我已经更新了这个问题,使它更友好和可复制。我还添加了一个赏金。所以请随意看看,看看你是否能解决它。谢谢!嗨@SeGa,非常感谢你看这个问题。我将尝试用我的实际数据集来补充这个问题,我将我会在几个小时内确认它是否有效:这很有效,我做了一些小调整,但只是针对我自己的数据。谢谢!嗨@SeGa,再次感谢你的解决方案,它工作得非常好!我想知道我是否可以再问一个问题。由于这段代码中的分析是自动化的,我正在考虑添加一个n额外的actionButton,它只是停止自动分析。我似乎无法在网上找到任何相关的示例,我想知道您是否可以提出一些建议?如果不太麻烦,我们将非常感谢:您可以使用相同的actionButton,并使用模2检查按钮是否处于活动状态。->放置reqinput$automatedanalysis%%2!=0,然后invalidateLater部分应该可以工作。嗨,世嘉,很抱歉我花了很长时间才回复。这很好,非常感谢!嗨@世嘉,非常感谢您查看此内容。我将尝试使用我的实际数据集对此进行扩充,我将在几个小时内确认它是否工作:这很好,我制作了一些轻微的调整,但仅与我自己的数据有关。谢谢!Hi@SeGa,再次感谢您的解决方案,它工作得非常好!我想知道是否可以再问一个问题。由于此代码中的分析是自动的,我正在考虑添加一个额外的actionButton,它可以简单地停止自动分析。我似乎无法回答我想知道您是否可以提供一些建议?如果不是太麻烦,我们将非常感谢:您可以使用相同的操作按钮,并使用模2检查按钮是否处于活动状态。->将reqinput$automatedanalysis%%2!=0放在invalidateLater部分工作之前。您好,SeGa,抱歉我花了很长时间才回复。这很好,非常感谢!