Reputation: 179
I have the following data:
> data
creadate employee
2020-01-11 A A
2020-01-11 A A
2020-01-11 A A
2020-01-12 A B
2020-01-12 A B
2020-01-12 A C
2020-01-13 A C
2020-01-14 A C
2020-01-14 A D
2020-01-15 A D
The objective is to build a shiny app containing a plotly graph. First, I want to transform the data and I add another column that counts the number of occurrences for each time frame(week, day, and date)-employee combination. E.g. the normal code for the week would be:
data %>%
count(Week = week(creadate), employee)
Now, I want to give the user the possibility to choose between the three choices. I write the following full code (see #2 specifically)
library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)
library(plotly)
data <- read.csv("Desktop/adf.csv")
ui <- fluidPage(
fluidRow(
column(4,
selectInput("timeFrame", "Select time frame", c("month", "week", "day"), multiple = TRUE),
dateRangeInput("date", "Select date", start = "2020-01-01"),
column(8,
plotlyOutput("errorTable")
)
)
)
server <- function(input, output, session) {
#create a reactive object with a NULL starting value
listofrows <- reactiveValues(data = NULL)
#observe the changes in inputs and update the reactive object
observeEvent(c(input$timeFrame, input$date), {
listofrows$data <- data %>%
subset(data, creadate >= input$date[1] & creadate <= input$date[2]) %>% # 1------------------------
count(input$timeFrame[1] = input$timeFrame[1](creadate), employee) # 2-----------------------------
}, ignoreInit = T,ignoreNULL = TRUE)
output$plot <- renderPlotly({
plot_ly(new_data,
x = ~ input$timeFrame[1],
y = ~ n,
color = ~ employee,
frame = ~ input$timeFrame[1],
type = 'scatter',
mode = 'markers'
)
}
)
}
shinyApp(ui, server)
However, this code does not work. It tells:
Error in : First argument, `data`, must be a data frame or shared data.
[No stack trace available]```
Do you have any idea?
Upvotes: 0
Views: 155
Reputation: 545528
There are multiple problems.
The first is with the subset
call, as indicated by the warning: you are passing data
twice — once via the first explicit argument, and once since you’re using subset
on the right-hand side of a pipe; the code
data %>%
subset(data, creadate >= input$date[1] & creadate <= input$date[2]) …
is the same as
subset(data, data, creadate >= input$date[1] & creadate <= input$date[2]) …
To fix this, remove the data
argument. Furthermore, since you’re using the tidyverse, I recommend sticking to it, rather than mixing it with base functions; in other words, use filter
instead of subset
. Besides consistency, the filter
function is also more robust in the face of errors and has better error messages than subset
.
the second problem is your count
function call — it’s not entirely clear what you’re attempting to do here. Consult the documentation of the count
function to ensure you’re using it correctly.
There are more issues. For example, your plotting code uses a new_data
variable that’s never defined. You probably want to use listofrows$data
here. The arguments to plot_ly
are also wrong: you can’t pass variables as formulas, you need to quote them properly (e.g. via formula(paste('~', input$timeFrame[1]))
).
Upvotes: 1