r - 输出表未显示在 R Shiny 应用程序中

标签 r shiny shinydashboard

我使用 Shiny 的应用程序编写了一个 R 脚本。该脚本应该读取任何输入 CSV 文件并将其显示在 Shiny 的用户界面上作为表格。我附上了我的代码。当我运行脚本时,我可以选择 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.

最佳答案

principles reactivity 指定我们不需要向 output 添加 react 组件。对于内容,我们只需将其设置为响应式并在需要时访问它。

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) 

关于r - 输出表未显示在 R Shiny 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70315436/

相关文章:

r - R Shiny 应用程序的本地版本和shinyapps.io 版本之间的颜色不同

r - ggplot 与来自数据表 (DT) 的过滤数据和 react 性小部件来选择 x 和 y 轴

css - 盒子之间 Shiny 的仪表板边距

html - 删除 Shiny 应用程序标题中的 div

r - 如何在 Shiny 的仪表板应用程序中包含远程 JavaScript 文件?

r - NetLogo - 与导入的 GIS shapefile 不一致

将数组 reshape 为距离矩阵(在 R 中)

r - 在同一个 Shiny 的应用程序中使用 shinyjs 和 ggplot2::autoplot

r - 独立的 react 在 Shiny 的应用程序中进行交互

r - 在 R 中添加对线