在闪亮的仪表板中打印表格的str()

时间:2018-03-13 04:23:07

标签: r shinydashboard

我是闪亮仪表板的新手。我想知道如何打印我在闪亮的仪表板中导入的表格的str()。我的代码无效。当我打印str()时,我得到以下输出,

str()

请检查我写的代码,

library(shiny)
library(shinydashboard)
library(DT)

ui <- dashboardPage(
  dashboardHeader(title = "Analytics Workbench 2.0", titleWidth = 250,
                  dropdownMenuOutput("msgs")),

  dashboardSidebar(
    sidebarMenu(
      fileInput("Table1", "Train Data"),
      fileInput("Table2", "Test Data"),
      menuItem("Variable Analysis", icon = icon("edit"),
        menuSubItem("Uni-Variate Analysis"),
        menuSubItem("Multi-Variate Analysis"))
    )
  ),
  dashboardBody(
    fluidRow(
      column(12, box(title = "Train Data", width = 6, solidHeader = TRUE, status = "warning", 
                     collapsible = TRUE, DT::DTOutput("Train")),
                 box(title = "Test Data", width = 6, solidHeader = TRUE, status = "warning", 
                     collapsible = TRUE, DT::DTOutput("Test")))),

    fluidRow(
      column(12, box(title = "Structure", width = 6, solidHeader = TRUE, status = "warning", 
                     collapsible = TRUE, verbatimTextOutput("str1")),
                 box(title = "Structure", width = 6, solidHeader = TRUE, status = "warning", 
                     collapsible = TRUE, verbatimTextOutput("str2"))))
  )
)


server <- function(input, output) { 

  output$msgs <- renderMenu({ 
    msg <- apply(read.csv("messages.csv"), 1, function(row){
      messageItem(from = row[["from"]], message = row[["message"]])   })
    dropdownMenu(type = "messages", .list = msg)
  })

  output$Train <- DT::renderDT({

    if (is.null(input$Table1)) return(NULL)


    data1 <- read.table(input$Table1$datapath, fill = TRUE, header=T, sep=",")
    DT::datatable(data1, options = list(scrollX = TRUE))
  })

  output$Test <- DT::renderDT({

    if (is.null(input$Table2)) return(NULL)

    data2 <- read.table(input$Table2$datapath, fill = TRUE, header=T, sep=",") 
    DT::datatable(data2, options = list(scrollX = TRUE))
  })

  output$str1 <- renderText({
    paste(capture.output(str(input$Table1)), collapse = "\n")
  })

  output$str2 <- renderText({
    paste(capture.output(str(input$Table1)), collapse = "\n")
  })

}

我无法找到str()

的输入

由于 巴拉吉

1 个答案:

答案 0 :(得分:1)

textOutput切换verbatimTextOutput。此外,您需要reactive来处理fileInput ...请注意,当输入值为 NULL 时,您应该捕获案例。

app.R

library(shiny)

write.csv(mtcars, "mtcars.csv") # file created to test file input

ui <- fluidPage(

  mainPanel(
    verbatimTextOutput("strfile"),
    fileInput("file1", "File")
  )
)

server <- function(input, output) {

  df <- reactive({

    if (is.null(input$file1)) {

      return(NULL)

    } else {

      read.csv(input$file1$datapath, row.names = 1) # note the row.names are dependent on your input requirements

    }

  })

  output$strfile <- renderPrint({str(df())})

}

shinyApp(ui = ui, server = server)

获得此输出......

enter image description here