I am new to R and I face a problem when I trying to create a shiny application.
My app contains UI dan Server in a single app.RI want to create a package that launches a shiny app.
In the server function, I have one reactive element that subset the dataset based on dateRangeInput
function from the UI. Here's part of the code in the UI
df <- coronavirus # I use coronavirus package from CRAN
ui <- sidebarLayout(
sidebarPanel(
dateRangeInput(
inputId = "range",
label = "Select date range",
start = "2020-03-01",
min = "2020-03-01",
end = "2020-09-27",
max = "2020-09-27",
format = "dd/mm/yyyy",
separator = " - "
)
Originally the reactive code and output plot in Server
function is
server <- function(input,output,session){
newdate <- reactive({
df %>% filter(between(date, input$range[1], input$range[2]))
})
output$plotly <- renderPlotly({
plot <- newdate() %>%
ggplot(aes(x = date,
y = cases,
color = country)) +
geom_line()
ggplotly(plot)
}
I passed this reactive function into another R file named newdate.R
#' @export
newdate <- reactive({
req(input$range)
df %>% filter(between(date, input$range[1], input$range[2]))
})
Then I pass this newdate()
function into the output plot
output$plotly <- renderPlotly({
plot <- newdate() %>%
ggplot(aes(x = date,
y = cases,
color = country)) +
geom_line()
ggplotly(plot)
There is an error that says
Error: object 'input' not found
The reactive function work fine when I put them directly in server. However when I try to put in separate R files, it gives me an error. My question is, how to put the Reactive function that has input argument into another R file? Help is much appreciated.
You could pass input
as argument to the reactive function:
newdate <- function(input) {
reactive({
req(input$range)
df %>% filter(between(date, input$range[1], input$range[2]))
})
}
The call to the function in the server is then:
plot <- newdate(input)() %>% ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.