0
UI.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Example"),
sidebarLayout(
sidebarPanel(
radioButtons("orderdata", "Sort by :",
c("Name" = "name",
"MRDNo" = "mrdno"
))
),
mainPanel(
uiOutput("deatilscv")
)
)
))
Server.R
library(shiny)
library(shinyjs)
shinyServer(function(input, output) {
observeEvent(input$orderdata,
{
output$deatilscv <- renderUI({
if(input$orderdata=="name")
{
mid<-c("1","2")
name<-c("a","b")
datatable1 <- data.frame(mid,name)
fluidPage(shinyjs::useShinyjs(),
actionButton("button1", "CLICK") )
}
else if(input$orderdata=="mrdno")
{
mid<-c("3","4")
name<-c("c","d")
datatable2 <- data.frame(mid,name)
fluidPage(shinyjs::useShinyjs(),
actionButton("button1", "CLICK") )
}
})
})
observeEvent(
input$button1,{
a <- datatable1[1,2] #this shows an error object 'datatable1' not found
print(a)
})
observeEvent(
input$button2,{
a <- datatable2[1,2] #this shows an error object 'datatable2' not found
print(a)
})
})
有如圖above.How可在數據表中觀察事件來訪問在程序兩個錯誤?
你的數據是在'observeEvent'函數中定義的......所以它在按鈕函數裏面是不知道的。在全局或直接在'shinyServer'中定義數據。 – drmariod
先生,但我希望只有單擊單選按鈕時才能爲數據表1和數據表2分配一個數據框,這就是爲什麼我將數據置於observeEvent中的原因。 – Rad
嘗試熟悉像這裏http://shiny.rstudio.com/tutorial/lesson6/這樣的反應式表達式,這是如何基於單選按鈕選擇數據更新以及如何使用其他功能中的數據 – drmariod