Shiny不会在应用程序中显示结果,但结果会在控制台中显示

时间:2019-12-13 16:35:14

标签: r shiny

我创建了一个R Shiny应用程序,以产生k均值聚类结果。该应用程序未产生结果。但是,它在控制台中显示输出。此外,在Rmarkdown中编织时,它也可以正常工作。如果您对代码部分有任何意见或任何其他建议来解决此问题。让我知道。

这是我的代码。

library(shiny)
library(dplyr)
library(cluster)



ui <- pageWithSidebar(
  headerPanel("Cluster Analysis"),
  sidebarPanel(
    fileInput('file1', 'Choose CSV File',
              accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
    tags$hr(),
    checkboxInput('header', 'Header', TRUE),
    helpText("You will be able to see the variable name after you add in a datafile"),
    fluidRow(
      column(6,checkboxGroupInput("variable","Select Variables:", c("1"="1","2"="2")))
    ),
    sliderInput("kvalue", "Select number of clusters", 
                value = 3, min = 3, max= 6),
    radioButtons('sep', 'Separator',
                 c(Comma=',', Semicolon=';',Tab='\t'), ','),
        uiOutput("choose_columns")
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Result", verbatimTextOutput("result")),
      tabPanel("Data", tableOutput('contents'))
    )
  )
)
server <- function(input, output,session) {
  dsnames <- c()

  data_set <- reactive({
    inFile <- input$file1

    if (is.null(inFile))
      return()

    data_set <-read.csv(inFile$datapath, header=input$header, 
                       sep=input$sep)
  })

  output$contents <- renderTable({data_set()})

  observe({
    dsnames <- names(data_set())
    cb_options <- list()
    cb_options[ dsnames] <- dsnames

    updateCheckboxGroupInput(session, "variable",
                             label = "Select Variables",
                             choices = cb_options,
                             selected = "")
  })
  output$choose_dataset <- renderUI({
    selectInput("dataset", "Data set", as.list(data_sets))
  })

  dfInput <- reactive({
    data <- data_set()

    if(is.null(data))
      return()

    data %>%
      dplyr::select(input$variable)
    }
)
  output$result <- renderPrint(
    {
      dfin <- dfInput()
      gp <- NULL

      if (!is.null(dfin)){
            df <- dfin
            test <- na.omit(df)
            test1 <- scale(test)
            test2 <- daisy(test1)
            seg.k <- kmeans(test2, centers=input$kvalue, nstart=25)
            df$segment <- seg.k$cluster 
            test2 <- df%>%
            dplyr::group_by(segment)%>%
            dplyr::summarise_all(list(mean))
            print(test2)
      }
      return(gp)
    }
  )
  output$choose_columns <- renderUI({

    if(is.null(input$dataset))
      return()
    colnames <- names(contents)
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  }) 
}
shinyApp(ui, server)

0 个答案:

没有答案