“我的闪亮应用程序”的目的是上传一个.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'”。