Group_by_at 和 summarise_ 在使用 shiny/markdown 可重载时产生值。 R
Group_by_at and summarise_ producing values when using shiny/markdown rendetable. R
这是我的代码
```{r}
#Assign Variables
Categorical.Variables = c( "Race/Ethnicity" ,"Gender", "Education", "Intervention")
Numeric.Variables = c("Age", "Pre Weight", "Post Weight", "Follow-up Weight","Wieght Loss/Gain after Intervention","Wieght Loss/Gain on Follow Up" )
```
我有这个简单的 table 我正在尝试用 dplyr 制作。
```{r}
renderTable({
wl_data %>% group_by_at(input$categorical_variable) %>%
summarise("Average " = mean(wl_data[[input$numeric_variable]]),
"Median " = median(wl_data[[input$numeric_variable]])
)
})
```
这是我得到的输出,它对整个数字输入具有相同的值,并且没有被分类输入分解。
我想要的输出是有一个反应性的 table,它将给我根据输入变化的汇总统计数据。到目前为止,我已经非常接近了,但我不知道如何让它发挥作用。
我做错了什么?
数据
> dput(wl_data)
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100), Gender = c("Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male"), Age = c(34.1958081813646, 38.342817530618, 31.4255555318668,
39.9174711825326, 30.4606180330738, 35.0906190911774, 33.4250614438206,
32.4111385603901, 30.9181484377477, 25.1908965916373, 33.1378793967888,
37.1778275772231, 33.7549078873126, 30.3750618664781, 40.9762685345486,
27.3728471407667, 36.3470769267296, 33.8341794455191, 36.9817041397328,
32.4507015799172, 31.736502675456, 29.6957238939358, 36.1113904697122,
33.9414452506462, 36.9691713027423, 34.3095768736093, 32.6794349499396,
32.5801726981881, 27.2223803373054, 34.2432519724825, 36.7139034247957,
27.5831041259225, 41.5007923189551, 37.1082421375904, 30.3266722008702,
33.9694667824078, 35.6325324142817, 35.2054973669583, 29.9515829434386,
25.677076079417, 31.1902828949387, 32.4210338627454, 28.3575206745882,
32.5940152075491, 31.9765592545155, 41.3789200289175, 36.1047693482833,
41.7112493929453, 31.2430063028005, 34.3678300092579, 33.7714243110968,
27.4467708701268, 31.7782484822674, 27.5600393402856, 33.2923297870439,
38.9376543504186, 36.1805939215701, 36.2597199606244, 30.7401512296638,
27.6097705105785, 37.7263165470213, 30.2310251905583, 32.0336846167338,
30.4912316247355, 37.9383197620045, 29.5154438541504, 36.9983115129871,
32.7947406882304, 33.9285486157751, 31.0574057190097, 26.5039522824809,
31.8339186529629, 32.5527787177707, 31.8562467478914, 34.1271822586423,
26.0893318378367, 32.6484211806091, 32.8446673998842, 35.6959423848893,
38.4928932513576, 30.124668880133, 29.9166947266785, 35.4745850168983,
31.8399849826237, 34.1574638847378, 33.9537143341731, 32.5872485669679,
33.5270658220979, 31.9265234034974, 32.4627478372422, 29.9351938489126,
34.0414714779472, 31.2413191901287, 27.0438647172414, 19.2909317016602,
33.415520844399, 27.2705926514463, 42.3596463557333, 29.8770043778932,
33.5144147507963), Education = c("Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "HS Diploma", "HS Diploma", "HS Diploma",
"HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma",
"HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma",
"HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "College Degree"), Ethnicity = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), `Pre Weight` = c(192.056576386152,
199.200034264708, 206.876365089032, 195.881563220086, 200.023472577799,
209.835274224519, 197.638006672991, 188.53439444449, 197.556054379151,
195.261374235473, 183.834639690234, 206.592420843139, 201.388593429714,
210.134243322886, 210.711872502929, 191.050579749048, 197.758412402822,
194.271341504122, 198.923689983916, 198.967602661854, 192.920791894197,
203.365595032141, 199.845179783937, 196.529509982996, 209.761359590106,
195.030192050966, 191.221678439819, 188.893717611267, 205.644104360021,
193.924385191553, 195.469716734282, 205.8901999839, 201.684244009288,
199.956401891017, 202.022335022251, 204.845240593771, 201.497865807527,
214.361511032097, 190.574097748526, 197.017952975715, 200.709252046567,
196.371000972635, 185.010079737753, 197.991679603932, 201.731562744884,
206.684686806897, 198.528149601247, 190.756862982584, 201.257889946122,
201.911622232205, 202.823506631306, 192.12975887733, 196.74718730805,
197.705610551042, 198.324502368836, 199.593768163526, 183.679870976834,
195.266928974364, 195.89751916776, 209.72831764759, 198.465869677806,
207.538604561181, 205.284033133852, 195.080391611031, 193.136535406142,
202.807351731666, 194.04551510654, 201.363628937019, 200.066255208571,
203.038557790409, 191.260291037383, 200.227987240782, 201.838208956746,
196.743805124628, 195.478231878427, 202.583284069464, 191.771438428463,
206.52146661398, 193.483762293385, 190.160595982365, 206.933106189739,
206.326923423534, 205.130903784651, 198.517406195082, 202.249084556955,
200.390387751599, 199.138963969221, 204.348338163458, 198.339567011702,
197.763306621186, 198.144029061426, 191.630341694166, 202.501124865579,
210.04412604036, 202.621188398029, 207.952265488915, 197.56589056435,
197.411320802916, 199.215568434214, 195.430133251124), Intervention = c("Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout"),
`Post Weight` = c(183.884925652295, 199.800326915851, 194.671078067971,
203.366324901639, 200.073624389392, 198.747637957626, 197.077328200452,
205.294385457295, 203.63121523353, 202.319217967146, 194.309724454273,
204.37792175237, 201.774254426186, 199.169666236616, 196.435276100077,
188.147545420565, 192.339179127361, 197.267082901293, 193.759855326265,
194.147504705528, 210.371321306797, 198.317242327379, 193.102086020575,
186.002956913784, 187.153649610467, 197.444735638259, 199.397255917051,
199.896955043572, 200.289578221986, 200.359694579151, 194.154234966321,
201.103945462295, 198.381657289632, 174.026079230011, 195.176185252116,
192.193440534233, 189.540944711247, 195.402285634977, 190.26270833789,
188.311830833904, 193.327807801019, 186.754787273152, 189.741880512578,
184.221094195585, 194.408037395682, 187.552772240146, 178.570551406359,
199.402663974673, 189.554024381927, 195.699110599584, 196.260223355843,
184.848634171125, 194.972185590508, 186.89522462379, 195.963659870962,
189.975194325292, 185.117492173653, 188.429422814501, 191.845467861334,
184.460234458733, 181.766015566303, 195.604591362702, 185.29349076713,
185.749602360069, 186.493563341777, 194.774700184702, 170.250064507127,
192.264929460362, 194.279346537951, 194.095480815304, 192.09355792118,
205.814604264742, 201.010346295516, 205.747699560743, 209.566269909556,
196.717178873951, 190.261858329177, 210.87666987587, 184.596615831833,
200.588227067055, 204.084495230927, 185.477325026295, 206.07568608757,
199.725719135313, 193.537903719698, 202.579156443971, 204.824664756597,
197.221812009229, 181.948258209741, 198.307839632616, 212.828922990593,
183.819634508109, 209.631753528083, 201.93635969481, 191.993820458447,
210.423491403344, 196.130759427615, 216.762351151556, 194.786605839181,
214.179886420607), `Follow-up Weight` = c(196.754451103698,
216.156309357029, 210.484507129149, 180.150482770114, 192.390769421036,
203.047307473025, 212.955081274413, 181.630344336736, 194.986069395964,
189.119214660604, 210.027861208073, 191.293032053363, 210.267319794366,
209.707298431749, 187.987575877924, 174.893785292807, 205.768015398644,
198.770255160707, 211.5933289635, 182.237056883605, 175.814034769428,
202.397501930318, 180.344375671848, 211.968205626545, 222.363337848219,
179.163385432912, 205.026072358305, 188.93023303026, 198.485623109445,
219.364188119653, 175.826722220518, 176.65140654135, 198.980842974561,
232.273055694532, 202.227960749224, 221.662799554178, 212.652799341595,
164.536254992709, 181.760834089073, 197.487373093463, 204.981188794773,
219.285266716906, 230.166802389431, 185.610656949575, 202.313856839464,
191.789445630275, 198.555966840286, 181.177552399458, 200.24041355573,
198.072104290259, 190.301193975029, 186.122213613271, 180.075892835885,
207.392077350232, 193.10916223294, 192.415564520343, 208.723270639166,
214.108275081526, 206.051982381905, 205.617556553261, 223.241295821208,
206.747063707735, 222.621179596172, 193.962939015546, 191.982042502059,
183.000486736273, 194.58624415638, 203.504464984871, 192.261302850238,
217.979959920922, 206.377081263054, 190.020876339258, 207.567484772153,
191.626350492879, 206.334397539831, 214.376621493284, 204.515487717072,
195.394387042325, 190.006551797705, 179.74960024294, 223.11257958354,
205.544995929085, 191.169158874982, 177.952415974869, 190.496962709585,
200.471106886835, 185.645138167456, 214.420277238969, 219.606989098975,
183.150109983399, 209.9404815046, 205.259732915874, 195.812441966336,
197.740712842642, 181.31540905888, 221.787286641484, 207.900393939053,
180.352527018113, 245.932392822579, 213.72185352011), `Race/Ethnicity` = c("Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "White",
"White", "White", "White", "White", "White", "White", "White",
"Black", "Black", "Black", "Black", "Black", "Black", "Black",
"Black", "Black", "Black", "Black", "Black"), `Wieght Loss/Gain after Intervention` = c(8.17165073385695,
-0.600292651142809, 12.2052870210609, -7.48476168155321,
-0.0501518115925137, 11.0876362668932, 0.560678472538712,
-16.7599910128047, -6.07516085437965, -7.05784373167262,
-10.4750847640389, 2.2144990907691, -0.385660996471415, 10.9645770862699,
14.2765964028513, 2.90303432848305, 5.41923327546101, -2.99574139717151,
5.16383465765102, 4.82009795632621, -17.4505294125993, 5.04835270476178,
6.74309376336168, 10.5265530692122, 22.6077099796385, -2.41454358729243,
-8.17557747723185, -11.0032374323055, 5.354526138035, -6.43530938759795,
1.31548176796059, 4.78625452160486, 3.30258671965566, 25.9303226610064,
6.84614977013553, 12.6518000595388, 11.95692109628, 18.9592253971205,
0.311389410635456, 8.70612214181165, 7.38144424554775, 9.61621369948261,
-4.73180077482539, 13.770585408347, 7.3235253492021, 19.1319145667512,
19.9575981948874, -8.64580099208979, 11.7038655641954, 6.21251163262059,
6.56328327546362, 7.28112470620545, 1.77500171754218, 10.8103859272524,
2.36084249787382, 9.61857383823371, -1.43762119681924, 6.83750615986355,
4.05205130642571, 25.2680831888574, 16.6998541115026, 11.9340131984791,
19.9905423667224, 9.33078925096197, 6.64297206436459, 8.03265154696419,
23.795450599413, 9.0986994766572, 5.7869086706196, 8.94307697510521,
-0.833266883797478, -5.58661702396057, 0.827862661230029,
-9.00389443611493, -14.0880380311282, 5.86610519551323, 1.50958009928581,
-4.35520326188998, 8.88714646155131, -10.4276310846908, 2.84861095881206,
20.8495983972389, -0.944782302918611, -1.20831294023083,
8.71118083725742, -2.18876869237283, -5.68570078737685, 7.12652615422849,
16.3913088019617, -0.544533011430758, -14.6848939291667,
7.8107071860577, -7.13062866250402, 8.10776634555077, 10.6273679395817,
-2.47122591442894, 1.43513113673544, -19.3510303486401, 4.42896259503323,
-18.7497531694826), `Wieght Loss/Gain on Follow Up` = c(-12.8695254514023,
-16.3559824411786, -15.8134290611779, 23.2158421315253, 7.68285496835597,
-4.29966951539973, -15.8777530739608, 23.664041120559, 8.64514583756682,
13.2000033065415, -15.7181367537996, 13.0848896990065, -8.4930653681804,
-10.5376321951335, 8.4477002221538, 13.2537601277581, -13.4288362712832,
-1.50317225941399, -17.8334736372344, 11.9104478219233, 34.5572865373688,
-4.0802596029389, 12.7577103487274, -25.9652487127605, -35.2096882377518,
18.2813502053468, -5.62881644125446, 10.9667220133124, 1.80395511254028,
-19.0044935405022, 18.3275127458037, 24.4525389209448, -0.599185684928671,
-58.2469764645211, -7.05177549710788, -29.4693590199458,
-23.1118546303478, 30.866030642268, 8.50187424881733, -9.17554225955973,
-11.6533809937537, -32.5304794437543, -40.4249218768527,
-1.38956275398959, -7.90581944378209, -4.236673390129, -19.9854154339264,
18.2251115752151, -10.686389173803, -2.37299369067478, 5.95902938081417,
-1.273579442146, 14.8962927546236, -20.4968527264427, 2.85449763802171,
-2.44037019505049, -23.6057784655131, -25.6788522670249,
-14.2065145205706, -21.1573220945284, -41.4752802549046,
-11.1424723450327, -37.327688829042, -8.21333665547718, -5.48847916028171,
11.7742134484288, -24.3361796492536, -11.2395355245098, 2.01804368771263,
-23.8844791056181, -14.2835233418737, 15.7937279254838, -6.55713847663719,
14.1213490678638, 3.23187236972444, -17.6594426193333, -14.2536293878948,
15.4822828335455, -5.40993596587214, 20.8386268241156, -19.0280843526125,
-20.0676709027903, 14.9065272125881, 21.7733031604439, 3.04094101011287,
2.10804955713684, 19.1795265891415, -17.1984652297397, -37.658730889234,
15.1577296492178, 2.8884414859931, -21.4400984077656, 13.8193115617469,
4.19564685216756, 10.6784113995673, -11.3637952381396, -11.7696345114382,
36.4098241334432, -51.1457869833976, 0.458032900496619)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
附加代码块
错误照片:
```{r}
renderTable({
wl_data %>% group_by(across(all_of(input$categorical_variable))) %>%
summarise("Average " = mean(.data[[input$numeric_variable]]),
"Median " = median(.data[[input$numeric_variable]]),
"Freq " = count(.data[[input$categorical_variable]])
)
})
```
mean(wl_data[[input$numeric_variable]])
正在从整个数据帧中提取平均值,并且不考虑分组。请尝试使用 .data
。
另外 group_by_at
已被取代,我们可以使用 across
。
renderTable({
wl_data %>%
group_by(across(all_of(input$categorical_variable))) %>%
summarise(Average = mean(.data[[input$numeric_variable]]),
Median = median(.data[[input$numeric_variable]])
)
})
使用您的数据,这是一个简单的 Shiny 应用程序,可以执行我认为您想要的操作:
library(shiny)
library(DT)
categorical_variables = c( "Race/Ethnicity" ,"Gender", "Education", "Intervention")
numeric_variables = c("Age", "Pre Weight", "Post Weight", "Follow-up Weight","Wieght Loss/Gain after Intervention","Wieght Loss/Gain on Follow Up" )
ui <- {
fluidPage(
selectInput("categorical_var",
"Select categorical variable",
choices = categorical_variables
),
selectInput("numeric_var",
"Select numeric variable",
choices = numeric_variables
),
DTOutput("tbl")
)
}
server <- function(input, output, session) {
output$tbl <- renderDT({
dat %>%
group_by(!!sym(input$categorical_var)) %>%
summarise(
mean = mean(!!sym(input$numeric_var)),
median = median(!!sym(input$numeric_var))
)
})
}
shinyApp(ui, server)
几点:
group_by_at
已弃用,不要再使用它
- 您正在使您的
group_by
/summarise
变得比需要的更复杂 -- 它可以为您完成工作:)
这是我的代码
```{r}
#Assign Variables
Categorical.Variables = c( "Race/Ethnicity" ,"Gender", "Education", "Intervention")
Numeric.Variables = c("Age", "Pre Weight", "Post Weight", "Follow-up Weight","Wieght Loss/Gain after Intervention","Wieght Loss/Gain on Follow Up" )
```
我有这个简单的 table 我正在尝试用 dplyr 制作。
```{r}
renderTable({
wl_data %>% group_by_at(input$categorical_variable) %>%
summarise("Average " = mean(wl_data[[input$numeric_variable]]),
"Median " = median(wl_data[[input$numeric_variable]])
)
})
```
这是我得到的输出,它对整个数字输入具有相同的值,并且没有被分类输入分解。
我想要的输出是有一个反应性的 table,它将给我根据输入变化的汇总统计数据。到目前为止,我已经非常接近了,但我不知道如何让它发挥作用。 我做错了什么?
数据
> dput(wl_data)
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100), Gender = c("Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male"), Age = c(34.1958081813646, 38.342817530618, 31.4255555318668,
39.9174711825326, 30.4606180330738, 35.0906190911774, 33.4250614438206,
32.4111385603901, 30.9181484377477, 25.1908965916373, 33.1378793967888,
37.1778275772231, 33.7549078873126, 30.3750618664781, 40.9762685345486,
27.3728471407667, 36.3470769267296, 33.8341794455191, 36.9817041397328,
32.4507015799172, 31.736502675456, 29.6957238939358, 36.1113904697122,
33.9414452506462, 36.9691713027423, 34.3095768736093, 32.6794349499396,
32.5801726981881, 27.2223803373054, 34.2432519724825, 36.7139034247957,
27.5831041259225, 41.5007923189551, 37.1082421375904, 30.3266722008702,
33.9694667824078, 35.6325324142817, 35.2054973669583, 29.9515829434386,
25.677076079417, 31.1902828949387, 32.4210338627454, 28.3575206745882,
32.5940152075491, 31.9765592545155, 41.3789200289175, 36.1047693482833,
41.7112493929453, 31.2430063028005, 34.3678300092579, 33.7714243110968,
27.4467708701268, 31.7782484822674, 27.5600393402856, 33.2923297870439,
38.9376543504186, 36.1805939215701, 36.2597199606244, 30.7401512296638,
27.6097705105785, 37.7263165470213, 30.2310251905583, 32.0336846167338,
30.4912316247355, 37.9383197620045, 29.5154438541504, 36.9983115129871,
32.7947406882304, 33.9285486157751, 31.0574057190097, 26.5039522824809,
31.8339186529629, 32.5527787177707, 31.8562467478914, 34.1271822586423,
26.0893318378367, 32.6484211806091, 32.8446673998842, 35.6959423848893,
38.4928932513576, 30.124668880133, 29.9166947266785, 35.4745850168983,
31.8399849826237, 34.1574638847378, 33.9537143341731, 32.5872485669679,
33.5270658220979, 31.9265234034974, 32.4627478372422, 29.9351938489126,
34.0414714779472, 31.2413191901287, 27.0438647172414, 19.2909317016602,
33.415520844399, 27.2705926514463, 42.3596463557333, 29.8770043778932,
33.5144147507963), Education = c("Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Less than HS", "HS Diploma", "HS Diploma", "HS Diploma",
"HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma",
"HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma",
"HS Diploma", "HS Diploma", "HS Diploma", "HS Diploma", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "Some College", "Some College", "Some College",
"Some College", "College Degree"), Ethnicity = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), `Pre Weight` = c(192.056576386152,
199.200034264708, 206.876365089032, 195.881563220086, 200.023472577799,
209.835274224519, 197.638006672991, 188.53439444449, 197.556054379151,
195.261374235473, 183.834639690234, 206.592420843139, 201.388593429714,
210.134243322886, 210.711872502929, 191.050579749048, 197.758412402822,
194.271341504122, 198.923689983916, 198.967602661854, 192.920791894197,
203.365595032141, 199.845179783937, 196.529509982996, 209.761359590106,
195.030192050966, 191.221678439819, 188.893717611267, 205.644104360021,
193.924385191553, 195.469716734282, 205.8901999839, 201.684244009288,
199.956401891017, 202.022335022251, 204.845240593771, 201.497865807527,
214.361511032097, 190.574097748526, 197.017952975715, 200.709252046567,
196.371000972635, 185.010079737753, 197.991679603932, 201.731562744884,
206.684686806897, 198.528149601247, 190.756862982584, 201.257889946122,
201.911622232205, 202.823506631306, 192.12975887733, 196.74718730805,
197.705610551042, 198.324502368836, 199.593768163526, 183.679870976834,
195.266928974364, 195.89751916776, 209.72831764759, 198.465869677806,
207.538604561181, 205.284033133852, 195.080391611031, 193.136535406142,
202.807351731666, 194.04551510654, 201.363628937019, 200.066255208571,
203.038557790409, 191.260291037383, 200.227987240782, 201.838208956746,
196.743805124628, 195.478231878427, 202.583284069464, 191.771438428463,
206.52146661398, 193.483762293385, 190.160595982365, 206.933106189739,
206.326923423534, 205.130903784651, 198.517406195082, 202.249084556955,
200.390387751599, 199.138963969221, 204.348338163458, 198.339567011702,
197.763306621186, 198.144029061426, 191.630341694166, 202.501124865579,
210.04412604036, 202.621188398029, 207.952265488915, 197.56589056435,
197.411320802916, 199.215568434214, 195.430133251124), Intervention = c("Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Placebo Diet", "Placebo Diet", "Placebo Diet", "Placebo Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Diet", "Adjusted Diet",
"Adjusted Diet", "Adjusted Diet", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout", "Adjusted Workout",
"Adjusted Workout", "Adjusted Workout", "Adjusted Workout"),
`Post Weight` = c(183.884925652295, 199.800326915851, 194.671078067971,
203.366324901639, 200.073624389392, 198.747637957626, 197.077328200452,
205.294385457295, 203.63121523353, 202.319217967146, 194.309724454273,
204.37792175237, 201.774254426186, 199.169666236616, 196.435276100077,
188.147545420565, 192.339179127361, 197.267082901293, 193.759855326265,
194.147504705528, 210.371321306797, 198.317242327379, 193.102086020575,
186.002956913784, 187.153649610467, 197.444735638259, 199.397255917051,
199.896955043572, 200.289578221986, 200.359694579151, 194.154234966321,
201.103945462295, 198.381657289632, 174.026079230011, 195.176185252116,
192.193440534233, 189.540944711247, 195.402285634977, 190.26270833789,
188.311830833904, 193.327807801019, 186.754787273152, 189.741880512578,
184.221094195585, 194.408037395682, 187.552772240146, 178.570551406359,
199.402663974673, 189.554024381927, 195.699110599584, 196.260223355843,
184.848634171125, 194.972185590508, 186.89522462379, 195.963659870962,
189.975194325292, 185.117492173653, 188.429422814501, 191.845467861334,
184.460234458733, 181.766015566303, 195.604591362702, 185.29349076713,
185.749602360069, 186.493563341777, 194.774700184702, 170.250064507127,
192.264929460362, 194.279346537951, 194.095480815304, 192.09355792118,
205.814604264742, 201.010346295516, 205.747699560743, 209.566269909556,
196.717178873951, 190.261858329177, 210.87666987587, 184.596615831833,
200.588227067055, 204.084495230927, 185.477325026295, 206.07568608757,
199.725719135313, 193.537903719698, 202.579156443971, 204.824664756597,
197.221812009229, 181.948258209741, 198.307839632616, 212.828922990593,
183.819634508109, 209.631753528083, 201.93635969481, 191.993820458447,
210.423491403344, 196.130759427615, 216.762351151556, 194.786605839181,
214.179886420607), `Follow-up Weight` = c(196.754451103698,
216.156309357029, 210.484507129149, 180.150482770114, 192.390769421036,
203.047307473025, 212.955081274413, 181.630344336736, 194.986069395964,
189.119214660604, 210.027861208073, 191.293032053363, 210.267319794366,
209.707298431749, 187.987575877924, 174.893785292807, 205.768015398644,
198.770255160707, 211.5933289635, 182.237056883605, 175.814034769428,
202.397501930318, 180.344375671848, 211.968205626545, 222.363337848219,
179.163385432912, 205.026072358305, 188.93023303026, 198.485623109445,
219.364188119653, 175.826722220518, 176.65140654135, 198.980842974561,
232.273055694532, 202.227960749224, 221.662799554178, 212.652799341595,
164.536254992709, 181.760834089073, 197.487373093463, 204.981188794773,
219.285266716906, 230.166802389431, 185.610656949575, 202.313856839464,
191.789445630275, 198.555966840286, 181.177552399458, 200.24041355573,
198.072104290259, 190.301193975029, 186.122213613271, 180.075892835885,
207.392077350232, 193.10916223294, 192.415564520343, 208.723270639166,
214.108275081526, 206.051982381905, 205.617556553261, 223.241295821208,
206.747063707735, 222.621179596172, 193.962939015546, 191.982042502059,
183.000486736273, 194.58624415638, 203.504464984871, 192.261302850238,
217.979959920922, 206.377081263054, 190.020876339258, 207.567484772153,
191.626350492879, 206.334397539831, 214.376621493284, 204.515487717072,
195.394387042325, 190.006551797705, 179.74960024294, 223.11257958354,
205.544995929085, 191.169158874982, 177.952415974869, 190.496962709585,
200.471106886835, 185.645138167456, 214.420277238969, 219.606989098975,
183.150109983399, 209.9404815046, 205.259732915874, 195.812441966336,
197.740712842642, 181.31540905888, 221.787286641484, 207.900393939053,
180.352527018113, 245.932392822579, 213.72185352011), `Race/Ethnicity` = c("Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "White",
"White", "White", "White", "White", "White", "White", "White",
"Black", "Black", "Black", "Black", "Black", "Black", "Black",
"Black", "Black", "Black", "Black", "Black"), `Wieght Loss/Gain after Intervention` = c(8.17165073385695,
-0.600292651142809, 12.2052870210609, -7.48476168155321,
-0.0501518115925137, 11.0876362668932, 0.560678472538712,
-16.7599910128047, -6.07516085437965, -7.05784373167262,
-10.4750847640389, 2.2144990907691, -0.385660996471415, 10.9645770862699,
14.2765964028513, 2.90303432848305, 5.41923327546101, -2.99574139717151,
5.16383465765102, 4.82009795632621, -17.4505294125993, 5.04835270476178,
6.74309376336168, 10.5265530692122, 22.6077099796385, -2.41454358729243,
-8.17557747723185, -11.0032374323055, 5.354526138035, -6.43530938759795,
1.31548176796059, 4.78625452160486, 3.30258671965566, 25.9303226610064,
6.84614977013553, 12.6518000595388, 11.95692109628, 18.9592253971205,
0.311389410635456, 8.70612214181165, 7.38144424554775, 9.61621369948261,
-4.73180077482539, 13.770585408347, 7.3235253492021, 19.1319145667512,
19.9575981948874, -8.64580099208979, 11.7038655641954, 6.21251163262059,
6.56328327546362, 7.28112470620545, 1.77500171754218, 10.8103859272524,
2.36084249787382, 9.61857383823371, -1.43762119681924, 6.83750615986355,
4.05205130642571, 25.2680831888574, 16.6998541115026, 11.9340131984791,
19.9905423667224, 9.33078925096197, 6.64297206436459, 8.03265154696419,
23.795450599413, 9.0986994766572, 5.7869086706196, 8.94307697510521,
-0.833266883797478, -5.58661702396057, 0.827862661230029,
-9.00389443611493, -14.0880380311282, 5.86610519551323, 1.50958009928581,
-4.35520326188998, 8.88714646155131, -10.4276310846908, 2.84861095881206,
20.8495983972389, -0.944782302918611, -1.20831294023083,
8.71118083725742, -2.18876869237283, -5.68570078737685, 7.12652615422849,
16.3913088019617, -0.544533011430758, -14.6848939291667,
7.8107071860577, -7.13062866250402, 8.10776634555077, 10.6273679395817,
-2.47122591442894, 1.43513113673544, -19.3510303486401, 4.42896259503323,
-18.7497531694826), `Wieght Loss/Gain on Follow Up` = c(-12.8695254514023,
-16.3559824411786, -15.8134290611779, 23.2158421315253, 7.68285496835597,
-4.29966951539973, -15.8777530739608, 23.664041120559, 8.64514583756682,
13.2000033065415, -15.7181367537996, 13.0848896990065, -8.4930653681804,
-10.5376321951335, 8.4477002221538, 13.2537601277581, -13.4288362712832,
-1.50317225941399, -17.8334736372344, 11.9104478219233, 34.5572865373688,
-4.0802596029389, 12.7577103487274, -25.9652487127605, -35.2096882377518,
18.2813502053468, -5.62881644125446, 10.9667220133124, 1.80395511254028,
-19.0044935405022, 18.3275127458037, 24.4525389209448, -0.599185684928671,
-58.2469764645211, -7.05177549710788, -29.4693590199458,
-23.1118546303478, 30.866030642268, 8.50187424881733, -9.17554225955973,
-11.6533809937537, -32.5304794437543, -40.4249218768527,
-1.38956275398959, -7.90581944378209, -4.236673390129, -19.9854154339264,
18.2251115752151, -10.686389173803, -2.37299369067478, 5.95902938081417,
-1.273579442146, 14.8962927546236, -20.4968527264427, 2.85449763802171,
-2.44037019505049, -23.6057784655131, -25.6788522670249,
-14.2065145205706, -21.1573220945284, -41.4752802549046,
-11.1424723450327, -37.327688829042, -8.21333665547718, -5.48847916028171,
11.7742134484288, -24.3361796492536, -11.2395355245098, 2.01804368771263,
-23.8844791056181, -14.2835233418737, 15.7937279254838, -6.55713847663719,
14.1213490678638, 3.23187236972444, -17.6594426193333, -14.2536293878948,
15.4822828335455, -5.40993596587214, 20.8386268241156, -19.0280843526125,
-20.0676709027903, 14.9065272125881, 21.7733031604439, 3.04094101011287,
2.10804955713684, 19.1795265891415, -17.1984652297397, -37.658730889234,
15.1577296492178, 2.8884414859931, -21.4400984077656, 13.8193115617469,
4.19564685216756, 10.6784113995673, -11.3637952381396, -11.7696345114382,
36.4098241334432, -51.1457869833976, 0.458032900496619)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
附加代码块
错误照片:
renderTable({
wl_data %>% group_by(across(all_of(input$categorical_variable))) %>%
summarise("Average " = mean(.data[[input$numeric_variable]]),
"Median " = median(.data[[input$numeric_variable]]),
"Freq " = count(.data[[input$categorical_variable]])
)
})
```
mean(wl_data[[input$numeric_variable]])
正在从整个数据帧中提取平均值,并且不考虑分组。请尝试使用 .data
。
另外 group_by_at
已被取代,我们可以使用 across
。
renderTable({
wl_data %>%
group_by(across(all_of(input$categorical_variable))) %>%
summarise(Average = mean(.data[[input$numeric_variable]]),
Median = median(.data[[input$numeric_variable]])
)
})
使用您的数据,这是一个简单的 Shiny 应用程序,可以执行我认为您想要的操作:
library(shiny)
library(DT)
categorical_variables = c( "Race/Ethnicity" ,"Gender", "Education", "Intervention")
numeric_variables = c("Age", "Pre Weight", "Post Weight", "Follow-up Weight","Wieght Loss/Gain after Intervention","Wieght Loss/Gain on Follow Up" )
ui <- {
fluidPage(
selectInput("categorical_var",
"Select categorical variable",
choices = categorical_variables
),
selectInput("numeric_var",
"Select numeric variable",
choices = numeric_variables
),
DTOutput("tbl")
)
}
server <- function(input, output, session) {
output$tbl <- renderDT({
dat %>%
group_by(!!sym(input$categorical_var)) %>%
summarise(
mean = mean(!!sym(input$numeric_var)),
median = median(!!sym(input$numeric_var))
)
})
}
shinyApp(ui, server)
几点:
group_by_at
已弃用,不要再使用它- 您正在使您的
group_by
/summarise
变得比需要的更复杂 -- 它可以为您完成工作:)