我尝试在创建对象或单击按钮(理想情况为对象)时动态生成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个带按钮(显示),另一个带有图表(隐藏)。
由于
答案 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)