R Shiny仪表盘开发报错argument "expr" is missing问题咨询
报错原因
你遇到的argument "expr" is missing, with no default报错,核心是误用了Shiny的eventReactive()函数:
eventReactive()是Shiny用于定义事件触发响应式变量的函数,标准语法为eventReactive(触发条件, 要执行的表达式),第二个参数expr就是你缺失的必填参数,用来指定事件触发后要运行的代码逻辑。- 你在绘图代码里写的
dataset[eventReactive(input$sip)]完全是错误用法,既没有给eventReactive传入第二个参数,也不需要用这个函数来取输入控件的值。
修复步骤
除了修正eventReactive的错误用法,你的代码还有两处可优化的逻辑问题,一并修复:
- 统一数据读取逻辑:不要在每个输出渲染函数里重复读取CSV,单独定义一个响应式数据集,复用读取结果
- 调整下拉框更新逻辑:不要把
updateSelectInput放在renderPlot里,否则只有绘图时才会更新下拉选项,应该放到observe里,数据集变化时自动更新下拉 - 修正绘图取值逻辑:直接通过输入控件的值取数据集对应列即可,同时补充校验保证下拉选好值再绘图
修正后完整代码
library(shiny) library(shinydashboard) ui <- dashboardPage(skin='red', dashboardHeader(title = "Dashboard"), ## Sidebar content dashboardSidebar( sidebarMenu( menuItem("Data", tabName = "dashboard"), menuItem("Visualization", tabName = "viz", menuSubItem("Summary",tabName="sum"), menuSubItem("Plot",tabName = "plot")), menuItem("Interpretation", tabName = "int") ) ), ## Body content dashboardBody( tabItems( # First tab content tabItem(tabName = "dashboard", fluidPage(titlePanel("DATA"), fluidRow( column(width=4, fileInput("file1", "Input CSV file", multiple = TRUE, accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv"))), column(width=2,checkboxInput("header", "Header", TRUE)), column(width=2,radioButtons("sep","Separator", choices = c(Comma=",", Semicolon = ";", Tab = "\t"), selected = ",")), column(width=2,radioButtons("quote", "Quote", choices = c(None = "", "Double Quote" = '"', "Single Quote" = "'"), selected = '"')), column(width=2,radioButtons("disp", "Display", choices = c(Head = "head", All = "all"), selected = "head")) ), tableOutput("contents") )), # Second tab content tabItem(tabName = "sum", box( title="Summary", status="primary", solidHeader = TRUE, width = "100%", collapsible = TRUE, verbatimTextOutput("summary") ) ), tabItem(tabName="plot", h2("Plot"), fluidRow( selectInput("sip","X轴字段",choices = NULL), selectInput("sila","Y轴字段",choices = NULL) ), fluidRow( box( title = "Plot A", status= "primary", solidHeader = TRUE, collapsible = TRUE, plotOutput("plota",height="300px") ), box( title = "Plot B", status= "primary", solidHeader = TRUE, collapsible = TRUE, plotOutput("plotb",height="300px") ) ) )), tabItem(tabName = "int", h2("hello") ) ) ) ) server <- function(input, output,session) { # 统一的响应式数据集,只读取一次CSV reactive_df <- reactive({ req(input$file1) read.csv(input$file1$datapath, header = input$header, sep = input$sep, quote = input$quote) }) # 数据集变化时自动更新下拉选项 observe({ req(reactive_df()) updateSelectInput(session,"sip",label="X轴字段",choices=names(reactive_df())) updateSelectInput(session,"sila",label="Y轴字段",choices = names(reactive_df())) }) output$contents <- renderTable({ df <- reactive_df() if(input$disp == "head") { return(head(df)) } else { return(df) } }) output$summary <- renderPrint({ summary(reactive_df()) }) output$plota <- renderPlot({ req(reactive_df(), input$sip, input$sila) df <- reactive_df() # 直接用输入值取对应列绘图 plot(df[[input$sip]], df[[input$sila]], pch=19, xlab = input$sip, ylab = input$sila) }) output$plotb <- renderPlot({ # 可自行补充Plot B的逻辑 }) } shinyApp(ui, server)
内容的提问来源于stack exchange,提问作者c.jungc
相关产品推荐
相关产品推荐

