繁体   English   中英

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

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

我的 Shiny 应用程序的目的是上传 a.csv 文件和 plot 及其数据的 ggplot 图。 当我在我的 Shiny 应用程序上上传 a.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',
                      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)

我得到的错误信息是:

“错误:未找到 object 'x'”。

欢迎来到 SO

您的问题是您在错误的时间更新了selectInput() :一旦服务器完成计算,更新就完成了,所以一旦data()完成运行就在这里。

所以要清楚,你在尝试 select之后更新你的输入。

另一件事是你需要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',
      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