Reputation: 667
I have a really simple csv file.
> head(data)
X Y
1 1 1
2 2 2
3 3 4
4 4 8
I am trying to create a shinny app dashboard that plots this data after the file has been uploaded.
This is what I have so far.
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
fluidRow(
box(plotOutput("plot1", height = 250))
)
),
# Second tab content
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
)
server <- function(input, output) {
options(shiny.maxRequestSize=100*1024^2)
inFile <- input$file1
data <- read.csv(inFile$datapath, header = input$header, stringsAsFactors = FALSE)
output$plot1 <- renderPlot({
plot(data)
})
}
shinyApp(ui, server)
But I am getting an error
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
From what I have read the read file needs to be reactive, and then that need to be called by the plot, but I am not familiar with the reactive concepts, any help will be appreciated.
Upvotes: 1
Views: 1583
Reputation: 84529
You can do
data <- reactive({
inFile <- input$file1
if(!is.null(inFile)){
read.csv(inFile$datapath, header = input$header, stringsAsFactors = FALSE)
}
})
output$plot1 <- renderPlot({
req(data())
plot(data()$X, data()$Y)
})
Upvotes: 3