I'm trying to build a shiny app with the golem package. I'm stuck with this error for a week and couldnot find solutions. Please help me if anyone knows how to resolve the problem.
So, the goal of this app is very simple: there are selection boxes X and Y which allow users to select two variables from a dataset(vars), then a plot is produced.
I define the dataset in data-raw file, named vars.R, here's the code:
## code to prepare `vars` dataset goes here
rm(list=ls())
Sys.setenv(LANG = "en")
cat("\014")
# install.packages("data.table")
# install.packages("ggplot2")
# install.packages("shiny")
# install.packages("dplyr")
library(data.table)
library(shiny)
library(ggplot2)
library(dplyr)
#import data
HRS <- fread("C:/myShinyApp/shinyapptest/inst/extdata/HRS_small.csv")
#create a new data only including CESD, age, sex and edyrs
col_need = c("CESD", "age", "sex","edyrs")
vars <- HRS[, ..col_need]
vars_names = colnames(vars)
vars = as.data.frame(vars)
#clean data
clean_inputdat <- function(d_in){
n = NCOL(d_in)
col_names = colnames(d_in)
for (i in 1:n){
d = d_in[,i]
if ( is.numeric(d) ) {
d_in = d_in [!is.na(d),]
}else if( is.character(d) ){
d_in = d_in [!startsWith(d, "."),]
d_in = d_in [!is.na(d),]
d_in[, i] = as.numeric(d_in[,i])
colnames(d_in)[i] = col_names[i]
}
}
return (d_in)
}
vars = clean_inputdat(vars)
# try to solve the problem: xlim need finite values
vars = na.omit(vars)
table(is.na(vars))
usethis::use_data(vars, overwrite = TRUE)
usethis::use_data(vars_names, overwrite = TRUE)
Here's the code in app_server.R:
#' The application server-side
#' @param input,output,session Internal parameters for {shiny}.
#' DO NOT REMOVE.
#' @import shiny
#' @import ggplot2
#' @import data.table
#' @rawNamespace import(dplyr, except=c(first, between, last))
#' @importFrom stats lm
#' @noRd
globalVariables(c("vars_names"))
globalVariables(c("vars"))
# Your application server logic
app_server <- function(input,output){
output$ycol = renderUI({
selectInput(inputId = "ycol", label = "Y Variable", vars_names)
})
output$xcol = renderUI({
data_available = vars_names[! vars_names %in% input$ycol]
selectInput(inputId = "xcol", label = "X Variable", data_available)
})
X <- eventReactive(input$go, {
vars[, c(input$xcol)]
})
Y <- eventReactive(input$go, {
vars[, c(input$ycol)]
})
output$plot1 <- renderPlot({
input$go
ggplot2::ggplot(vars, ggplot2::aes(X(), Y())) + ggplot2::geom_point() +
ggplot2::geom_smooth(method = lm)
})
}
The error appears at the line: ggplot2::ggplot(vars, ggplot2::aes(X(), Y())) + ggplot2::geom_point() + ggplot2::geom_smooth(method = lm)
it says: Warning: Error in ggplot2::ggplot: You're passing a function as global data.
Have you misspelled the data
argument in ggplot()
An odd thing is, when I just run run_dev.R, this error appears. But if I run other files at a time, app_ui.R, app_server.R, run_app.R, and fianlly run_dev.R. Then everything is fine, the plot is produced. I think this is the reason why I could not successfully build the package afterwards so I must figure it out.
Great thanks in advance.