在 R Shiny 中自动生成 GUI

问题描述:

我遇到的问题是我无法在 Shiny 中自动生成 GUI.这个想法是查看有多少文件并为每个文件生成一组图像+按钮.我相信这个问题的解决方案是在 R 中ANY GUI 生成的解决方案.

The problem I have is that I am not able to automatically generate a GUI in Shiny. The idea is to see how many files there are and generate a set of Image + button for each file. I believe the solution to this problem is the solution to ANY GUI generation in R.

我可以静态地执行此操作,为每个按钮和图像编写代码,但是将其放入 for 循环或在 server.R 中呈现图像并将其作为 HTML 传递给ui.R.我会在下面解释.

I am able to do this statically, writing the code for each button and image, but it doesn't work to put it in a for loop, or to render the image in the server.R and pass it as HTML to the ui.R. I will explain below.

我要生成的代码是:

actionButton("sug1",icon=imageOutput("sug1",width="100px",height="100px"),label="First")

这给了我一个 100x100 的可点击图片.

Which gives me a 100x100 clickable image.

我尝试了以下方法:

1) 在 ui.R 中用 for 循环包围它,并使 ID("sug1") 和标签成为变量,其中最后一个数字增加每个循环.

1) Surrounding it with a for loop inside of ui.R and making the ID("sug1") and the label a variable where the last number increments each loop.

2) 累加结果并使用 server.R 中的 HTML() 函数稍后在 ui.R 中输出

2) Accumulating the result and using the HTML() function in server.R to later output it in ui.R

output$generateImages <- renderUI({
    (...)
    for(...){
        (...)
        w <- paste(w, actionButton(paste("oc",which(dir==folders)),label=dir))
   }
   HTML(w)
   })

然后在 ui.R 中我希望它出现的地方:

and then in ui.R in the place I want it to appear:

htmlOutput("generateImages")

3) 我猜使用 HTMLoutput 或 UIOutput 应该会有所帮助,但鉴于我的代码生成的 HTML 输出(如右击/查看页面源代码所示)是:

3) I guessed that using HTMLoutput or UIOutput should help, but given that the HTML output my code generates(as seen in righ tlick/view page source) is:

<button id="sug1" type="button" class="btn action-button">
<div id="sug1" class="shiny-image-output" style="width: 100px ; height: 100px"></div>
First
</button>

我无法弄清楚如何生成它,因为我不知道如何以及在何处插入对图像的引用.

I was not able to figure out how to generate this as I knew not how and where to insert a reference to an image.

希望得到帮助.

感谢 Jeff 在评论部分的帮助,我能够得到一些循环并生成 UI 元素的东西.

Thanks to the help of Jeff in the comment section, I was able to get something that loops and generates elements of UI.

整个技巧是有一个 renderUI 函数,里面有一个 for 循环,它将元素累积在一个列表中,在我的例子中是:

The whole trick is to have a renderUI function, that has a for loop inside, which accumulates the elements in a list, which in my case is:

LL[[i]] <- list(actionButton(txt,icon=imageOutput(pp,width="100px",height="100px"),label=dir))

此列表必须由函数返回.所有这些都在 server.R 中.在 ui.R 中应该这样写:

This list has to be returned by the function. All this is in server.R. In ui.R one should write:

uiOutput(nameOfTheFunctionThatReturnedTheList)

但是我的问题仍然是我无法使用相同的循环显示图像...帮助任何人?;)

My problem still is however that I cannot make it to display images using the same loop...Help anyone? ;)