Search code examples
rshinypaste

What in this code causes text in an R Shiny app to repeat itself?


I recently modified a few lines in the server script in an R Shiny app. The code creates text by a long paste0 function that includes "hard-coded" text as well as numbers that are updated dynamically by references to a data object (data.tab1) created elsewhere in the app. Now, the pasted text repeats itself twice (i.e., it's there three times instead of one).

I'm wondering if someone might look through the code and see what's causing the text to repeat itself. I know that it's good practice, but in this case I simply can't provide a reprex: if I knew how to replicate the error, I would know how to solve the problem.

Here's what the repeated text looks like in the app:

enter image description here

Here's the new code:

output$snapshottext <- renderUI({
    HTML(
      paste0("<h3>",
        rv$selectedCounty,
        " County faces climate change exposures that pose considerable health risks to the population, especially to a number of vulnerable groups. </h3>
        
        
        <h2><font color='#3182bd'>Environmental Exposures</font></h2>
        <p>More frequent extreme weather patterns will pose a hazard to ",
        rv$selectedCounty,
        " County's population health. If greenhouse gas emissions continue to grow as they have in the past, the number of <strong>extreme heat days</strong> over ",
        round(data.tab1()$numratr[data.tab1()$ind_strt == 'heat_2040-2060']*9/5+32, 1),
        "&#176;F are expected to be ",
        round(data.tab1()$County_Value[data.tab1()$ind_strt == 'heat_2040-2060'], 0),
        " days per year in 2040-2060 and ",
        round(data.tab1()$County_Value[data.tab1()$ind_strt == 'heat_2080-2099'], 0),
        " days per year in 2080-2099. Higher temperatures can also increase hazardous air pollution. In ",
        data.tab1()$tperiod[data.tab1()$ind == 'ozone'],
        ", ",
        rv$selectedCounty,
        " County's average maximum <strong>ozone</strong> concentration was ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'ozone'], 3),
        " ppm and average <strong>fine particulate matter</strong> (PM2.5) was ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'pm'], 1),
        " µg/m3, which is ",
        ifelse(data.tab1()$County_Value[data.tab1()$ind == 'pm'] < 12, 'lower', 'higher'),
        " than the state standard of 12 µg/m3 (the statewide averages were ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'ozone'], 3),
        " ppm for ozone and ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'pm'], 1),
        " µg/m3 for PM2.5).</p>             
        <p>Higher temperatures and changes in precipitation are leading to longer, more severe droughts which, in turn, contribute to increased risk of <strong>wildfires</strong>. Around ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'wildfire'], 1),
        "% (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'wildfire'], 0),
              big.mark = ",",
              scientific = FALSE),
        " residents) of the ",
        rv$selectedCounty,
        " County population lived in very high wildfire risk areas in 2010 (statewide average was ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'wildfire'], 0),
        "%).</p>
        
        
        <h2><font color='#d95f0e'>Population Sensitivity</font></h2> 
        <p>Certain populations will experience the health impacts of climate change earlier, more often, or more severely, such as <strong>children and elderly</strong>, and those with disabilities. In ",
        data.tab1()$tperiod[data.tab1()$ind == 'children'], 
        ", ",
        rv$selectedCounty,
        " County's population included ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'children'], 1),
        "% children (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'children'], 0),
              big.mark = ",",
              scientific = FALSE),
        " persons under 5) and ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'elderly'], 1),
        "% elderly (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'elderly'], 0),
              big.mark = ",",
              scientific = FALSE),
        " persons 65 years and older) (state population included ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'children'], 1),
        "% children and ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'elderly'], 1),
        "% elderly). </p>
        <p>Between ",
        data.tab1()$tperiod[data.tab1()$ind == 'disability'],
        ", ", 
        round(data.tab1()$County_Value[data.tab1()$ind_strt == 'disability_physical'], 1),
        "% (",
        format(round(data.tab1()$numratr[data.tab1()$ind_strt == 'disability_physical'], 0),
              big.mark = ",",
              scientific = FALSE),
        " people) of ",
        rv$selectedCounty,
        " County's population reported having <strong>physical disabilities</strong> (statewide average was ",
        round(data.tab1()$CA_avg[data.tab1()$ind_strt == 'disability_physical'], 0),
        "%). In ",
        data.tab1()$tperiod[data.tab1()$ind == 'disability'], 
        ", ",
        rv$selectedCounty,
        " County's population included ",
        round(data.tab1()$County_Value[data.tab1()$ind_strt == 'disability_mental'], 1),
        "% (",
        format(round(data.tab1()$numratr[data.tab1()$ind_strt == 'disability_mental'], 0),
              big.mark = ",",
              scientific = FALSE),
        " people) with <strong>mental disabilities</strong> (statewide average was ", 
        round(data.tab1()$CA_avg[data.tab1()$ind_strt == 'disability_mental'], 0), 
        "%).</p>
        <p>Social or cultural isolation can also limit protective behaviors, community support, or the efficacy of emergency notifications during extreme weather and disasters. In ", data.tab1()$tperiod[data.tab1()$ind == 'linguistic'], ", ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'linguistic'], 1),
        "% of the county's households were considered <strong>linguistically isolated</strong> (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'linguistic'], 0),
              big.mark = ",",
              scientific = FALSE),
        " households), where no one aged 14 or older spoke English (statewide average was ", 
        round(data.tab1()$CA_avg[data.tab1()$ind == 'linguistic'], 1), 
        "%). </p>
        <p>Climate change and its impacts add to the cumulative stresses already experienced by populations without adequate financial resources, those living in communities with high incidence of violence, or who have limited access to supports like higher education, insurance, or personal transportation. In ",         rv$selectedCounty,         " County, ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'poverty'], 0),
        "% (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'poverty'], 0),
              big.mark = ",",
              scientific = FALSE),
        " residents) of the <strong>population was living below 200% of the poverty level</strong> in ", data.tab1()$tperiod[data.tab1()$ind == 'poverty'], ", (statewide average was ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'poverty'], 0),  
        "%). ",
        rv$selectedCounty,
        " County's <strong>violent crime rate</strong> in ",
        data.tab1()$tperiod[data.tab1()$ind == 'crime'],
        " was ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'crime'], 1),
        " per 100,000 persons (statewide average was ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'crime'], 1),
        " per 100,000). In ", data.tab1()$tperiod[data.tab1()$ind == 'education'], ", ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'education'], 0),
        "% of people (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'education'], 0),
              big.mark = ",",
              scientific = FALSE),
        " residents) in ",
        rv$selectedCounty,
        " County aged 25 years or older had an <strong>educational attainment</strong> of less than a four-year college degree (statewide average was ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'education'], 1),
        "%). In ", 
        data.tab1()$tperiod[data.tab1()$ind == 'insurance'], 
        ", ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'insurance'], 0),
        "% of the population (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'insurance'], 0),
              big.mark = ",",
              scientific = FALSE),
        " residents) in the county were <strong>without health insurance</strong> (statewide average was ", round(data.tab1()$CA_avg[data.tab1()$ind == 'insurance'], 1), "%). Please note that data is provided for ", 
        data.tab1()$tperiod[data.tab1()$ind == 'insurance'], 
        ".  This covers a time period both before and after the full implementation of the Affordable Care Act, which increased access to insurance. In ", 
        data.tab1()$tperiod[data.tab1()$ind == 'vehicles'], 
        ", ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'vehicles'], 0),
        "% of households (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'vehicles'], 0),
              big.mark = ",",
              scientific = FALSE),
        " households) <strong>lacked a personal vehicle</strong> (statewide average was ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'vehicles'], 1),
        "%). Finally, ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'outdoor'], 0),
        "% (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'outdoor'], 0),
              big.mark = ",",
              scientific = FALSE),
        " workers) of ",
        rv$selectedCounty,
        " County's labor force <strong>worked outdoors</strong> in ", 
        data.tab1()$tperiod[data.tab1()$ind == 'outdoor'], 
        ", and face an elevated risk to heat's effects on health (statewide average was ",
        round(data.tab1()$CA_avg[data.tab1()$ind == 'outdoor'], 1),
        "%).</p>


        <h2><font color='#54278f'>Adaptive Capacity</font></h2>
        <p>Adaptive capacity is important for responding to the impacts of climate change. Expanses of concrete and asphalt (<strong>impervious surfaces</strong>) make hot summers even hotter, while parks and trees can help make future heat waves more bearable. 
        In the portions of ", 
        rv$selectedCounty,
        " County where people resided, the land was about ",
        round(data.tab1()$County_Value[data.tab1()$ind == 'impervious'], 0),
        "% impervious surfaces (", 
        data.tab1()$tperiod[data.tab1()$ind == 'impervious'],
        " data) and ", 
        round(data.tab1()$County_Value[data.tab1()$ind == 'canopy'], 0),
        "% areas without <strong>tree canopy cover</strong> (", 
        data.tab1()$tperiod[data.tab1()$ind == 'canopy'],
        " data) (statewide averages were ", 
        round(data.tab1()$CA_avg[data.tab1()$ind == 'impervious'], 1), 
        "% and ", 
        round(data.tab1()$CA_avg[data.tab1()$ind == 'canopy'], 1), 
        "%, respectively). Access to public transit and <strong>air conditioning</strong> helps people relocate to cooler or safer spaces in the event of heat waves, extreme weather, or other events like wildfires.  According to a ", 
        data.tab1()$tperiod[data.tab1()$ind == 'ac'], 
        " survey, ", 
        round(data.tab1()$County_Value[data.tab1()$ind == 'ac'], 0),
        "% (",
        format(round(data.tab1()$numratr[data.tab1()$ind == 'ac'], 0),
              big.mark = ",",
              scientific = FALSE),
        " households) of households in ",
        rv$selectedCounty,
        " County did not have air conditioning (statewide average was ", 
        round(data.tab1()$CA_avg[data.tab1()$ind == 'ac'], 1), 
        "%). These findings highlight the aspects of vulnerability and the populations in ",
        rv$selectedCounty,
        " County most susceptible to health risks from current and future climate change exposures. Responding to and preparing for a changing climate presents opportunities for local health departments and partners to consider policies, actions, and infrastructure design that will not just protect the public, but also promote health equity, resiliency, and sustainability.</p>"
      )
    )
  })

And here's the corresponding text of the ui file:

tabPanel(
      "County Snapshot",value = "county_snapshot",
      # Create a new Row in the UI for selectInputs
      
      
      #####  Select a County  #####
      
      fluidRow(column(8, includeMarkdown("countyPlot.md")),
               column(
                 2,
                 # selectInput("cnty1",
                 #             "Select a County",
                 #             c(sort(
                 #               unique(as.character(CHVIdata$county))
                 #             ))),
                 tags$h3(textOutput("CountyText2")),
                 p(uiOutput("downloadCHPR1"))
               )),
      fluidRow(column(
        12,
        wellPanel(
          HTML("<h3> County compared to the state average</h3>"),
          plotlyOutput("plotCounty", height = "800px"),
          downloadLink(outputId = "downloadCountySnapshot", label = "Download the data in this figure")
        )
      )
      # column(3,
      #        wellPanel(
      #          uiOutput("topindicators")
      #        ))
      ),
      wellPanel(uiOutput("snapshottext")),
      
      wellPanel(DT::dataTableOutput("countyTable"))
      
    ),

where the text created in server is the uiOutput object snapshottext.

All of the modifications (with the exception of correcting a few typos) inserted dynamically updated numbers instead of hard-coded ones. For example,

" County's population reported having <strong>physical disabilities</strong> (statewide average was 6%). In 2011-2015,",
rv$selectedCounty,
" County's population included ", 

is now:

" County's population reported having <strong>physical disabilities</strong> (statewide average was ",
round(data.tab1()$CA_avg[data.tab1()$ind_strt == 'disability_physical'], 0),
"%). In ",
data.tab1()$tperiod[data.tab1()$ind == 'disability'],
", ",

where round(data.tab1()$CA_avg[data.tab1()$ind_strt == 'disability_physical'], 0) substitutes for "6%" and round(data.tab1()$CA_avg[data.tab1()$ind_strt == 'disability_physical'], 0) substitutes for "2011-2015".


Solution

  • Without seeing the data, I'd guess that the problem is with the items in the paste(). Possibly one of the arguments is resolving to a length-3 vector and the rest of the expression is getting recycled.

    Try cutting parts of the paste() expression until the repetition disappears and you should be able to zero in on the problematic item.