r - 使用 eventReactive 在闪亮的 R 上绘制点时出现问题
问题描述
这是我在这里的第一篇文章,所以我希望我能清楚地解释我的问题。我目前正在进入闪亮状态,并想从做一些基本的事情开始。我决定通过制作一个应用程序来挑战自己,该应用程序接受两个数字输入并在我按下操作栏时将它们绘制出来。问题是我想保留之前绘制的点。我无法让它工作,因为它不断重置情节。我尝试了许多不同的方法,但我真的不知道 points() 如何在闪亮上起作用。这是代码:
library(shiny)
ui <- fluidPage(
actionButton(inputId="execute",label="Execute"),
numericInput(inputId="numY",label="Y",value=0),
numericInput(inputId="numX",label="X",value=0),
plotOutput("plot")
)
server <- function(input, output) {
coordx <- eventReactive(input$execute,{input$numX})
coordy <- eventReactive(input$execute,{input$numY})
if(!exists("input$execute"))
{
output$plot <- renderPlot({
plot(x=coordx(),y=coordy())
})
}
else
output$plot <- renderPlot({
points(x=coordx(),y=coordy())
})
}
shinyApp(ui = ui, server = server)
先感谢您!
解决方案
正如评论者@Limey 和@fvall 所说,问题似乎是eventReactive()
每次都会覆盖的问题。我所做的是将 x 和 y 坐标放在 a 中reactiveValues()
。然后我放置了一个observeEvent()
for any timeinput$execute
被按下,写入 x 和 y 坐标来更新reactiveValues()
. 这将保留旧值和新值。我还添加了一些内容tableOutput()
来跟踪值:
library(shiny)
ui <- fluidPage(
actionButton(inputId="execute",label="Execute"),
numericInput(inputId="numY",label="Y",value=0),
numericInput(inputId="numX",label="X",value=0),
plotOutput("plot"),
tableOutput("TABLE")
)
server <- function(input, output) {
coord<-reactiveValues("x" = NULL, "y" = NULL)
observeEvent(input$execute, {
req(input$numY, input$numX)
tempx<-c(isolate(coord$x), input$numX)
tempy<-c(isolate(coord$y), input$numY)
coord$x <- tempx
coord$y <- tempy
})
output$plot <- renderPlot({
req(input$execute)
plot(x=isolate(coord$x),y=isolate(coord$y))
})
output$TABLE<-renderTable({
data.frame("x" = coord$x, "y" = coord$y)
})
}
shinyApp(ui = ui, server = server)
祝你好运!虽然我在此过程中遇到了麻烦,但我真的很喜欢自己学习 Shiny,我希望你也这样做!
推荐阅读
- javascript - 当缩略图悬停在一页中的所有产品时替换主图像
- python - 在模型中使用类元时如何修复关系不存在 - 错误?
- php - 检查表中的 id-es
- azure-ad-b2c - Azure AD B2C 自定义策略不尊重 B2C 密码保护配置
- notation - 如何在 BNF 中写一个句子?
- json - 如果 JSON 文件包含未连接的数组,AWS Quicksight 会错误地将导入的 JSON 数字字段留空
- javascript - 仅允许逗号后的空格并使用正则表达式限制文本字段中的特殊字符
- mysql - 如何在 MySQL 语句中捕获空输入值?
- javascript - 当标题是键和值的混合时,将数据从 json 附加到表
- r - 在 R 循环中删除列表中的项目