代码之家  ›  专栏  ›  技术社区  ›  Alex Yahiaoui Martinez

使用actionbutton上载csv文件并显示corrplot

  •  1
  • Alex Yahiaoui Martinez  · 技术社区  · 7 年前

    我尝试使用actionbutton()和updateSelectizeInput()设置相关图

    但是发生了一个错误:

    有人有办法吗?谢谢

    library(shiny)
    library(readr)
    library(corrplot)
    library(DT)
    
    
    # File used for the example
    data(iris)
    write.csv(x = iris, file = "iris.csv")
    
    
    #UI
    ui <- shinyUI(
      fluidPage(
        navbarPage(
          id = "navbar",
          tabPanel(
            title = "UPLOAD",
            br(),
            actionButton(inputId = "file", label = "ADD A FILE")
          )
        )
      )
    )
    
    #SERVER
    server <- function(input, output, session) {
    
      path <- reactiveValues(pth = NULL)
    
      file.choose2 <- function(...) {
        pathname <- NULL;
        tryCatch({
          pathname <- file.choose();
        }, error = function(ex) {
        })
        pathname;
      }
    
      observeEvent(input$file,{
        path$pth <- file.choose2()
      })
    
      observeEvent(input$file,  {
        newvalue <- "B"
        updateNavbarPage(session, "navbar", newvalue)
      })
    
      data <- reactive({
            df <- readr::read_csv(file = path$pth)
            return(df)
      })
    
      observeEvent(input$file,  {
        appendTab(
          inputId = "navbar",
          tabPanel(
            value = "B",
            title = "Corr",
            sidebarLayout(
              sidebarPanel(
                selectizeInput(
                  inputId = "select04",
                  label = "Select features",
                  choices = NULL,
                  multiple = TRUE)
              ),
              mainPanel(
                plotOutput(
                  outputId = "corrplot01", height = "650px")
              )
            )
          )
        )
      }, once = TRUE)
    
      # I suppose there is a problem with this line
      observeEvent(input$select04, { 
        col <- names(data())
        col.num <- which(sapply(data(), class) == "numeric")
        col <- col[col.num]
        updateSelectizeInput(session = session, inputId = "select04", choices = col)
      })
    
      output$corrplot01 <- renderPlot({ 
        df <- data()
        df1 <- df[,input$select04]
        corr <- cor(x = df1, use  = "pairwise.complete.obs")
        corrplot(corr = corr, 
                 title = "")
      })
    }
    
    shinyApp(ui, server)
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   SeGa    7 年前

    我删除了 observeEvent(input$file, ...{}) 并直接在ui中添加ui部分。

    req() 电话 data 反应性,第二种 observeEvent(input$select04, ...{}) observe renderPlot 呼叫

    library(shiny)
    library(readr)
    library(corrplot)
    library(DT)
    
    
    # File used for the example
    data(iris)
    write.csv(x = iris, file = "iris.csv", row.names = F)
    
    
    #UI
    ui <- shinyUI(
      fluidPage(
        navbarPage(
          id = "navbar",
          tabPanel(
            title = "UPLOAD",
            br(),
            actionButton(inputId = "file", label = "ADD A FILE"),
            tabPanel(
              value = "B",
              title = "Corr",
              sidebarLayout(
                sidebarPanel(
                  selectizeInput(width = "300px",
                    inputId = "select04",
                    label = "Select features",
                    choices = NULL,
                    multiple = TRUE)
                ),
                mainPanel(
                  plotOutput(
                    outputId = "corrplot01", height = "650px")
                )
              )
            )
          )
        )
      )
    )
    
    #SERVER
    server <- function(input, output, session) {
    
      path <- reactiveValues(pth = NULL)
    
      file.choose2 <- function(...) {
        pathname <- NULL;
        tryCatch({
          pathname <- file.choose();
        }, error = function(ex) {
        })
        pathname;
      }
    
      observeEvent(input$file,{
        path$pth <- file.choose2()
      })
    
      observeEvent(input$file,  {
        newvalue <- "B"
        updateNavbarPage(session, "navbar", newvalue)
      })
    
      data <- reactive({
        req(path$pth)
        df <- readr::read_csv(file = path$pth)
        return(df)
      })
    
    
      # I suppose there is a problem with this line
      observe({ 
        req(names(data()))
        col <- names(data())
        col.num <- which(sapply(data(), class) == "numeric")
        col <- col[col.num]
        updateSelectizeInput(session = session, inputId = "select04", choices = col)
      })
    
      output$corrplot01 <- renderPlot({ 
        req(input$select04)
        df <- data()
        df1 <- df[,input$select04]
        corr <- cor(x = df1, use  = "pairwise.complete.obs")
        corrplot(corr = corr, 
                 title = "")
      })
    }
    
    shinyApp(ui, server)
    
    推荐文章