Shiny tableOutput: 'Error: could not find function "df"'

Shiny tableOutput: 'Error: could not find function "df"'

当我尝试将 tableOutput 或 DTOutput 编码到我的 Shiny 应用程序中时,我得到 'Error: could not find function "daysSince10"'。

我试过 validate(need()) 和 require()。知道为什么这不起作用吗?

library(shiny)
library(tidyverse)

daysSince10 <- read_csv("https://raw.githubusercontent.com/joegoodman94/CoronavirusTracker/master/days10.csv")

ui <- fluidPage(
  titlePanel("Coronavirus Tracker"),
  sidebarLayout(
    sidebarPanel(selectInput('Country', 'Select Country', multiple = T, unique(daysSince10$`Country`))),
    mainPanel(
      tabsetPanel(
        tabPanel("Plot", plotly::plotlyOutput('trend')),
        tabPanel("Table", DT::DTOutput('table'))
      )
    )
  )
)

server <- function(input, output, session) {
  observe({
    moddays <- daysSince10[daysSince10$`Country` %in% input$Country,]
    output$trend <- plotly::renderPlotly({
      validate(
        need(input$Country, "please select a country")
      ) 
      ggplot(moddays) +
        geom_line(aes(x = `Days since tenth death`, y = `Total Deaths`, color = `Country`)) +
        scale_y_log10()
    })
    output$table <- DT::renderDT({
      validate(
        need(input$Country, "please select a country")
      )      
      daysSince10()
    })
  })
}

shinyApp(ui = ui, server = server)

不写daysSince10(),写daysSince10不带括号。 由于它们,R 查找名为 "daysSince10" 的函数,该函数不存在。

output$table <- DT::renderDT({
  validate(
    need(input$Country, "please select a country")
  )      
  daysSince10
})

output$XXX <- renderYYY() 赋值通常应该在 observe().

之外

output$table 是指依赖于 moddays 而不是 daysSince10 吗?假设是这样:

server <- function(input, output, session) {
  moddays <- reactive({
    daysSince10[daysSince10$`Country` %in% input$Country,]
  })

  output$trend <- plotly::renderPlotly({
    validate(
      need(input$Country, "please select a country")
    ) 
    ggplot(moddays()) +
      geom_line(aes(x = `Days since tenth death`, y = `Total Deaths`, color = `Country`)) +
      scale_y_log10()
  })

  output$table <- DT::renderDT({
    validate(
      need(input$Country, "please select a country")
    )      
    moddays()
  })
}

使 moddays 成为其自身的反应式,现在应在后续代码中将其称为 moddays()(函数调用)