使用 eventReactive 找不到对象错误
Object not found error using eventReactive
我有这个简单的 Shiny 应用程序,我希望用户在其中选择四个数据帧——即 df1、df2、df3 和 df4——将使用 DT:dataTableOutput 显示的那个。问题是我得到这个错误:
dots_list(...) 中出错:找不到对象 'df_opt'
我也不知道为什么。我认为错误消息的触发是因为与我之前在 eventReactive 中的 input$df_opt 参数有关,但后来我通过添加按钮输入将其删除。然而,我不断收到相同的错误消息
有人知道我为什么会收到这条消息吗?
这是我的应用程序的可重现示例:
library(shiny)
library(DT)
choices_df <- c("opt1", "opt2", "opt3", "opt4")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(
inputId = df_opt,
label = "Choose a database",
choices = df_choices,
selected = choices_df[1] ),
actionButton(
inputId = df_button,
label = "")
),
mainPanel(
DT::dataTableOutput("base_p1")
)
))
server <- function(input, output) {
df_selected <- eventReactive(
input$df_button, {
if( input$df_opt == choices_df[1] ){
df1
}
if( input$df_opt == choices_df[2] ){
df2
}
if( input$df_opt == choices_df[3] ){
df3
}
if( input$df_opt == choices_df[4] ){
df4
}
})
output$base_p1 <- DT::renderDataTable( df_selected(), filter = "top")
}
shinyApp(ui = ui, server = server)
这是您想要执行的操作的工作版本,但带有 reactiveValues。所以基本上我发现最好为反应变量创建一个空的容器,然后在用户更改输入时将想要的 df 分配给它。代码:
library(shiny)
library(DT)
df_choices <- c("opt1", "opt2", "opt3", "opt4")
df1 <- matrix(rnorm(100,10,10),10,10)
df2 <- matrix(rnorm(100,100,10),10,10)
df3 <- matrix(rnorm(100,1099,10),10,10)
df4 <- matrix(rnorm(100,100000,10),10,10)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(
inputId = "df_opt",
label = "Choose a database",
choices = df_choices,
selected = choices_df[1] ),
actionButton(
inputId = "df_button",
label = "click to show")
),
mainPanel(
DT::dataTableOutput("base_p1")
)
))
server <- function(input, output) {
df <- reactiveValues(df=df1)
observeEvent(input$df_button,{
if( input$df_opt == choices_df[1] ){
df$df <- df1
}
if( input$df_opt == choices_df[2] ){
df$df <-df2
}
if( input$df_opt == choices_df[3] ){
df$df <- df3
}
if( input$df_opt == choices_df[4] ){
df$df <- df4
}
})
output$base_p1 <- DT::renderDataTable( df$df, filter = "top")
}
shinyApp(ui = ui, server = server)
编辑:是的,inputID 应该是字符串,所以 "df_opt" 和 "df_button"。
我有这个简单的 Shiny 应用程序,我希望用户在其中选择四个数据帧——即 df1、df2、df3 和 df4——将使用 DT:dataTableOutput 显示的那个。问题是我得到这个错误:
dots_list(...) 中出错:找不到对象 'df_opt'
我也不知道为什么。我认为错误消息的触发是因为与我之前在 eventReactive 中的 input$df_opt 参数有关,但后来我通过添加按钮输入将其删除。然而,我不断收到相同的错误消息
有人知道我为什么会收到这条消息吗?
这是我的应用程序的可重现示例:
library(shiny)
library(DT)
choices_df <- c("opt1", "opt2", "opt3", "opt4")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(
inputId = df_opt,
label = "Choose a database",
choices = df_choices,
selected = choices_df[1] ),
actionButton(
inputId = df_button,
label = "")
),
mainPanel(
DT::dataTableOutput("base_p1")
)
))
server <- function(input, output) {
df_selected <- eventReactive(
input$df_button, {
if( input$df_opt == choices_df[1] ){
df1
}
if( input$df_opt == choices_df[2] ){
df2
}
if( input$df_opt == choices_df[3] ){
df3
}
if( input$df_opt == choices_df[4] ){
df4
}
})
output$base_p1 <- DT::renderDataTable( df_selected(), filter = "top")
}
shinyApp(ui = ui, server = server)
这是您想要执行的操作的工作版本,但带有 reactiveValues。所以基本上我发现最好为反应变量创建一个空的容器,然后在用户更改输入时将想要的 df 分配给它。代码:
library(shiny)
library(DT)
df_choices <- c("opt1", "opt2", "opt3", "opt4")
df1 <- matrix(rnorm(100,10,10),10,10)
df2 <- matrix(rnorm(100,100,10),10,10)
df3 <- matrix(rnorm(100,1099,10),10,10)
df4 <- matrix(rnorm(100,100000,10),10,10)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(
inputId = "df_opt",
label = "Choose a database",
choices = df_choices,
selected = choices_df[1] ),
actionButton(
inputId = "df_button",
label = "click to show")
),
mainPanel(
DT::dataTableOutput("base_p1")
)
))
server <- function(input, output) {
df <- reactiveValues(df=df1)
observeEvent(input$df_button,{
if( input$df_opt == choices_df[1] ){
df$df <- df1
}
if( input$df_opt == choices_df[2] ){
df$df <-df2
}
if( input$df_opt == choices_df[3] ){
df$df <- df3
}
if( input$df_opt == choices_df[4] ){
df$df <- df4
}
})
output$base_p1 <- DT::renderDataTable( df$df, filter = "top")
}
shinyApp(ui = ui, server = server)
编辑:是的,inputID 应该是字符串,所以 "df_opt" 和 "df_button"。