在将任何.csv文件上传到应用程序后,我尝试使用用户选择的变量输入,并绘制一个简单的散点图。
错误:不允许从Shinyoutput对象读取对象。
我知道应该在 reactive 函数中添加,但是我不知道在何处添加它。有人可以跟我分享吗?谢谢!
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Javier Test"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
# Input: Select a file ----
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Horizontal line ----
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
# Input: Select quotes ----
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"'),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head"),
#implementing dropdown column
uiOutput("input1"), #use this instead when you are trying to upload something instead then process it
uiOutput("input2"),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
# Output: Data file ----
plotOutput("distPlot"),
plotOutput("scatterplot")
)
)
)
library(ggplot2)
# Define server logic required to draw a histogram
server <- function(input, output) {
#df <- reactiveValues(x= list, y = data.frame())
output$input1 <- renderUI({
selectInput("variable", "Variable:",
colnames(input$file1)) #this will select
})
output$input2 <- renderUI({
selectInput("variable1", "Variable:",
colnames(input$file1)) #this will select
})
# output$distPlot <- renderPlot({
#
# # obtain input from user for column
# x <- output$contents[, input1$variable] #selecting which column position
#
# # generate bins based on input$bins from ui.R
# bins <- seq(min(x), max(x), length.out = input$bins + 1)
#
# # draw the histogram with the specified number of bins
# hist(x, breaks = bins, col = 'darkgray', border = 'white')
#
# })
output$scatterplot <- renderPlot({
x <- output$contents[, input$variable]
y <- output$contents[, input$variable1]
#generate plot
ggplot(output$contents, aes(x=x, y=y, colour=Species)) + geom_point() + theme_classic()
})
}
shinyApp(ui, server)
更新
我已经开始工作了。解决方法如下。
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Javier Test"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
# Input: Select a file ----
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
# Horizontal line ----
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
#implementing dropdown column
selectInput('xcol', 'X Variable', ""),
selectInput('ycol', 'Y Variable', "", selected = "")),
# Show a plot of the generated distribution
mainPanel(
# Output: Data file ----
plotOutput('MyPlot')
)
)
)
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output, session) {
# added "session" because updateSelectInput requires it
data <- reactive({
req(input$file1) ## ?req # require that the input is available
inFile <- input$file1
# tested with a following dataset: write.csv(mtcars, "mtcars.csv")
# and write.csv(iris, "iris.csv")
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
quote = input$quote)
# Update inputs (you could create an observer with both updateSel...)
# You can also constraint your choices. If you wanted select only numeric
# variables you could set "choices = sapply(df, is.numeric)"
# It depends on what do you want to do later on.
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$MyPlot <- renderPlot({
x <- data()[,c(input$xcol, input$ycol)]
plot(x)
})
})
shinyApp(ui, server)