0

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.

Ann
  • 1
  • 3
  • 1
    `vars()` is a function in some packages, like `dplyr`. Perhaps R retrieve the function before the global variable? Try with a different name for `vars`. – RobertoT Aug 31 '22 at 10:36
  • 1
    Where are you defining the `vars` variable? This code is incomplete so we cannot run and test it without all variables being defined. It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Aug 31 '22 at 13:14
  • @RobertoT thanks for your comment. I've tried with a different name but the error still exists. – Ann Sep 01 '22 at 01:51
  • @MrFlick thanks for your advice. I've added the code where I define the 'vars'. Regarding providing a reprducible example, I think I can provide a sample data by 'dput()' function. But I think that's not enough for you to test and run it? Shall I provide other files like app_ui.R and run_app.R because it is written in the golem package I suppose the app could only run when the files in the package are complete. Sorry I'm quite new to R. If anything I misunderstand please let me know. – Ann Sep 01 '22 at 03:50

0 Answers0