I do have another Rmarkdown/papaja package question from my side and would be very happy if anyone was willing to help :)
As many people on the internet I have trouble controlling the position of my tables.
I have the
floatsintext: yes
option included in the YAML header.
I know that there are LATEX options such as fig.pos = "!H" for which I have to load the float package
header-includes:
- usepackage\{float}
However, doing this I get the following error message:
! LaTeX Error: Unknown float option `H'.
Which tells me the float package was not able to load I guess?
(I do hav MacTex installed and also the newest R version).
What also is absolutely confusing to me is that tables (generated with app_table()) appear in the (approximately) right position when I leave the default spacing, but only appear at the end of my work when I add
header-includes:
- \usepackage{setspace}
- \AtBeginEnvironment{tabular}{\singlespacing}
- \AtBeginEnvironment{lltable}{\singlespacing}
- \AtBeginEnvironment{tablenotes}{\singlespacing}
to the YAML header in order to control the spacing of my tables.
I would really appreciate any help!
Thanks in advance!
Edit:
I don't know if this serves the purpose, but if I create the following options, my table appears at the end (and not where it should be)
title : "TITLE"
shorttitle : "short title"
author:
- name : "me"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "x"
email : "y"
role: # Contributorship roles (e.g., CRediT, https://casrai.org/credit/)
- Conceptualization
- Writing - Original Draft Preparation
- Writing - Review & Editing
# - name : "Ernst-August Doelle"
# affiliation : "1,2"
# role:
# - Writing - Review & Editing
affiliation:
- id : "1"
institution : ""
# - id : "2"
# institution : "Konstanz Business School"
authornote: |
Enter author note here.
abstract: |
keywords : "keywords"
wordcount : "X"
bibliography :
floatsintext : yes
figurelist : no
tablelist : no
footnotelist : no
linenumbers : no
mask : no
draft : no
csl: "apa.csl"
documentclass : "apa7"
classoption : "man"
output : papaja::apa6_pdf
toc: true
header-includes:
- \usepackage{float}
- \usepackage{setspace}
- \AtBeginEnvironment{tabular}{\singlespacing}
- \AtBeginEnvironment{lltable}{\singlespacing}
- \AtBeginEnvironment{tablenotes}{\singlespacing}
---
{r setup, include = FALSE}
library("papaja")
library("apa")
library("tidyverse")
library("apaTables")
r_refs("r-references.bib")
{r analysis-preferences}
# Seed for random number generation
set.seed(42)
knitr::opts_chunk$set(cache.extra = knitr::rand_seed)
{r}
cor_table <- apa.cor.table(iris)
Text BLABLABLABLA
{r tab, results = "asis", fig.pos = "!h"}
apa_table(cor_table$table.body,
caption = "Means, standard deviations, and correlations with confidence intervals for study variables.",
note = "Note. M and SD are used to represent mean and standard deviation, respectively.Values in square brackets indicate the 95% confidence interval.The confidence interval is a plausible range of population correlations that could have caused the sample correlation (Cumming, 2014). * indicates p < .05. ** indicates p < .01.", font_size = "footnotesize", row.names = F,
placement = "p")
# Methods
We report how we determined our sample size, all data exclusions (if any), all manipulations, and all measures in the study. <!-- 21-word solution (Simmons, Nelson & Simonsohn, 2012; retrieved from http://ssrn.com/abstract=2160588) -->
## Participants
## Material
## Procedure
## Data analysis
We used `r cite_r("r-references.bib")` for all our analyses.
# Results
# Discussion
\newpage
# References
\begingroup
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}
<div id="refs" custom-style="Bibliography"></div>
\endgroup
For PDF output, there is a recommended way to customize the placement of tables created via apa_table(). (You do not have to load the float package via header includes.)
First, set the YAML header option floatsintext: yes.
Second, when creating your table with apa_table(), use the function's placement argument:
```{r tab}
apa_table(cor_table$table.body,
caption = "Means, standard deviations, and correlations with confidence intervals for study variables.",
note = "Note. M and SD are used to represent mean and standard deviation, respectively.Values in square brackets indicate the 95% confidence interval.The confidence interval is a plausible range of population correlations that could have caused the sample correlation (Cumming, 2014). * indicates p < .05. ** indicates p < .01.", font_size = "footnotesize", row.names = F,
placement = "H")
So I have been struggling with an issue for a while now and seem to be getting nowhere...
The problem exists while I am looping through a hash, around the 3rd level deep.
So my code looks a little like:
request[:items][:location_item].each do |locaton_item|
pp location_item[:name]
location_item[:items][:sub_area_option_item].each do |sub_area_option_item|
pp sub_area_option_item[:name]
sub_area_option_item[:items][:option_item].each do |option_item|
option_item[:name] <- THIS IS WHERE MY CODE BREAKS
end
end
end
The reason this is failing is because this particular part of the loop has 2 different variants, one where there is an infinite amount of "option_items" and one where there is only one "option_item". Instead of sending me back the single "option_item" it is looping through this "option_item" and not finding this particular field. Any ideas as how to force it when only one object is found to still return it as an array?
UPDATE(05/092012)
TypeError - Symbol as array index
This error does not appear if I remove the single "option_item" from the hash or even by filtering it with a conditional statement.
As another update that was an example before, here is the code:
request[:items][:house_option_item][:items][:location_option_item].each do |location_option_item|
pp location_option_item.try(:[], :location).try(:[], :name)
# pp location_option_item[:location][:name]
location_option_item[:items][:sub_area_option_item].each do |sub_area_option_item|
pp "--" + sub_area_option_item[:sub_area][:name] + "--" + sub_area_option_item[:sub_area][:id]
count = 0
# pp sub_area_option_item[:items].length
# pp "+++++++++++++++++"
# if sub_area_option_item[:sub_area][:name] != "Conservatory"
[sub_area_option_item[:items][:option_item]].each do |option_item_key_info|
pp "----"
pp option_item_key_info[:usage_name]
end
# end
end
end
Example data(abbreviated):
{:"#xmlns:i"=>"http://www.w3.org/2001/XMLSchema-instance",
:"#xmlns:a"=>"http://schemas.datacontract.org/2004/07/RXTypes",
:items=>
{:house_option_item=>
{:design=>
{:house_style=>nil,
:sales_name=>nil,
:sq_ft=>"0",
:"#xmlns:b"=>"http://schemas.datacontract.org/2004/07/",
:arch_ref=>"D3H102.1",
:marketing_name=>"Warwick",
:num_beds=>"0"},
:items=>
{:location_option_item=>
[{:location=>{:sort_order=>"0", :name=>"Eat", :id=>"1"},
:items=>
{:sub_area_option_item=>
[{:sub_area=>
{:location_id=>"7",
:sort_order=>"0",
:name=>"Conservatory",
:id=>"31"},
:items=>
{:option_item=>
{:lead_days=>"0",
:collection_code=>"NEWHE01",
:usage_name=>"Conservatory",
:max_qty=>"1",
:is_a_bundle=>false,
:only_order_bundle=>false,
:sub_area=>"Conservatory",
:key_info=>
{:specification=>
"PVC-U 'Edwardian' style Conservatory in white with high performance toughened glass double glazed windows and 600mm (approx) dwarf base wall internally and externally. Conservatory to include fan and light, electric heater, double power point and ceramc floor tiling selected from tile choice . Size of Conservatory to be as follows: 3.2 x 3.1 metre (Availabil
ity will be subject to ground levels/conditions)",
:description=>"Conservatory",
:image_url=>"/2012/8/10/334_web.jpg",
:item_id=>"3",
:thumbnail_url=>"/2012/8/10/334_thumb.jpg"},
:build_stage_id=>"0",
:location=>"Building",
:option_flag=>"67",
:bundle_items=>nil,
:price=>"8950",
:choice_items=>nil,
:spec_level=>"85"}}},
{:sub_area=>
{:location_id=>"7",
:sort_order=>"0",
:name=>"Doors",
:id=>"32"},
:items=>
{:option_item=>
[{:lead_days=>"0",
:collection_code=>"NEWHE01",
:usage_name=>"Front Door",
:max_qty=>"0",
:is_a_bundle=>false,
:only_order_bundle=>false,
:sub_area=>"Doors",
:key_info=>
{:specification=>
"GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.",
:description=>"GRP Front door",
:image_url=>nil,
:item_id=>"4",
:thumbnail_url=>nil},
:build_stage_id=>"0",
:location=>"Building",
:option_flag=>"88",
:bundle_items=>nil,
:price=>"0",
:choice_items=>
{:option_item_key_info=>
[{:specification=>nil,
:description=>"NG42 Diamond",
:image_url=>"/2012/8/21/485_web.jpg",
:item_id=>"211",
:thumbnail_url=>"/2012/8/21/485_thumb.jpg"},
{:specification=>nil,
:description=>"NG21 Square",
:image_url=>"/2012/8/21/486_web.jpg",
:item_id=>"212",
:thumbnail_url=>"/2012/8/21/486_thumb.jpg"},
{:specification=>nil,
:description=>"NG51 Square",
:image_url=>"/2012/8/21/487_web.jpg",
:item_id=>"213",
:thumbnail_url=>"/2012/8/21/487_thumb.jpg"},
{:specification=>nil,
:description=>"NG42 Rectangle",
:image_url=>"/2012/8/21/488_web.jpg",
:item_id=>"214",
:thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]},
:spec_level=>"83"},
{:lead_days=>"0",
:collection_code=>"NEWHE01",
:usage_name=>"Front Door",
:max_qty=>"0",
:is_a_bundle=>false,
:only_order_bundle=>false,
:sub_area=>"Doors",
:key_info=>
{:specification=>
"GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.",
:description=>"GRP Front door",
:image_url=>nil,
:item_id=>"4",
:thumbnail_url=>nil},
:build_stage_id=>"0",
:location=>"Building",
:option_flag=>"88",
:bundle_items=>nil,
:price=>"0",
:choice_items=>
{:option_item_key_info=>
[{:specification=>nil,
:description=>"NG42 Diamond",
:image_url=>"/2012/8/21/485_web.jpg",
:item_id=>"211",
:thumbnail_url=>"/2012/8/21/485_thumb.jpg"},
{:specification=>nil,
:description=>"NG21 Square",
:image_url=>"/2012/8/21/486_web.jpg",
:item_id=>"212",
:thumbnail_url=>"/2012/8/21/486_thumb.jpg"},
{:specification=>nil,
:description=>"NG51 Square",
:image_url=>"/2012/8/21/487_web.jpg",
:item_id=>"213",
:thumbnail_url=>"/2012/8/21/487_thumb.jpg"},
{:specification=>nil,
:description=>"NG42 Rectangle",
:image_url=>"/2012/8/21/488_web.jpg",
:item_id=>"214",
:thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]},
:spec_level=>"83"},
{:lead_days=>"0",
:collection_code=>"NEWHE01",
:usage_name=>"Front Door",
:max_qty=>"0",
:is_a_bundle=>false,
:only_order_bundle=>false,
:sub_area=>"Doors",
:key_info=>
{:specification=>
"GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.",
:description=>"GRP Front door",
:image_url=>nil,
:item_id=>"4",
:thumbnail_url=>nil},
:build_stage_id=>"0",
:location=>"Building",
:option_flag=>"88",
:bundle_items=>nil,
:price=>"0",
:choice_items=>
{:option_item_key_info=>
[{:specification=>nil,
:description=>"NG42 Diamond",
:image_url=>"/2012/8/21/485_web.jpg",
:item_id=>"211",
:thumbnail_url=>"/2012/8/21/485_thumb.jpg"},
{:specification=>nil,
:description=>"NG21 Square",
:image_url=>"/2012/8/21/486_web.jpg",
:item_id=>"212",
:thumbnail_url=>"/2012/8/21/486_thumb.jpg"},
{:specification=>nil,
:description=>"NG51 Square",
:image_url=>"/2012/8/21/487_web.jpg",
:item_id=>"213",
:thumbnail_url=>"/2012/8/21/487_thumb.jpg"},
{:specification=>nil,
:description=>"NG42 Rectangle",
:image_url=>"/2012/8/21/488_web.jpg",
:item_id=>"214",
:thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]},
:spec_level=>"83"},
Krista's example will work if there is only one option_item, but not if there's more. I'd add flatten and adjust it to:
[sub_area_option_item[:items][:option_item]].flatten.each do |option_item|
option_item[:name]
end
the [] around it makes sure it's an array. the flatten makes sure that if it already was an array, that you remove the extra layer of [] eg:
['a'].flatten
=> ["a"]
[['a','b','c',]].flatten
=> ["a", "b", "c"]
As far as I can tell, the second part of your loop should be the problem - given your source code and the sample data
01: request[:items][:house_option_item][:items][:location_option_item].each do |location_option_item|
02: location_option_item[:items][:sub_area_option_item].each do |sub_area_option_item|
03: count = 0
04: # if sub_area_option_item[:sub_area][:name] != "Conservatory"
05: [sub_area_option_item[:items][:option_item]].each do |option_item_key_info|
06: pp option_item_key_info[:usage_name]
07: end
08: # end
09: end
10: end
Note that line 01 would end up accessing location_option_item which in your sample above is:
:location_option_item=>
[{:location=>{:sort_order=>"0", :name=>"Eat", :id=>"1"},
:items=>
{:sub_area_option_item=>
[{:sub_area=>
{:location_id=>"7",
:sort_order=>"0",
:name=>"Conservatory",
:id=>"31"},
:items=>
{:option_item=>
{:lead_days=>"0",
:collection_code=>"NEWHE01",
........ etc .......
Notice that location_option_item is the key, which has for value an array.
Thus, my guess is that this line is the problem (02): location_option_item[:items]
Since location_option_item is an array, you cannot access the :items symbol from it.
If you wanted to get items it would be something more like:
location_option_item[1][:items]