Reputation: 87
I know this question has been posted a few times but this is my first time developing something i Shiny and I am getting confused with a couple different things. One of them is inputting the data frame correctly and using it in the output functions.
My only goals right now is to:
Display the head or complete dataframe depending on user choice
I have a binary column called status (status being Pass or Fail). I want to group by dates to count the status (any one would do) and plot it.
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(readxl)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Data Quality Result Monitoring"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
# Show a plot of the generated distribution
mainPanel(
#plotOutput("linechart"),
h4("Observations"),
tableOutput("contents")
)
)
# Define server logic required to draw a histogram'
library(ggplot2)
server <- function(input, output) {
df <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
df <- read_xlsx(inFile$datapath, sheet = 1)
return(inFile)})
output$linechart <- renderPlot({
ndf() <- group_by(df,Execution_Date) %>% summarize( count = n() )
ggplot(ndf()) + geom_bar(aes(x=week,y=count),stat="identity")
})
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
dataset() <- df
if(input$disp == "head") {
return(head(dataset()))
}
else {
return(dataset())
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
Upvotes: 2
Views: 3236
Reputation: 2867
dataset() <- df
This is where you get the error:
"Error in <-: invalid (NULL) left side of assignment"
You can not assign a value to a reactive expression. It works the other way round:
dataset <- df()
Play around with this by using the print
function.
Another error in your code is this:
df <- read_xlsx(inFile$datapath, sheet = 1)
return(inFile)
You return the wrong variable, you want to return the df.
Here is the code which should work for you:
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(readxl)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Data Quality Result Monitoring"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
# Show a plot of the generated distribution
mainPanel(
#plotOutput("linechart"),
h4("Observations"),
tableOutput("contents")
)
)
# Define server logic required to draw a histogram'
library(ggplot2)
server <- function(input, output) {
df <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
df <- read_xlsx(inFile$datapath, sheet = 1)
df
})
output$linechart <- renderPlot({
ndf <- group_by(df(),Execution_Date) %>% summarize( count = n() )
ggplot(ndf + geom_bar(aes(x=week,y=count),stat="identity"))
})
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
dataset <- df()
if(input$disp == "head") {
return(head(dataset))
}
else {
return(dataset)
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
I also recommend that you implement a check for structure and names in your code.
Upvotes: 3
Reputation: 5481
This is due to ndf() <- group_by(df,Execution_Date) %>% summarize( count = n() )
ndf()
is NULL function that does not exist.
df
is a reactive and you use it with df()
instead of df
, meaning that code is evaluated each time the reactivity changes.
Upvotes: 1