[英]Shiny reactivity with unused argument
我是R / Shiny的新手,正努力让自己了解反应性。 到目前为止, 这篇文章和这段视频都是有用的资源,但是我碰到了一些我不太了解的东西。
这是我正在运行的代码的简化版本。
library(shiny)
some_func <- function(some_input, unused_argument) {
print("Running - you changed something")
if (some_input) {1} else {-1}
}
ui <- fluidPage(
checkboxInput("negative", "Clicking makes all values negative"),
checkboxInput("negative_pt2", "I thought clicking would not change results
other than writing to the console,
but it does not even do that."),
plotOutput("graphs")
)
server <- function(input, output) {
library(ggplot2)
output$graphs <- renderPlot({
dataset2 <- reactive({mtcars * some_func(input$negative, input$negative_pt2)})
ggplot(dataset2(), aes(x = mpg, y = hp)) + geom_line()
})
}
shinyApp(ui = ui, server = server)
选中第一个框具有预期结果。 但是在检查第二个控制台时查看控制台表明Shiny足够聪明,可以意识到我未使用的参数是无用的。 很好,但是怎么知道呢?
谢谢!
您没有在函数中调用第二个参数,因此它不会做任何事情
我可以编写一个函数,要求用户输入一百万个变量,但是如果我不将其称为函数本身,就永远不会使用它们
some_func <- function(some_input, unused_argument) {
print("Running - you changed something")
if (some_input) {1} else {-1}
}
.....
some_func(input$negative, input$negative_pt2)
是的,该变量被放置在函数中未使用的参数位置,但是由于它从未被使用过,因此它只是存在。
如果我们更改函数以使用其收集的变量执行某项操作,那么我们将进行更改。
some_func <- function(some_input, unused_argument) {
print("Running - you changed something")
if (some_input) {1} else {**unused_argument**}
}
Shiny正在为negative_pt2分配一个值,但是您永远不会说:“嘿,根据该变量执行某些操作”,因此它就摆在了周围。
故事的寓意,您不仅需要接受变量,还需要在函数中使用变量以使它们发挥作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.