DS4PS / cpp-529-fall-2020

http://ds4ps.org/cpp-529-fall-2020/
0 stars 0 forks source link

Mutating Error for Chorlopleth maps #35

Open ekmcintyre opened 3 years ago

ekmcintyre commented 3 years ago

I keep getting an error regarding this part of the code for creating Choropleth maps in the dashboard mutate( q = ntile( get(input$home.value), 10 ) image

lecy commented 3 years ago

The best way to debug the code is to use one variable to make sure it is working, then replace it with the shiny widget name.

test.variable <- dat$variable.name  # mhval or some actual variable name 

dat %>% 
  mutate( q = ntile( test.variable, 10 ) )

When the code works fine then integrate the shiny widget ID.

Note that the current value is referenced at the ID placeholder. But the current value will be a string.

class( input$home.value )  
# input$home.value stores whichever variable name is selected
# data type will be character:  e.g. "mhval" 

If you want to reference the object with the same name, you need get():

get( input$home.value )  
# returns the object mhval not the string "mhval" 
ekmcintyre commented 3 years ago

Is this the correct way to debug the code?

test.variable <- no$pnhwht12.x

renderPlot({

get_data <- 
  reactive({
             no.sf <- 
             no.sf %>% 
             mutate( q = ntile( get(test.variable), 10 ) )  

I ran it in shiny and got the same error message.

lecy commented 3 years ago

You would not use get() with the test variable.

I am suggesting to take the code outside of the shiny functions completely to test it. Something like:

no.sf <- 
  no.sf %>% 
  mutate( q = ntile( pnhwht12.x, 10 ) ) 

Note you pulled the test variable from no and then use the dataframe no.sf in your reactive function. Make sure you are pulling data from the same dataframe to avoid incongruency errors.

I am guessing the problem, though, is that you define the variable pnhwt12 in your widget and it is named pnhwht12.x in your dataset.

This occurs when you merge the same data twice in your data steps. To avoid duplicate variable names R will add a .x to one an a .y to another to keep them distinct.

This would be a problem with data steps, not the dashboard. Are you merging one of the census files twice in your data steps?