Dear all,
I cannot figure out how to get ggplot to plot user-imported data. The data import is done with the reactive function and the table containg this data is drawn in the mainpanel of one tab. However, the same data does not plot in the other tab.
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("Boxplot"),
sidebarLayout(
sidebarPanel( position = "left",
conditionalPanel(condition = "input.tabselected == 1",
uiOutput("Y_achse"),
uiOutput("X_achse"),
checkboxInput("scatter","Jitter"),
sliderInput("jitt","jitter", min=0.1, max = 1, value = 0.25)),
conditionalPanel(condition = "input.tabselected == 2",
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain")),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = "\t"),
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"'),
tags$hr(),
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
)
),
mainPanel(
tabsetPanel(type = "tabs", id="tabselected",
tabPanel("Data", value = 2, tableOutput("contents")),
tabPanel("Plot", value = 1, plotOutput("box"))
)
)
))
server <- function(input, output, session) {
observe({ print(input$Y_ax)})
observe({ print(typeof(input$Y_ax))})
observe({ print(input$X_ax)})
observe({ print(typeof(input$X_ax))})
observe({ print(head(df()))})
observe({ print(is.data.frame(df()))})
observe({ print(df()$Gene)})
df <- reactive(
tryCatch({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data <- read.csv(inFile$datapath,
header = input$header,
sep = input$sep,
quote = input$quote,
dec=",",
row.names = (NULL))
data
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
})
)
output$contents <- renderTable({
if(input$disp == "head") {return(head(df()))}
else {return(df())}
})
output$Y_achse <-
renderUI({selectInput("Y_ax","Y-axis", choices = names(df()))})
output$X_achse <-
renderUI({selectInput("X_ax","X-axis", choices = names(df()))})
output$box <- renderPlot({
if (is.null(input$Y_ax)) {return()}
#ggplot(aes(x=df()$Gene, y=df()$Ct)+
ggplot(data = df(), mapping = aes(x=eval(as.name(input$X_ax)), y=eval(as.name(input$Y_ax)))+
geom_boxplot(notch = FALSE))
})
}
shinyApp(ui, server)
the data looks like this:
head(pcr)
Gene Sample Rep Type Serial Ct
1 14-3-3e A05 1 Unkn NA 16.24
2 14-3-3e A05 1 Unkn NA 16.24
3 14-3-3e A05 2 Unkn NA 16.42
4 14-3-3e A05 2 Unkn NA 16.46
5 14-3-3e A05 3 Unkn NA 16.64
6 14-3-3e A05 3 Unkn NA 16.76