eval(parse(text))) in Shiny

889 views
Skip to first unread message

charlot...@gmail.com

unread,
Jun 18, 2014, 10:03:28 AM6/18/14
to shiny-...@googlegroups.com
Hi everybody !!

My app needs to have sliderInputs but the number of these sliders are depending on the operator.
To solve the problem I coded :

n= 10

library("shiny")
runApp(list( 
  ui = fluidPage(
    titlePanel("title panel"),
   
    sidebarLayout(
      sidebarPanel("sidebar panel",
                   uiOutput('SAMPLEselection1'),
                   for(p in 2:(n-1)){                    
                     eval(parse(text= paste("uiOutput(","\'","SAMPLEselection",p,"\'","),",sep="")))
                   }
                   uiOutput('SAMPLEselection10')
                   ),
      mainPanel("main panel"))
  )
  ,
  server = function(input, output, session) {
   
    for(i in 1:n){
      eval(parse(text = paste("output$SAMPLEselection",i, "= renderUI({","\n","selectInput(","\"","SampledirSelection",i,"\"",",","\"", "Choose the Sample to analyze","\"", ",c(1,2,3))","\n","})",sep ="")))
      }
    }


))
But it doesn't work.
I don't why ??!! Perhaps it is not possible to have a "for" in the ui ?
Thanks in advance for your answers.

Charlotte

Yihui Xie

unread,
Jul 14, 2014, 2:54:10 PM7/14/14
to charlot...@gmail.com, shiny-discuss
If you find you need eval(parse(text)) in shiny, you are almost always
on the wrong way. The super power of eval(parse()) is rarely needed,
and should be avoided for security reasons. Here is an example of how
you can achieve your goal more elegantly without security holes:

library(shiny)
runApp(list(
ui = fluidPage(lapply(1:10, function(i) {
uiOutput(paste0('x', i))
})),
server = function(input, output) {
lapply(1:10, function(i) {
output[[paste0('x', i)]] = renderUI(paste('Hi, this is', i))
})
}
))


Regards,
Yihui

charlot...@gmail.com

unread,
Jul 23, 2014, 2:43:26 AM7/23/14
to shiny-...@googlegroups.com
Thank you very much Yihui !
I am developing the code and I will share it with the group ASAP
Again thank you for your advice !
bye
Reply all
Reply to author
Forward
0 new messages