在我的应用上载.CSV文件时,如何将变量x设置为默认值?

“我的闪亮应用程序”的目的是上传一个.csv文件,并使用其数据绘制ggplot图。 当我在Shiny Application上上传.csv文件时,两个输入变量的默认变量均为“ y”。但是,当我在“ X变量”输入上将“ y”更改为“ x”时,该应用程序可以正常工作。 上传.csv文件时,我只需将“ X变量”输入设置为“ x”,将“ Y变量”输入设置为“ y”。

以下是app.R代码以及我的评论和上传文件时收到的错误消息。

library(shiny)
library(datasets)
library(ggplot2)

ui <- shinyUI(fluidPage(
  titlePanel("Column Plot"),tabsetPanel(
    tabPanel("Upload File",titlePanel("Uploading Files"),sidebarLayout(
               sidebarPanel(
                 fileInput('file1','Choose CSV File',accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),tags$br(),checkboxInput('header','Header',TRUE),radioButtons('sep','Separator',c(Comma=',',Semicolon=';',Tab='\t'),'),selectInput('xcol','X Variable',"",selected = NULL),selectInput('ycol','Y Variable',selected = NULL)

               ),mainPanel(
                 tableOutput('contents'),plotOutput('MyPlot')
               )
             )
    )
  )
)
)


server <- shinyServer(function(input,output,session) {

  data <- reactive({ 
    req(input$file1)

    inFile <- input$file1 

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

    ## Update inputs
    ## I've already tried to change the selected = names(df) to selected = NULL on this part of the server code,but it didn't work.

    updateSelectInput(session,inputId = 'xcol',label = 'X Variable',choices = names(df),selected = names(df))
    updateSelectInput(session,inputId = 'ycol',label = 'Y Variable',selected = names(df)[2])

    return(df)
  })

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

  output$MyPlot <- renderPlot({

    xy <- data()[,c(input$xcol,input$ycol)]
    ggplot(data = xy,aes(x,y)) +
      geom_line() +
      geom_point()

  })
})

shinyApp(ui,server)

我收到的错误消息是:

  

“错误:找不到对象'x'”。

www4046994 回答:在我的应用上载.CSV文件时,如何将变量x设置为默认值?

欢迎来到SO?

您的问题是,您在错误的时间更新了selectInput():服务器完成计算后就完成了更新,因此data()运行完毕后就完成了更新。

为清楚起见,请在之后尝试从中进行选择,然后更新输入。

另一件事是,您需要sym()!! input$才能与ggplot()一起使用(例如:参见https://github.com/ColinFay/tidytuesday201942/blob/master/R/mod_dataviz.R#L184)>

这是一个有效的版本:

library(shiny)
library(datasets)
library(ggplot2)
library(rlang)

ui <- shinyUI(fluidPage(
  titlePanel("Column Plot"),tabsetPanel(
    tabPanel("Upload File",titlePanel("Uploading Files"),sidebarLayout(
               sidebarPanel(
                 fileInput('file1','Choose CSV File',accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),tags$br(),checkboxInput('header','Header',TRUE),radioButtons('sep','Separator',c(Comma=',',Semicolon=';',Tab='\t'),'),selectInput('xcol','X Variable',"",selected = NULL),selectInput('ycol','Y Variable',selected = NULL)

               ),mainPanel(
                 tableOutput('contents'),plotOutput('MyPlot')
               )
             )
    )
  )
)
)


server <- shinyServer(function(input,output,session) {

  r <- reactiveValues(
    df = NULL
  )

  observe({
    req(input$file1$datapath)
    # req(input$xcol)
    # req(input$ycol)
    r$df <- read.csv(
      input$file1$datapath,header = input$header,sep = input$sep
    )
  })


  observeEvent( r$df,{
    updateSelectInput(
      session,inputId = 'xcol',label = 'X Variable',choices = names(r$df),selected = names(r$df)[1]
    )
    updateSelectInput(
      session,inputId = 'ycol',label = 'Y Variable',selected = names(r$df)[2]
    )
  },ignoreInit = TRUE)


  output$contents <- renderTable({
    req(r$df)
    head(r$df)
  })

  output$MyPlot <- renderPlot({
    req(r$df)
    req(input$xcol)
    req(input$ycol)
    ggplot(
      data = r$df,aes(!!sym(input$xcol),!!sym(input$ycol))
    ) +
      geom_line() +
      geom_point()

  })
})

shinyApp(ui,server)

enter image description here

本文链接:https://www.f2er.com/3169892.html

大家都在问