Here's a working example. I took your code and modified it so that the Column Numbers can be passed from UI.R
as inputs. (I use the diamonds dataset in ggplot2 for my dataframe.)
Note that I have created a couple of reactive
functions in Server.R.
Server.R
library(shiny)
library(datasets)
library(ggplot2)
#x <- read.csv(file.choose())
x <- diamonds
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
createPlot <- function(df, colx, coly) {
x <- names(df)[colx]
y <- names(df)[coly]
ggplot(data=df, aes_string(x = x, y = y) ) + geom_line()
}
Y <- reactive({
x
})
# Generate a summary of the dataset
output$summary <- renderPrint({
dataset <- x
summary(dataset)
})
# Show the first "n" observations
output$view <- renderTable({
head(x, n = input$obs)
})
# create line plot (I took this from https://gist.github.com/pssguy/4171750)
output$plot <- reactivePlot(function() {
df <- Y()
print(createPlot(df, colx=input$xa, coly=input$ya))
})
})
UI.R
library(shiny)
# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Sample Proj"),
# Sidebar with controls to select a dataset and specify the number
# of observations to view
sidebarPanel(
numericInput("obs", "Number of observations to view:", 10)
,numericInput("xa", "Column to plot as X-axis:", 5)
,numericInput("ya", "Column to plot as Y-axis:", 6)
),
# Show a summary of the dataset and an HTML table with the requested
# number of observations
mainPanel(
tabsetPanel(
tabPanel("Table", tableOutput("view")),
tabPanel("LineGraph", plotOutput("plot"))
)
)
))
As a separate suggestion, you could first get your shiny app working with a static dataframe, then try the file.choose()
option with variable data frames.
Hope this helps you move forward.
Updated based on @joran's comment:
My original response was using the column number inside ggplot's aes
with an environment=environment()
argument added. I have modified the createPlot function in server.R to use aes_string
instead.