R 闪亮操作按钮第二次不起作用
R shiny action button doesnt work a second time
我在 R shiny 中创建了以下应用程序
library(shiny)
library(data.table)
导入库后,我们现在创建 UI 如下
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
这将创建一个 ui,其中包含一个数字输入和两个按钮,一个是开始按钮,另一个是重置按钮
接下来我们创建服务器和 运行 应用程序
server <- function(input, output, session) {
table_output<-eventReactive(input$goButton,{ ##### WE CREATE THE TABLE OBJECT HERE
table_output<-data.frame("SLNO"= seq(1:input$n))
table_output$Col2<-table_output$SLNO^2
return(table_output)})
output$all<-renderDataTable({
table_output()})
observeEvent(input$Reset, { #### WE EMPTY THE PLOT AREA HERE
output$all <- renderDataTable({ })})}
shinyApp(ui = ui, server = server)
table 是在我按下 Go 按钮时生成的。然而,在按下 rest 按钮后,table 尽管更改了输入,但仍拒绝呈现。我在这里请求一些帮助。
检查这是否适合您:
library(shiny)
library(data.table)
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
server <- function(input, output, session) {
table_output <- eventReactive({input$goButton}, data.frame("SLNO" = seq(1:input$n), "Col2" = seq(1:input$n)^2))
observeEvent(input$goButton, {
output$all <- renderDataTable({
table_output()})
})
observeEvent(input$Reset, {
output$all <- renderDataTable({ })})}
shinyApp(ui = ui, server = server)
在这种情况下,我更喜欢使用 observeEvent
和 reactiveValues
。
library(shiny)
library(data.table)
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
server <- function(input, output, session) {
table_output <- reactiveValues(df = data.frame(SLNO = as.numeric(), Col2 = as.numeric()))
observeEvent(input$goButton, {
temp <- table_output$df
temp <- data.frame("SLNO"= seq(1:input$n))
temp$Col2 <- temp$SLNO^2
table_output$df <- temp
})
observeEvent(input$goButton, {
if(nrow(table_output$df) > 1) {
output$all<-renderDataTable({
table_output$df})
}
}
)
observeEvent(input$Reset, { #### WE EMPTY THE PLOT AREA HERE
table_output <- reactiveValues(df = data.frame(SLNO = as.numeric(), Col2 = as.numeric()))
output$all<-renderDataTable({})
})
}
shinyApp(ui = ui, server = server)
我在 R shiny 中创建了以下应用程序
library(shiny)
library(data.table)
导入库后,我们现在创建 UI 如下
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
这将创建一个 ui,其中包含一个数字输入和两个按钮,一个是开始按钮,另一个是重置按钮
接下来我们创建服务器和 运行 应用程序
server <- function(input, output, session) {
table_output<-eventReactive(input$goButton,{ ##### WE CREATE THE TABLE OBJECT HERE
table_output<-data.frame("SLNO"= seq(1:input$n))
table_output$Col2<-table_output$SLNO^2
return(table_output)})
output$all<-renderDataTable({
table_output()})
observeEvent(input$Reset, { #### WE EMPTY THE PLOT AREA HERE
output$all <- renderDataTable({ })})}
shinyApp(ui = ui, server = server)
table 是在我按下 Go 按钮时生成的。然而,在按下 rest 按钮后,table 尽管更改了输入,但仍拒绝呈现。我在这里请求一些帮助。
检查这是否适合您:
library(shiny)
library(data.table)
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
server <- function(input, output, session) {
table_output <- eventReactive({input$goButton}, data.frame("SLNO" = seq(1:input$n), "Col2" = seq(1:input$n)^2))
observeEvent(input$goButton, {
output$all <- renderDataTable({
table_output()})
})
observeEvent(input$Reset, {
output$all <- renderDataTable({ })})}
shinyApp(ui = ui, server = server)
在这种情况下,我更喜欢使用 observeEvent
和 reactiveValues
。
library(shiny)
library(data.table)
ui <- fluidPage(
pageWithSidebar(
headerPanel("TestApp"),
sidebarPanel(
numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
br(),
actionButton(inputId = "goButton", label = "Go!"),
actionButton(inputId = "Reset",label = "Reset")),
mainPanel(dataTableOutput(outputId = "all") ) ))
server <- function(input, output, session) {
table_output <- reactiveValues(df = data.frame(SLNO = as.numeric(), Col2 = as.numeric()))
observeEvent(input$goButton, {
temp <- table_output$df
temp <- data.frame("SLNO"= seq(1:input$n))
temp$Col2 <- temp$SLNO^2
table_output$df <- temp
})
observeEvent(input$goButton, {
if(nrow(table_output$df) > 1) {
output$all<-renderDataTable({
table_output$df})
}
}
)
observeEvent(input$Reset, { #### WE EMPTY THE PLOT AREA HERE
table_output <- reactiveValues(df = data.frame(SLNO = as.numeric(), Col2 = as.numeric()))
output$all<-renderDataTable({})
})
}
shinyApp(ui = ui, server = server)