0

I have quite a long R-markdown file that I give cost input values to, it runs an analysis (simple mathematics) using these costs, and tells me if a proposed treatment is cost-effective per a threshold value I have supplied.

I would like to apply this code file to 3 different countries, each with country-specific cost values and cost-effective thresholds.

So, I need to update the input values I use in this model 3 times, but I would prefer not to just copy and paste the block of code into a file 3 times and then manually update the input values per the country I study.

Is there an elegant way to create the values at the start of my code, i.e. costs_Ireland, costs_Italy, costs_Taiwan, and then tell R to run the code for all 3 in turn?

I would also like the output values and graphs the code creates to be produced, but with an indication as to what starting values they drew from, i.e., when my code creates a histogram, that it uses the costs_Ireland and is titled histogram_Ireland, or something like that, with this being updated for costs_Italy and costs_Taiwan. A big part of this is that, rather than a mathematical result or graphic being created in the first run, and and being replaced during the second run, that they will all be available at the end of the execution of the code.

Would anyone be able to advise me on how to do this?

Thank you.

I've created an Rmarkdown file that is just the R code repeated 3 times and the input values manually updated each time, but I think that is a very inelegant solution, particularly as my code is already very long.

Update:

A good point was made on including a simple example, below I include one for a simple calculation and histogram, with the question of, if these were Irish cost values, how would I repeat this for Italian and Spanish values, without having to copy out my code twice, and without overwriting the output values I create below?

cost_drugs <-10
cost_dressing <- 30 
cost_consultation <- 50

total_costs <- cost_drugs + cost_dressing + cost_consultation 

# Create data for a graph.
hist_data <-  c(10,30,50)

# Create the histogram:
hist(hist_data , xlab = "Costs", col = "yellow", border = "blue")

# Show me total costs:

total_costs

Further Update on My Attempts at Parameterized Reporting

Thanks for your input, I think a parameterized report is the best approach. As I said I have 3 countries, Ireland, Italy, and Taiwan. Say for argument's sake that everything in Ireland costs 10 Dollars, everything in Italy costs 30 Dollars and everything in Taiwan costs 50 Dollars. I want to set it up so that I can tell it to run the code in my Rmarkdown file for Ireland, Italy and Taiwan, and to keep the created values.

I do this by adding the params in the YAML as follows:

---title: 'Study Title'
author: "Author"
output:
  html_document: default
editor_options:
  markdown:
    wrap: sentence
params:country: c(Ireland, Italy, Taiwan)
cost_drugs: c(10, 30, 50)
cost_dressing: c(10, 30, 50)
cost_consultation: c(10, 30, 50)
---

Then later in my Rmarkdown code to create total costs per the country I'm looking at via:

params$country[1]_total_costs <- params$cost_drugs[1] + params$cost_dressing[1] + params$cost_consultation[1] 

to hopefully create output like:  

Ireland_total_costs = 30

I instead get the following error:

Error: unexpected input in "params$country[1]_"

How can I update this to create variables from when I add Irish costs together only, Italian costs together only, and Taiwanese costs together only?  Thank you.

  • 1
    You could turn your code into a function – Juan C Jan 06 '23 at 18:25
  • 1
    RMarkdown supports parameterized reports. Take a look at https://bookdown.org/yihui/rmarkdown/parameterized-reports.html. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 06 '23 at 19:05
  • I've updated the question per your suggestions @MrFlick – James Moore Jan 06 '23 at 19:26
  • 1
    But where do the values of 10, 30, 50 come from? Do you have any sort of data frame with different values for each of the countries? You need the data in a form that can be iterated over. Have you gone though the information on parameterized reports yourself yet? Have you tried that at all? Where exactly are you getting stuck? – MrFlick Jan 06 '23 at 20:12
  • @MrFlick I've added my approach to parameterized reporting to the question – James Moore Jan 10 '23 at 10:41

0 Answers0