user2478159 user2478159 -3 years ago 117
R Question

adding a level as default value on Shiny select input

Suppose I hage some levels in a dataframe and I want to default the value to a certain level, so I tried this

selectInput('type', h4("some text"), c(levels(df$type)),value=as.factor(levels(df$type)[1])))

and I get this error message

ERROR: unused argument (value = as.factor(levels(df$type)[1]))

Of course, since
is equal to some string
; I tried to put directly
but it errorred out the same way.

Can someone help ?

Answer Source

You should use instead of value =: selected =

So the full answer would be:

selectInput('type', h4("some text"), c(levels(df$type)),selected=as.factor(levels(df$type)[1])))

There is no such an argument in selectInput as value. Please refer to this link.


selectInput('type', h4("some text"), choices=levels(df$type),selected=levels(df$type)[1]))

if you call levels, then your output is already a vector, there is no need to use c() after level().

Additionally your df$type must already be a factor (because you use levels() argument)!Therefore you do not need to use selected=as.factor(levels(...)[1])), instead use simply selected=levels(...)[1]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download