简体   繁体   English

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

[英]How can I set variable x as default when I upload a .CSV file on my App?

My Shiny Application's purpose is to upload a.csv file and plot a ggplot graph with its data.我的 Shiny 应用程序的目的是上传 a.csv 文件和 plot 及其数据的 ggplot 图。 When I upload a.csv file on my Shiny Application, the default variable is "y" for both input variables.当我在我的 Shiny 应用程序上上传 a.csv 文件时,两个输入变量的默认变量都是“y”。 However, when I change "y" to "x" on the "X Variable" input, the App works fine.但是,当我在“X 变量”输入上将“y”更改为“x”时,应用程序工作正常。 I just have to set "x" for "X Variable" input and "y" for "Y Variable" input as default when I upload the.csv file.当我上传.csv 文件时,我只需将“X 变量”输入设置为“x”,将“Y 变量”输入设置为“y”。

Here follows the app.R code with my comments and Error Message I get when I upload the file.下面是 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',
                      choices = names(df), 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)

The error message I get is:我得到的错误信息是:

"Error: object 'x' not found". “错误:未找到 object 'x'”。

and welcome to SO欢迎来到 SO

Your issue here is that you update the selectInput() at the wrong time: the update is done once the server has finished computing, so here once the data() is done running.您的问题是您在错误的时间更新了selectInput() :一旦服务器完成计算,更新就完成了,所以一旦data()完成运行就在这里。

So to be clear, you update your input after trying to select from it.所以要清楚,你在尝试 select之后更新你的输入。

Other thing is that you need to sym() & !!另一件事是你需要sym() & !! the input$ to make it work with ggplot() (see: https://github.com/ColinFay/tidytuesday201942/blob/master/R/mod_dataviz.R#L184 for example) input$使其与ggplot()一起使用(例如,请参见: https://github.com/ColinFay/tidytuesday201942/blob/master/R/mod_dataviz.R#L184

Here is a working version:这是一个工作版本:

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',
      choices = names(r$df),
      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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM