【问题标题】:The output table did not display in R Shiny app输出表未显示在 R Shiny 应用程序中
【发布时间】:2022-01-15 18:58:51
【问题描述】:

我使用闪亮的应用程序编写了一个 R 脚本。该脚本应该读取任何输入的 CSV 文件并将其作为表格显示在闪亮的用户界面上。我附上了我的代码。当我运行脚本时,我可以选择 s CSV 文件,但输出表不显示。你能帮帮我吗?

library(shiny)
library(DT)
ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
       # tableOutput('dto')
      dataTableOutput('dto'),
    )
  )
)

server <- function(input,output){
  # Reactive expression with the data, in this case iris
#----------------
  output$contents <- reactive({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
    read.csv(inFile$datapath, header = input$header),
    
  })
#----------------
  
  #the extensions parameter coupled with the options list does the trick  
  output$dto <- renderDataTable(contents(), extensions = 'Buttons', 
                                options = list(dom = 'Bfrtip',
                                               buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))
  )
}

runApp(list(ui=ui,server=server), launch.browser=TRUE) #now runs by default in the external browser.

【问题讨论】:

  • contents 不应该放在 output 中,它是反应式的意思是你总是在没有改变的情况下访问它,它不会被写在任何地方。
  • 谢谢您,我根据您的评论更正了代码。它还不起作用。你能写出更正的代码作为答案吗?谢谢
  • 添加了下面的样子

标签: r shiny shinydashboard


【解决方案1】:

reactivityprinciples 指定我们不需要向output 添加反应组件。对于contents,我们只需将其设为reactive 并在需要时访问它。

library(shiny)
library(DT)
?runApp
ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
      # tableOutput('dto')
      dataTableOutput('dto'),
    )
  )
)

server <- function(input,output){
  # Reactive expression with the data, in this case iris
  #----------------
  contents <- reactive({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1
    
    if (is.null(inFile)) return(NULL)
    
    read.csv(inFile$datapath, header = input$header)
    
  })
  #----------------
  
  #the extensions parameter coupled with the options list does the trick  
  output$dto <- renderDataTable(contents(), extensions = 'Buttons', 
                                options = list(dom = 'Bfrtip',
                      buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))
  )
}

runApp(list(ui=ui,server=server), launch.browser=TRUE) 

【讨论】:

  • 在我真正看到关键区别之前,我不得不来回翻转几次。对于那些可能无法从措辞中了解 NelsonGon 观点的人,请将 output$contents &lt;- reactive(...) 更改为 contents &lt;- reactive(...),只需删除 output$
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 2018-02-27
  • 2016-10-26
  • 2021-02-05
  • 1970-01-01
  • 2016-05-10
相关资源
最近更新 更多