【问题标题】:Upload a csv file with actionbutton and display a corrplot使用 actionbutton 上传 csv 文件并显示 corrplot
【发布时间】:2018-12-18 13:12:16
【问题描述】:

我尝试使用 R::shiny 制作一个 Web 应用程序,但我遇到了一段代码的问题。确实,我想上传一个 csv 文件并显示一个相关图。

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

但是发生了错误:

错误:不支持的索引类型:NULL

有人有解决办法吗?谢谢

NB - 我不想使用 fileInput 小部件上传 csv 文件!只能通过操作按钮!

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)

【问题讨论】:

    标签: r shiny r-corrplot


    【解决方案1】:

    我稍微改变了你的用户界面和服务器,但我认为这可能会解决你的问题。

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

    我还在 data 反应式中添加了 3 个 req() 调用,在第二个 observeEvent(input$select04, ...{}) 中我将其更改为普通的 observerenderPlot 调用。

    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)
    

    【讨论】:

    • 我很高兴它有帮助:)
    • 不错的世嘉!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2011-09-20
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多