在R中创建基线比较数据-复制输入的数据帧
本练习的目的是允许用户根据输入比较两种不同的模型。为此,我创建了一个操作按钮,要求用户指定其基本模型,并创建了一个重置按钮,将数据集恢复到添加基线之前的状态。“基”逻辑确定用户是否希望包括基 单击添加基线操作按钮后,data.frame的当前状态将被保存,分组变量将被重命名,并在其前面添加“基线”(使用粘贴)。用户可以选择一个不同的模型,与此静态基础进行比较 由于某些原因,我无法让observe事件更改数据集。observe事件创建基线数据集fine(使用print()测试),但是,if()函数不会更改“data”,因此会停止添加到ggplot的基。这样编写代码有两个原因。1) 通过在observe事件之后包含if()函数,对数据的任何进一步更改只会更改“data”,然后将其添加到未更改的基线数据中。2) 还允许创建重置按钮,该按钮仅在RBIND发生之前将data.frame重置为 这个小问题激怒了我,我看不出我错在哪里。提前为人们能提供的任何帮助欢呼。有更简单的方法可以做到这一点(开放的建议),然而,虹膜数据只是一个例子的功能,而实际的版本更复杂在R中创建基线比较数据-复制输入的数据帧,r,shiny,R,Shiny,本练习的目的是允许用户根据输入比较两种不同的模型。为此,我创建了一个操作按钮,要求用户指定其基本模型,并创建了一个重置按钮,将数据集恢复到添加基线之前的状态。“基”逻辑确定用户是否希望包括基 单击添加基线操作按钮后,data.frame的当前状态将被保存,分组变量将被重命名,并在其前面添加“基线”(使用粘贴)。用户可以选择一个不同的模型,与此静态基础进行比较 由于某些原因,我无法让observe事件更改数据集。observe事件创建基线数据集fine(使用print()测试),但是,if()函数
library("ggplot2")
if (interactive()) {
ui <- fluidPage(
selectInput("rows", label = h3("Choose your species"),
choices = list("setosa", "versicolor", "virginica")
),
actionButton("base", "Create baseline"),
actionButton("reset", "Reset baseline"),
plotOutput(outputId = "plot")
) # close fluid page
server <- function(input, output) {
output$plot <- renderPlot({ # create plot
base <- "no" # create baseline indicator which we can change once the observeevent below is changed
data <- iris
data <- iris[which(data$Species == input$rows),] # Get datasubset based on user input
observeEvent(input$base, { # If base is Pressed, run code below:
baseline <- data # Make Baseline Data by duplicating the users' specification
baseline$Species <- paste("Baseline",
data$Species, sep = "_") # Rename the grouping variable to add Baseline B4 it
base <- "yes" # Change our indicator of whether a baseline had been made to yes
}) # Close observe Event
observeEvent(input$reset, {
base <- "no" # This is placed before the rbind so that if we want to reset it will stop the merging of the two dataframes before it happens.
})
if (base == "yes") {
data <- rbind(data, baseline) # Run once the observe event has changed baseline to yes.This is kept seperatel that way any subsequent changes to data will not effect
# the final data. This command will simple add the base onto the changed "data" before plotting
}
observeEvent(input$reset, {
base <- "no"
})
ggplot(data, aes(x=Petal.Width, y = as.numeric(Sepal.Width), colour = Species)) + # variable = each dataset selected, value = respective values for that model
labs(x="Hypothetical X", y="Hypothetical X") +
geom_line()
}) # Close Render Plot
} # Close Serve Function
shinyApp(ui, server)
}
库(“ggplot2”)
if(interactive()){
ui在反应式表达式中有观察者,我在更正闪亮代码时多次看到这会导致问题。创建反应式表达式(绘图函数)和观察者仅指定哪一个是物种的基线值(字符串),然后将其提供给绘图函数中的过滤数据:
library(shiny)
library(ggplot2)
ui <- fluidPage(
selectInput("rows", label = h3("Choose your species"),
choices = list("setosa", "versicolor", "virginica")
),
actionButton("base", "Create baseline"),
actionButton("reset", "Reset baseline"),
plotOutput(outputId = "plot")
) # close fluid page
server <- function(input, output) {
rVals = reactiveValues()
rVals[['data']] = iris
rVals[['baseline']] = NULL
output$plot <- renderPlot({
# here we duplicate table to manipulate it before rendering
# the reason for duplicate is that you dont want to affect your
# base data as it may be used elsewhere
# note that due to R's copy-on-write this may be expensive operation and
# have impact on app performance
# in all cases using data.table package is recommended to mitigate
# some of the CoW implications
render.data = rVals[['data']][rVals[['data']][['Species']] %in% c(rVals[['baseline']],input$rows),]
# here manipulate render.data
# and then continue with plot
ggplot(data=render.data,
aes(x=Petal.Width, y = as.numeric(Sepal.Width), colour = Species,group=Species)
) +
labs(x="Hypothetical X", y="Hypothetical X") +
geom_line()
})
observeEvent(input$base,{
rVals[['baseline']]=input$rows
})
observeEvent(input$reset,{
rVals[['baseline']]=NULL
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(GG2)
ui Sweet感谢您的输入,您的解决方案可以很好地将基础数据存储为基线,并使用rVals对象添加。您认为有没有一种方法可以在renderPlot内绘图之前简单地将数据集添加到一起?这将允许我修改输入值(例如,在选定的数据集上运行函数)并将其存储为基础?即,在输入$行和绘图之间有几个计算步骤。可能在RVAL[['baseline']]之间有一行代码=NULL和output$plot感谢Peter在此主题上提供的所有帮助,被动输入会很好地工作!我在这个主题上的最后一个问题。如果我在管道的早期将输入与数据集断开,您的解决方案会是什么样子?我已经使用您的解决方案和我的目标包含了相同add的修改版本oment它只会在控制台中循环一个错误。提前感谢您的所有时间和建议。在Shining函数中,反应、观察、观察、事件反应和renderXYZ会对函数中的输入做出响应(如果存在),区别在于对它们进行评估时。有一些关于Shining Responsive environment的主题需要阅读以掌握更多信息在我的示例中,rVals[['baseline']]是一个辅助变量,它假定为空状态或输入$rows中的值以帮助筛选主数据,您不应该将表分配给它。今天晚些时候我将查看您的代码并提出解决方案感谢@PeterS!我示例中的代码的后半部分使用reactiveObject遵循您的建议。它似乎可以工作,直到compa按下“重新操作”按钮。当这种情况发生时,图形变暗。这表明rVal[[Final
]]工作,直到它与基线和输入数据合并,然后产生问题。
library(shiny)
library(ggplot2)
ui <- fluidPage(
selectInput("rows", label = h3("Choose your species"),
choices = list("setosa", "versicolor", "virginica")
),
actionButton("base", "Create baseline"),
actionButton("reset", "Reset baseline"),
plotOutput(outputId = "plot")
) # close fluid page
server <- function(input, output) {
rVals = reactiveValues()
rVals[['data']] = iris
rVals[['baseline']] = NULL
output$plot <- renderPlot({
# here we duplicate table to manipulate it before rendering
# the reason for duplicate is that you dont want to affect your
# base data as it may be used elsewhere
# note that due to R's copy-on-write this may be expensive operation and
# have impact on app performance
# in all cases using data.table package is recommended to mitigate
# some of the CoW implications
render.data = rVals[['data']][rVals[['data']][['Species']] %in% c(rVals[['baseline']],input$rows),]
# here manipulate render.data
# and then continue with plot
ggplot(data=render.data,
aes(x=Petal.Width, y = as.numeric(Sepal.Width), colour = Species,group=Species)
) +
labs(x="Hypothetical X", y="Hypothetical X") +
geom_line()
})
observeEvent(input$base,{
rVals[['baseline']]=input$rows
})
observeEvent(input$reset,{
rVals[['baseline']]=NULL
})
}
shinyApp(ui, server)