Shiny tableOutput: '错误:找不到函数 "df"'

问题描述 投票:0回答:2

当我试图在Shiny应用程序中编写tableOutput或DTOutput的代码时,我得到'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)
r shiny dt
2个回答
1
投票

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() (函数调用)


0
投票

不要写 daysSince10()daysSince10 由于它们的存在,R寻找一个名为 "daysSince10 "的函数,而这个函数并不存在。

output$table <- DT::renderDT({
  validate(
    need(input$Country, "please select a country")
  )      
  daysSince10
})
© www.soinside.com 2019 - 2024. All rights reserved.