R Shiny在创建对象/单击按钮后创建menuItem

时间:2017-08-21 20:19:30

标签: r shiny shinydashboard

我尝试在创建对象或单击按钮(理想情况为对象)时动态生成menuItem。我尝试了多种方法,似乎无法找到一个干净,有效的解决方案。

我有很多代码,所以下面应该包含示例代码:

import numpy as np
import itertools

def uniquewords(*args):
    """Dictionary of words to their indices in the matrix"""
    words = {}
    n = 0
    for word in itertools.chain(*args):
        if word not in words:
            words[word] = n
            n += 1
    return words

def encode(*args):
    """One-hot encode the given input strings"""
    args_with_words = [arg.split() for arg in args]
    unique = uniquewords(*args_with_words)
    feature_vectors = np.zeros((len(args), len(unique)))
    for vec, s in zip(feature_vectors, args_with_words):
        for word in s:                
            vec[unique[word]] = 1
    return feature_vectors

print encode("awaken my love", "awaken the beast", "wake beast love")

上面我有2个标签,1个带按钮(显示),另一个带有图表(隐藏)。

  • 如何在点击按钮时显示隐藏的标签?
  • 对于奖励积分,假设按钮改为创建了一个对象,如何在创建所述对象的情况下显示隐藏的menuItem

由于

1 个答案:

答案 0 :(得分:1)

我设法解决了这个问题。下面是按显示按钮创建menuItem的代码。

ui <- fluidPage(
    dashboardPage(
        dashboardHeader(title = "text"),
        dashboardSidebar(
            sidebarMenu(id = 'MenuTabs',
                        menuItem("Tab1", tabName = "tab1", selected = TRUE),
                        # menuItem("Tab1", tabName = "tab2")
                        uiOutput('ui')
            )
        ),
        dashboardBody(
            tabItems(
                tabItem("tab1",
                        actionButton("newplot", "New plot"),
                        actionButton("show", "Show")),
                tabItem("tab2",
                        plotOutput('Plot'))
             )
        )
    )
)


server <- function(input, output, session){

    output$Plot <- renderPlot({
        input$newplot
        # Add a little noise to the cars data
        cars2 <- cars + rnorm(nrow(cars))
        plot(cars2)
    })


    output$ui <- renderUI({
        if(input$show == 0) return()
        print(input$show)
        sidebarMenu(id = 'MenuTabs',
                    menuItem("Tab1", tabName = "tab2")
        )
    })
}


shinyApp(ui, server)