Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用actionbutton上载csv文件并显示corrplot_R_Shiny_R Corrplot - Fatal编程技术网

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

使用actionbutton上载csv文件并显示corrplot,r,shiny,r-corrplot,R,Shiny,R Corrplot,我试图用R::Shining创建一个web应用程序,但在一段代码中遇到了问题。事实上,我想上传一个csv文件并显示一个相关图 我尝试使用actionbutton()和updateSelectizeInput()设置相关图 但是发生了一个错误: 错误:不支持的索引类型:NULL 有人有办法吗?谢谢 注意-我不想使用fileInput小部件上传csv文件!只有通过操作按钮 library(shiny) library(readr) library(corrplot) library(DT) #

我试图用R::Shining创建一个web应用程序,但在一段代码中遇到了问题。事实上,我想上传一个csv文件并显示一个相关图

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

但是发生了一个错误:

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

有人有办法吗?谢谢

注意-我不想使用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)
库(闪亮)
图书馆(readr)
图书馆(corrplot)
图书馆(DT)
#用于示例的文件
数据(iris)
write.csv(x=iris,file=“iris.csv”)
#用户界面

ui我稍微更改了您的ui和服务器,但我认为这可能会解决您的问题

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

我还在
data
reactive中添加了3个
req()
调用,在第二个
observeEvent(输入$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)
库(闪亮)
图书馆(readr)
图书馆(corrplot)
图书馆(DT)
#用于示例的文件
数据(iris)
write.csv(x=iris,file=“iris.csv”,row.names=F)
#用户界面

ui我很高兴它有帮助:)很好的一个世嘉!!