如何在闪亮中创建加载事件或默认事件?
How to create On load Event or default event in shiny?
我是 shiny 和 Whosebug 的新手,正在寻求一些帮助来解决我目前遇到的问题。
我正在尝试构建一个闪亮的应用程序,它收集用户的一些输入并根据单击按钮的输入创建可视化。目前,这工作正常,但一个主要的问题是,当应用程序第一次加载时,它应该根据默认输入准备好可视化。
我正在粘贴示例代码,它可以解释我面临的问题:
UI.R
#loading shiny
library(shiny)
ui<-shinyUI(fluidPage(
titlePanel("Iris Dataset"),
sidebarLayout(
sidebarPanel(
radioButtons("x", "Select X-axis:",
list("Sepal.Length"='a', "Sepal.Width"='b', "Petal.Length"='c', "Petal.Width"='d')),
radioButtons("y", "Select Y-axis:",
list("Sepal.Length"='e', "Sepal.Width"='f', "Petal.Length"='g', "Petal.Width"='h')),
actionButton("action","Submit")
),
mainPanel(
plotOutput("distPlot")
)
)
))
#SERVER.R
library(shiny)
#loading shiny
server<-shinyServer(function(input, output) {
observeEvent(input$action,{
output$distPlot <- renderPlot({if(input$x=='a'){i<-1}
if(input$x=='b'){i<-2}
if(input$x=='c'){i<-3}
if(input$x=='d'){i<-4}
if(input$y=='e'){j<-1}
if(input$y=='f'){j<-2}
if(input$y=='g'){j<-3}
if(input$y=='h'){j<-4}
s <- iris[, i]
k <- iris[, j]
plot(s,k)
})
})
})
shinyApp(ui<-ui, server<-server)
现在,如果您 运行 这个应用程序,您会看到在加载后的第一个屏幕上选择了输入(这是需要的),但只有在单击“提交”按钮后才会显示可视化,这是因为观察者事件。 在实际的应用程序中,在单击按钮时捕获输入后会执行计算。因此,计算仅在单击 actionButton 时触发
有没有办法,我们仍然可以保留“提交”按钮及其观察事件,但在开始时即在应用加载时自动触发观察事件内执行的可视化或操作第一次。
在 observeEvent
中使用 renderPlot
或其他渲染函数被认为是不好的做法。您正在寻找的是一个代表绘图参数的反应对象。然后,您可以在 renderPlot
上下文中访问此反应对象。这是一个例子
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Iris Dataset"),
sidebarLayout(
sidebarPanel(
radioButtons("x", "Select X-axis:",
list("Sepal.Length" = 'a', "Sepal.Width" = 'b',
"Petal.Length" = 'c', "Petal.Width" = 'd')),
radioButtons("y", "Select Y-axis:",
list("Sepal.Length" = 'e', "Sepal.Width" = 'f',
"Petal.Length" = 'g', "Petal.Width" = 'h')),
actionButton("action", "Submit")
),
mainPanel(
plotOutput("distPlot")
)
)
))
server <- shinyServer(function(input, output) {
user_choice <- eventReactive(input$action,{
if (input$x == 'a'){i <- 1}
if (input$x == 'b'){i <- 2}
if (input$x == 'c'){i <- 3}
if (input$x == 'd'){i <- 4}
if (input$y == 'e'){j <- 1}
if (input$y == 'f'){j <- 2}
if (input$y == 'g'){j <- 3}
if (input$y == 'h'){j <- 4}
return(c(i, j))
## use ignoreNULL to fire the event at startup
}, ignoreNULL = FALSE)
output$distPlot <- renderPlot({
uc <- user_choice()
plot(iris[, uc[1]], iris[, uc[2]])
})
})
shinyApp(ui = ui, server = server)
只要用户单击按钮 或 应用程序启动,对象 user_choice
就会更新。
observeEvent
中还有一个 ignoreNULL
参数,但由于某些原因,它不会产生相同的结果。
我是 shiny 和 Whosebug 的新手,正在寻求一些帮助来解决我目前遇到的问题。 我正在尝试构建一个闪亮的应用程序,它收集用户的一些输入并根据单击按钮的输入创建可视化。目前,这工作正常,但一个主要的问题是,当应用程序第一次加载时,它应该根据默认输入准备好可视化。
我正在粘贴示例代码,它可以解释我面临的问题:
UI.R
#loading shiny
library(shiny)
ui<-shinyUI(fluidPage(
titlePanel("Iris Dataset"),
sidebarLayout(
sidebarPanel(
radioButtons("x", "Select X-axis:",
list("Sepal.Length"='a', "Sepal.Width"='b', "Petal.Length"='c', "Petal.Width"='d')),
radioButtons("y", "Select Y-axis:",
list("Sepal.Length"='e', "Sepal.Width"='f', "Petal.Length"='g', "Petal.Width"='h')),
actionButton("action","Submit")
),
mainPanel(
plotOutput("distPlot")
)
)
))
#SERVER.R
library(shiny)
#loading shiny
server<-shinyServer(function(input, output) {
observeEvent(input$action,{
output$distPlot <- renderPlot({if(input$x=='a'){i<-1}
if(input$x=='b'){i<-2}
if(input$x=='c'){i<-3}
if(input$x=='d'){i<-4}
if(input$y=='e'){j<-1}
if(input$y=='f'){j<-2}
if(input$y=='g'){j<-3}
if(input$y=='h'){j<-4}
s <- iris[, i]
k <- iris[, j]
plot(s,k)
})
})
})
shinyApp(ui<-ui, server<-server)
现在,如果您 运行 这个应用程序,您会看到在加载后的第一个屏幕上选择了输入(这是需要的),但只有在单击“提交”按钮后才会显示可视化,这是因为观察者事件。 在实际的应用程序中,在单击按钮时捕获输入后会执行计算。因此,计算仅在单击 actionButton 时触发
有没有办法,我们仍然可以保留“提交”按钮及其观察事件,但在开始时即在应用加载时自动触发观察事件内执行的可视化或操作第一次。
在 observeEvent
中使用 renderPlot
或其他渲染函数被认为是不好的做法。您正在寻找的是一个代表绘图参数的反应对象。然后,您可以在 renderPlot
上下文中访问此反应对象。这是一个例子
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Iris Dataset"),
sidebarLayout(
sidebarPanel(
radioButtons("x", "Select X-axis:",
list("Sepal.Length" = 'a', "Sepal.Width" = 'b',
"Petal.Length" = 'c', "Petal.Width" = 'd')),
radioButtons("y", "Select Y-axis:",
list("Sepal.Length" = 'e', "Sepal.Width" = 'f',
"Petal.Length" = 'g', "Petal.Width" = 'h')),
actionButton("action", "Submit")
),
mainPanel(
plotOutput("distPlot")
)
)
))
server <- shinyServer(function(input, output) {
user_choice <- eventReactive(input$action,{
if (input$x == 'a'){i <- 1}
if (input$x == 'b'){i <- 2}
if (input$x == 'c'){i <- 3}
if (input$x == 'd'){i <- 4}
if (input$y == 'e'){j <- 1}
if (input$y == 'f'){j <- 2}
if (input$y == 'g'){j <- 3}
if (input$y == 'h'){j <- 4}
return(c(i, j))
## use ignoreNULL to fire the event at startup
}, ignoreNULL = FALSE)
output$distPlot <- renderPlot({
uc <- user_choice()
plot(iris[, uc[1]], iris[, uc[2]])
})
})
shinyApp(ui = ui, server = server)
只要用户单击按钮 或 应用程序启动,对象 user_choice
就会更新。
observeEvent
中还有一个 ignoreNULL
参数,但由于某些原因,它不会产生相同的结果。