作者clsmbstu ()
看板R_Language
标题[问题] Shiny app中对input使用which函数
时间Sat Feb 18 18:22:18 2017
[问题类型]:
程式谘询(我想用R做某件事情,但是我不知道要怎麽用R写出来)
[软体熟悉度]:
使用者(已经有用R做过不少作品)
PS: 但是第一次接触Shiny
[问题叙述]:
我正在用R内建的airquality,练习做Shiny App。
我想达到的目标是,使用者可自行选择:
1. 想看的月份(从五月到九月任一)
2. 关心的变数(温度、辐射强度等任一)
我想说做精美一点,月份不要用阿拉伯数字让使用者选,
而是真的可以从"May"、"June"、"July"...之中选一个,
我再依据使用者的选择subset data frame,
但会出现错误讯息:
Error in ==: comparison (1) is possible only for atomic and list types
如果我直接让使用者从阿拉伯数字择一就没有问题。
[程式范例]:
https://gist.github.com/anonymous/62410421333fd6fbca794ba64c6dbd65
里面包含:
server.R(我认为问题可能出在第6行)
ui.R(请留意第12行的设计)
[环境叙述]:
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950
[2] LC_CTYPE=Chinese (Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_2.2.1 shiny_1.0.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.9 assertthat_0.1 digest_0.6.12 mime_0.5
[5] grid_3.3.2 R6_2.2.0 plyr_1.8.4 xtable_1.8-2
[9] jsonlite_1.2 gtable_0.2.0 scales_0.4.1 lazyeval_0.2.0
[13] labeling_0.3 tools_3.3.2 munsell_0.4.3 httpuv_1.3.3
[17] colorspace_1.3-2 htmltools_0.3.5 sourcetools_0.1.5 tibble_1.2
[关键字]:
Shiny, which, comparison
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.42.143.128
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1487413342.A.7DD.html
1F:→ clsmbstu: 我灵机一动,把airquality的月份改成英文,顺利解决! 02/18 21:19
2F:→ clsmbstu: 但还是不懂为什麽input无法用which来比对... 02/18 21:19
3F:推 clansoda: 因为你的资料本来就没有英文月份 他抓不到 02/18 21:37
5F:→ clansoda: switch function might work 02/18 21:43
6F:推 cywhale: match(input$month,month.name[5:9])+4L 02/18 21:53
7F:→ cywhale: data = subset(airquality, Month == month_selected()) 02/18 21:53
8F:→ clsmbstu: clansoda,但是我比对的对象并不是我的资料啊? @@" 02/18 22:10
9F:→ clsmbstu: cywhale,我没想到match,感谢你! 02/18 22:12
10F:→ clsmbstu: 如此应该match(input$month, month.name)也可以 晚点试 02/18 22:13
11F:→ clsmbstu: 好奇input$month究竟是什麽资料结构,==会不能用? 02/18 22:14
12F:→ cywhale: 是的,只要传回正确的index即可,资料是数字,input是 02/18 22:15
13F:→ cywhale: 月份当然不行..另外 Month==那一行subsetting也有bug如上 02/18 22:16
14F:→ cywhale: input$month就是传回你自己写的choices.. 02/18 22:17
15F:→ cywhale: 下次你若不确定到底传回来什麽,就print(input$..) debug 02/18 22:18
16F:→ clsmbstu: 对!我也发现了Month == 那一行有问题! 02/18 22:25
17F:→ clsmbstu: 我写Month == month_selected,少了小括号 02/18 22:26
18F:→ clsmbstu: 很可能问题是出在这边 而非which函数 02/18 22:27
19F:→ clsmbstu: 第一次写Shiny真的不太熟,感谢大家! :D 02/18 22:27
※ 编辑: clsmbstu (114.42.143.128), 02/18/2017 23:33:10